Name this program averagec The program looks at all the com

Name this program average.c – The program looks at all the command-line arguments, which all represent valid integers, and prints the average of all values seen on the command line. There will always be at least one integer entered on the command line. Sample executions are shown below:

Solution

average.c:

#include<stdio.h>

void main(int argc, char * argv[]) {
int i,sum = 0;
float average;

if (argc < 2) {
printf(\"You didn\'t enter any number.\");
exit(1);
}

printf(\"The average of these %d numbers is: \",argc-1);

for (i = 1; i < argc; i++)
sum = sum + atoi(argv[i]);

average=(float) sum/(argc-1);
printf(\"%f\", average);

}


Sample run:
./a.out 10 20 30 25 15 5 20 40
The average of these 8 numbers is: 20.625000

./a.out 99 55 11 -6 600 32
The average of these 6 numbers is 131.833328

./a.out -5
The average of these 6 numbers is -5.000000

Name this program average.c – The program looks at all the command-line arguments, which all represent valid integers, and prints the average of all values seen

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site