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

I am trying to figure out a way to write a function in C where the user enters i

ID: 671749 • Letter: I

Question

I am trying to figure out a way to write a function in C where the user enters in an integer, then a operand (+,*,/, or -), then enters another integer.

It then needs to carryout the operation entered in

for example the user enters in 10 * 4

I understand how to take in the integeras and the character for the operand, I am not quite sure what the function should look like

Here is what I have so fare

int evaluate (int firstnumber, char operator, int secondnumber)

{

int result;

result = firstnumber operator secondnumber;

return result;

}

Explanation / Answer

int evaluate (int firstnumber, char operator, int secondnumber)
{
   int result;
   switch(operator)
   {
       case '+':

           result = firstnumber + secondnumber;
           break;
  
       case '*':

           result = firstnumber * secondnumber;
           break;

       case '-':

           result = firstnumber - secondnumber;
           break;

       case '/':

           result = firstnumber / secondnumber;
           break;
   }
   return result;

}

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