Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that runs on ocelot for a mini calculator using only the comma

ID: 3746122 • Letter: W

Question

Write a C program that runs on ocelot for a mini calculator using only the command line options. You must use getopt to parse the command line. The calculator will only do addition, multiplication and a power of 2. Usage: minicalc [-a num] [-m num] [-x] value The variable value is the starting value. Value should be validated to be an integer between 1 and 50 inclusive. Error message and usage shown if not. . For the -m option num should be a positive integer between 1 and 10 inclusive. . For the -a option num should be a positive integer between 1 and 500 inclusive. . a adds num to value. . -m multiplies value by num. .x squares value. (Note: no num is needed.) .Output should have exactly 2 decimal places no matter what the starting values are. . If-x is included, it is executed first. If-m is included it would be next. The-a would be executed last. There will be at most one of each option, if there are more than one you can use either of the options in the calculation. There should be no user input while the program is running. It runs in full from the command . line.

Explanation / Answer

PROGRAM

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

// Create command line arguments

int main(int argc,char *argv[])

{

int x1,x2,x3,x4; // declare integer variables

// convert string to integer values using atoi()

x1=atoi(argv[2]);

x2=atoi(argv[4]);

x3=atoi(argv[6]);

// check number of arguments in command line

if(argc<=5)

{

printf("Error: Invalid Arugments");

return 0;

}

else

if(x3>=1&&x3<=50) // check condition the value should be 1 to 50

{

if(strcmp(argv[1],"-a")==0) // check condition addition argument -a

{

if(x1>=1&&x1<=500) // check number should be 1 to 500

printf(" Sum of %d and %d is = %d",x1,x3,x1+x3); // calculate addition and print

else

printf(" Error: Addition should be done Number should be 1 to 500 only"); // display error message

}

if(strcmp(argv[3],"-m")==0) // check condition multiplication argument -m

{

if(x2>=1&&x2<=10) // check number should be 1 to 10

printf(" Multiplication of %d and %d is = %d",x2,x3,x2*x3); // calculate multiplication and print

else

printf(" Error: Mulpltiplication should be done Number should be 1 to 10 only"); // else display error message

}

if(strcmp(argv[5],"-x")==0) // check condition power argument -x

{

printf(" Square of %d is = %.2f",x3,(float)x3*x3); // calculate power and print in decimal value

}

}

else

printf("Error: Value in between 1 to 50 only"); // display

return 0;

}

OUTPUT

F:>minicalc -a 50 -m 10 -x 5

Sum of 50 and 5 is = 55
Multiplication of 10 and 5 is = 50
Square of 5 is = 25.00


F:>minicalc -a 50 -m 10 -x 51
Error: Value in between 1 to 50 only


F:>minicalc -a 50 -m 11 -x 5

Sum of 50 and 5 is = 55
Error: Mulpltiplication should be done Number should be 1 to 10 only
Square of 5 is = 25.00


F:>minicalc -a 501 -m 6 -x 5

Error: Addition should be done Number should be 1 to 500 only
Multiplication of 6 and 5 is = 30
Square of 5 is = 25.00


F:>minicalc -a 50 -m 6 -x 5

Sum of 50 and 5 is = 55
Multiplication of 6 and 5 is = 30
Square of 5 is = 25.00

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote