Answer to Question #251244 in C++ for aqeel shehzad

Question #251244

Write a function to sort data. (in increasing order) in an array using

a. pass by value

b. and pass by reference.?


1
Expert's answer
2021-10-14T07:55:23-0400
#include<iostream>
using namespace std;
//Pass by Reference
void sort(int *A, int n)
{
int i, j,temp;
for (i = 0; i < n-1; i++)     
for (j = 0; j < n-1; j++)
if (A[j] > A[j+1])
{
temp= A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}


//Pass by value
 void Sort(int A[], int m)
{ 


int j, i,temp;
for (j = 0; j < m-1; j++)     
for (i = 0; i < m-j-1; i++)
if (A[i] > A[i+1])
{
temp= A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
}
}


//Testing code


int main(){
	int A[] = {10,5,50,15,85,2,32,18};
int m = sizeof(A)/sizeof(A[0]);
Sort(A,m);
for(int i=0; i<m; i++){
	cout<<A[i]<<"  ";
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS