Write a program called add3.c that sums precisely 3 numerical arguments from the
ID: 3624340 • Letter: W
Question
Write a program called add3.c that sums precisely 3 numerical arguments from the command line:- each numerical arg may be positive (e.g. 1), negative (e.g. -1) or floating point (1.0 or -1.0)
-- there should be no more and no less than 3 numerical arguments
-- the sum of the 3 args is printed on stdout, and EXIT_SUCCESS is returned by the program
-- o you should use only integers to compute the sum: floating point numbers on the command line will be truncated
- if there are less than 3 args, or more than 3, the error message:
./add: incorrect number of args
o
is printed on stderr and the program returns EXIT_FAILURE
* otherwise, if one of the 3 args is non-numeric, the error message:
./add: found non-numerical arg
o
is printed on stderr and the program returns EXIT_FAILURE
* examples of usages that result in correct behaviour are the following:
./add3 1 2 3
6
./add3 9678 12505 342198
364381
./add3 0 0 0
0
./add3 -1 -2 -3
-6
./add3 1.0 2.0 3.0
6
./add3 -1.0 -2.0 3
0
./add3 1.9 1 1
3
* examples of usages that generate error messages are the following:
./add3
./add3: incorrect number of args
./add3 1
./add3: incorrect number of args
./add3 1 2
./add3: incorrect number of args
./add3 1 2 3 4
./add3: incorrect number of args
./add3 1 2 3 4 5
./add3: incorrect number of args
./add3 1 2 a
./add3: found non-numerical arg
./add3 1 a 2
./add3: found non-numerical arg
./add3 a 1 2
./add3: found non-numerical arg
./add3 a b c
./add3: found non-numerical arg
./add3 1 2 3 a
./add3: incorrect number of args
Explanation / Answer
#include #include int main(int argc, char *argv[]) { int ans = 0; // Start the answer at 0 int num; // Command line number int param; // Command line parameter index clrscr(); if (argc == 4) // need at least two parameters { // start after the program name to the last parameter for (param=1; paramRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.