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

Program :

#include <stdio.h>
int count, sum = 0; // variable declaration
float avg=0.0;   
void average(int *, int *); // function definition
void main(int argc, char *argv[])
{
int i, num[argc];
count = argc;
for (i = 1; i < argc; i++) // Loop runs from 1 through argc
{
num[i - 1] = atoi(argv[i]);
}
average(num, num + 1); // calling function
avg=(float)sum/count; // calculate averge
printf(\"The average of these %d numbers is %.3f\ \",count,avg); // print the average
}

/* computes sum of two numbers recursively */
void average(int *var1, int *var2) // called function
{
if (count == 1) // if the count is 1
return;
sum = sum + *var1 + *var2; // calculate sum
count -= 2; // count reduce by 2
average(var1 + 2, var2 + 2); // calling function
}

Output :

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