Question: The Exercise: Write the following program (saved as sumex.c). It must compile successfully using: gcc --std=c99 sumex.c -o sumex It will take whole numbers as

The Exercise:

Write the following program (saved as sumex.c). It must compile successfully using: gcc --std=c99 sumex.c -o sumex It will take whole numbers as command line arguments. It will add them up and print out the total and maximum values (space separated). It should return 0 on success and 1 if not enough arguments are given. If an argument is not a whole number, then treat it as 0. eg: ./sumex -2 4 1 Would output: 3 4

I have the following program which doesn't work. Please help as I do not know where I have gone wrong.

#include #include

/* return sum of numbers */

int total(int a, int b, int c) { int sum; sum = a + b + c; return sum; }

/* checks maximum of numbers */ int max(int a, int b, int c) { if (a > b && a > c) { return a; } else if (b > a && b > c) { return b; } else { return c; } return 0; }

/* taking argument as command line */ int main(int argc, const char* argv[]) { int a, b, c;

int sumnum; int maximum;

/* convert a1, b1, c1 to integer */

int a1 = atoi(argv[1]);

int b1 = atoi(argv[2]);

int c1 = atoi(argv[3]);

/* checking whether the number of arguments passed * is less than 4 (including argv[0]) */

if (argc

sumnum = total(a, b, c); maximum = max(a, b, c); /* print out the sum of numbers and max number * return 0 if successful */ printf("%d %d ", sumnum, maximum); return 0; }

OUTPUT:

The Exercise: Write the following program (saved as sumex.c). It must compile

S gccstd c99 sumex.c -o sumex $./sumex 3 4 5 1237059664 4195440

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!