Question: In this program, you will take command-line arguments of 10 numbers, which are assumed to be integers, and find the median or the average of
In this program, you will take command-line arguments of 10 numbers, which are assumed to be integers, and find the median or the average of them. The average of the array should be a floating point number (double). Before the numbers on the command line, there will be an argument that indicates whether to find the median (m) or average (-a), if the user entered an invalid option or incorrect number of arguments, the program should display an error message. To find the median of an array, use the selection_sort function provided to sort the array, the median is the number that is halfway into the array: a[n/2], where n is the number of the elements in array a.
Use this selection_sort function provided below in the program (do not modify any of the provided function given below):
void selection_sort(int a[], int n)
{
int i, largest = 0, temp;
if (n == 1)
return;
for (i = 1; i < n; i++)
if (a[i] > a[largest])
largest = i;
if (largest < n - 1) {
temp = a[n-1];
a[n-1] = a[largest];
a[largest] = temp;
}
selection_sort(a, n - 1);
}
Example input and output:
./a.out
echo 'Expected:'
echo 'Invaild number of arguments. Usage: ./a.out -a (or -m) followed by 10 integers'
./a.out -a 10 9 23 24 559 20 84 12 48 98
echo 'Expected:'
echo 'Average: 88.700000'
./a.out -m 8 12 42 10 89 4 1 39 102 3
echo 'Expected:'
echo 'Median: 12'
./sort -h 8 12 42 10 89 4 1 39 102 3
echo 'Expected:'
echo 'Invalid option'
1) Name the program command_line.c
2) Use strcmp function to process the first command line argument.
3) Use atoi function in
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
