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

Write a four function calculator (+,-,*,/) that works with an arbitrary number o

ID: 3797228 • Letter: W

Question

Write a four function calculator (+,-,*,/) that works with an arbitrary number of inputs. This means that instead of just taking two inputs it takes numbers and operations until the user decides to quit (the program runs in an infinite loop). Give updates on the current total after each operation and allow the user the choice to continue or choose a new operation each time. Total starts at 0.0 and the menu choice 5 is used to signal end of computation. Place the add, subtract, multiply, and divide functionality inside independent functions. Implement these functions:

This is my code so far in C. However, what can I add, fix, remove inorder to get it to compile like the image.

The function prototypes have to remain as they are!   

#include<stdio.h>
#include<math.h>
#include<stdlib.h>

// Function Prototypes

double add(double sum ,double value);
double subtract(double sum ,double value);
double multiply(double sum ,double value);
double divide(double sum ,double value);

int main(void) {
  
   int n;
   char ch;
   double sum;
   double value;
  
while(1)
{
printf("1: add ");
   printf("2: subtract ");
   printf("3: multiply ");
   printf("4: divide ");
   printf("5: quit ");
   printf("Enter operation choice ");
   scanf("%d", &n);

//printf("enter the value:");
   //scanf("%lf",&value);
switch(n)
{
case 1:
   add(sum,value);
   printf("enter number to add: ");
   scanf("%lf", &value);
  
      break;

case 2:
    subtract(sum,value);
    break;

case 3:
    multiply(sum,value);
   break;

case 4:
    divide(sum,value);
    break;

case 5:
    exit(1);
}
  
}
return 0;
}

double add(double sum,double value)
{
  
sum = sum + value;
//printf("the result: %d",sum);
return sum;

}
double subtract(double sum,double value)
{

sum = sum - value;
// printf("the result: %d",sum);
   return sum;
}
double multiply(double sum,double value)
{

sum = sum * value;
//printf("the result: %d",sum);
return sum;
}
double divide(double sum,double value)
{
   if(value == 0) {
       printf("zero not appilcable");
   }
   else
   {
        sum = sum / value;
        //printf("the result: %d",sum);
       return sum;
   }
}

1 add 2: subtract 3: multiply 4 divide 5: quit enter number to add: Total is: 5.000000 1 add 2: subtract 3: multiply 4 divide 5: quit enter number to multiply Total is 25.000000 1 add 2: subtract 3: multiply 4 divide 5: quit enter number to divide can not divide by 0, ignoring value and continuing Total is 25.000000 1 add 2: subtract 3: multiply 4 divide 5: quit Final total is: 25.000000

Explanation / Answer

#include <stdio.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
// Function Prototypes
double add(double sum ,double value);
double subtract(double sum ,double value);
double multiply(double sum ,double value);
double divide(double sum ,double value);
double Totalsum=0;
int main() {

int n;


double value;
double t;// variable to hold the return value from function
while(1)
{
printf("1: add ");
printf("2: subtract ");
printf("3: multiply ");
printf("4: divide ");
printf("5: quit ");

printf("Enter operation choice ");
scanf("%d", &n);


switch(n)
{
case 1:

printf("enter number to add: ");
scanf("%lf", &value);
t= add(Totalsum,value);
printf("Total Is:%lf",t);
printf(" ");

break;

case 2:
printf("enter number to substract: ");
scanf("%lf", &value);
t= subtract(Totalsum,value);
printf("Total Is:%lf",t);
printf(" ");
break;

case 3:
printf("enter number to Multiply: ");
scanf("%lf", &value);
t= multiply(Totalsum,value);
printf("Total Is:%lf",t);
printf(" ");
break;

case 4:
printf("enter number to Divide: ");
scanf("%lf", &value);
t= divide(Totalsum,value);
printf("Total Is:%lf",t);
printf(" ");
break;

case 5:
exit(1);
}

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