Question: A simple progamin in C has the current specs. - It will take whole numbers as command line arguments. - It will add them up
A simple progamin in C has the current specs.
- 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.
Notes: i) your program must not leak memory or perform any invalid memory accesses. ii) your program should not use more memory than it needs. iii) do not call fgets or getline
Example of code in shell:
eg: ./numbers -2 4 1 Would output: 3 4
I have written the code below, but am struggling to work out how to error check a float input as per the spec. If a float(i.e. not a whole nuber) is input and needs to be registered as a 0, how would it be coded to get it functioning?
#include
#include
#include
int sumnum (int a, int b, int c) {
int sum;
sum = a + b + c;
return sum;
}
int numCheck (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;
}
int main(int argc, const char * argv[]) {
int a = -2.2; // hypothetical for argv[1] for sake of testing
int b = 4; //hypothetical for argv[2] for sake of testing
int c = 1; //hypothetical for argv[3] for sake of testing
int total;
int highNum;
// if (argc != 3) to check that correct number of arguments is entered
// return 1;
if ((argv[1] % 1) == 0) { // would something along these lines work??
a = argv[1];
} else {
a = 0;
}
total = sumnum(a, b ,c);
printf("%d %d %d ", a, b, c);
highNum = numCheck(a, b, c);
printf("%d %d ", total, highNum);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
