I have to find the average of double numbers that the user inputs at the command line, so my program should work with any amount of inputs. I understand that I have to find the sum of all of the numbers and then divide by the number of inputs.
I was thinking, to find the number of inputs, I would use sscanf to read "char *num" in my argument list. Because sscanf returns the characters read. And then I was thinking of using that to divide the sum by to get the average. But I'm having trouble with my code. :(
#include <stdio.h>
void average(int arg_list, char *num[])
{
int x;
double sum, average;
x = sscanf(num, "%s\n", &x);
for (int i = 0; i != '\0'; i++)
{
sum = sum + num[i];
}
average = sum/x;
printf("%lf\n", average);;
}
int main(int argc, char *argv[])
{
if (argc == 0)
{
perror("Error!\n");
}
average(argc, argv);
}
Specifically, when I try to compile my program, the compiler complains about the "sscanf" and the sum. :/