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

In this C program, you will take command-line arguments of 10 numbers, which are

ID: 3587773 • Letter: I

Question

In this C 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 above 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.

input and output:

./a.out –a 2 6 8 4 9 10 7 3 11 5

output: 6.5

./a.out –m 2 6 8 4 9 10 7 3 11 5

output: 7

./a.out

output: usage: ./a.out –option (a or m) followed by 10 numbers

./a.out –d 2 6 8 4 9 10 7 3 11 5

output: 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 to convert a string to integer form.

1 selection sort.c, project 6, Program Design 4 #include 5 #define N 10 7 void selection sort (int ai, int n); 9 int main (void) 10 11 int i; 12 int a[N] 13 14 printf( "Enter %d numbers to be sorted. ", N); 15 for (i=0;i

Explanation / Answer

Tested on windows and it will work on Ubuntu as well

/***************************command_line.c***********************/

#include<stdio.h>

#include <string.h>

#include <stdlib.h>

#define N 10

// selection sort function

void selection_sort(int a[], int n);

int main(int argc, char *argv[])

{

// variable declaration

int i,j=2;

int a[N];

// checking number of argument

if( argc < 12 || argc > 12) {

printf("usage: ./a.out –option (a or m) followed by 10 numbers ");

return 1;

}

// storing value in to integer array with conversion string to integer

for(i=0; i<N; i++) {

a[i]=atoi(argv[j++]);

}

// comparing option for average

if(strcmp(argv[1],"-a")==0) {

int sum = 0;

for( i=0;i<N;i++){

sum+=a[i];

}

float average = (float)sum/N;

printf(" %.1f ", average);

} else if(strcmp(argv[1],"-m")==0) { // comparing option with for median

selection_sort(a,N);

printf(" %d", a[N/2]);

} else{

printf(" Invalid option ");

}

printf(" ");

return 0;

}

// selection sort implementation

void selection_sort(int a[], int n) {

int i, largest = 0, temp;

if(n==i) {

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);

}

}

/*********************output***********************************/

C:UserslmaliDesktopChegg>g++ command_line.c

C:UserslmaliDesktopChegg>a.exe -a 2 6 8 4 9 10 7 3 11 5
6.5


C:UserslmaliDesktopChegg>a.exe -m 2 6 8 4 9 10 7 3 11 5
7

C:UserslmaliDesktopChegg>a.exe -d 2 6 8 4 9 10 7 3 11 5
Invalid option

C:UserslmaliDesktopChegg>a.exe
usage: ./a.out -option (a or m) followed by 10 numbers

Thanks a lot. Please let me know if you have any doubt.