Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

E.2. Question 2: Sorting an Array of Numbers Write a program which inputs and so

ID: 3846656 • Letter: E

Question

E.2. Question 2: Sorting an Array of Numbers Write a program which inputs and sorts an array of 10 numbers. Program Requirements: Use the header function you created in your last lab and call it with appropriate arguments. Declare an integer array of size 10 in the main function. Ask the user to input ten integers which should be stored in the above array. When the user has finished entering the numbers, the program will call three functions. o The first function will Accept the array as a parameter. Calculate the average of the array Return the average of the array. o The second function will Accept the array as a parameter. array from greatest to least. (See the tutorial called Sorting Array' below) o The third function will Accept the array as a parameter. array from least greatest. (See the tutorial called Sorting Array below) The main function will then print the average and the sorted array. The two functions cannot contain any cout statements, and you cannot use any global variables

Explanation / Answer

#include<iostream.h>
#include<conio.h>

void sort(int x[ ]);
float average(int x[ ]);
main()
{   
int i;
int a[10],asc[10],des[10] ;
float avg;
      
printf("Please enter ten integers to be entered into array A ");   
for(i = 0; i < 10; i++)
{
   cout<<"Enter A["<<i<<"] ";
       cin >> A[i];
   }
for (i = 0; i < 10; i++)   
{
       asc[i]=a[i];
       des[i]=a[i];
   }
sort_asc (asc );
sort_des (des );
   avg=average(a);
   cout<<"Contents of A=[";
for(i = 0; i < 10; i++)
cout<<a[i]<<" ";
cout<<"] ";
  
   cout<<"Sorted Contents of A in ascending order=[";
for(i = 0; i < 10; i++)
cout<<asc[i]<<" ";
cout<<"] ";
     
   cout<<"Sorted Contents of A in descending order=[";
for(i = 0; i < 10; i++)
cout<<des[i]<<" ";
cout<<"] ";

   cout<<" The Average of the elements in array A is "<< avg;
   getc();
  
}   
void sort_asc(int x[ ])
{   
int i, j, t;

for(i = 0; i < 10i++)   
for(j = 1; j < 10;j++)   
if(x[j-1] > x[j])
{
t = x[j-1];
x[j-1] = x[j];   
x[j] = t;
}   
}
  
void sort_des(int x[ ])
{   
int i, j, t;

for(i = 0; i < 10i++)   
for(j = 1; j < 10;j++)   
if(x[j-1] < x[j])
{
t = x[j-1];
x[j-1] = x[j];   
x[j] = t;
}   
}

float average(int x[ ])
{
  
       float avg;
       int sum=0;
       for (int i=0;i<10;i++)
       sum+=x[i];
       avg=sum/10;
       return avg;
}

As mentioned in the problem use the function which is already with you for printing header.