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

Rewrite the program below, but use arithmetic functions - add, subtract, multipl

ID: 3568939 • Letter: R

Question

Rewrite the program below, but use arithmetic functions - add, subtract, multiply, division functions. Use a pointer to function concept to execute those arithmetic functions. You should never call those functions directly.

In other words, when your program prompted the user

what is 3 + 4 ?

This interactions should done in the add function. The add function should be called using a pointer. And the print report function you wrote should now be written to a file - report.txt and should be written to the output terminal.

=====================================================================================

#include <stdio.h>

#define MAX_INPUT 60
#define MAX_RESPONSES 20


int PromptQuestions(char operator, char operand1, char operand2);
void PrintReport(char input[MAX_INPUT], int responses[MAX_RESPONSES]);


int main()
{
char input[MAX_INPUT] = {'+', 5, 2, '-', 4, 2, '*', 4, 2, '/', 6, 3, '+', 3, 4,
    '*', 4, 2, '/', 8, 2, '-', 7, 2, '*', 3, -5, '+', 3, -5,
    '*', 5, 2, '/', 14, 2, '/', 4, 2, '*', 6, 3, '-', 3, 4,
    '+', 4, 2, '-', 8, 2, '*', 7, 2, '-', 3, -5, '/', 30, -5};

int responses[MAX_RESPONSES];

int r = 0;

for(int i = 0; i < MAX_INPUT; i += 3)
{
  char ch1 = input[i];
  char ch2 = input[i + 1];
  char ch3 = input[i + 2];

  int resp = PromptQuestions(ch1, ch2, ch3);

  responses[r] = resp;
  r++;
}

PrintReport(input, responses);

return 0;
}


int PromptQuestions(char operator, char operand1, char operand2)
{
int res;

switch(operator)
{
  case '+':
   printf("What is %d + %d? ", operand1, operand2);
   break;

  case '-':
   printf("What is %d - %d? ", operand1, operand2);
   break;

  case '*':
   printf("What is %d * %d? ", operand1, operand2);
   break;

  default: // case '/':
   printf("What is %d / %d? ", operand1, operand2);
}

scanf("%d", &res);   
return res;
}


void PrintReport(char input[MAX_INPUT], int responses[MAX_RESPONSES])
{
printf(" Welcome to Standardized test ");

for(int i = 0, r = 0; i < MAX_INPUT; i += 3, r++)
{
  char ch1 = input[i];
  char ch2 = input[i + 1];
  char ch3 = input[i + 2];

  int result;

  switch(ch1)
  {
   case '+':
    printf("What is %d + %d? ", ch2, ch3);
    result = ch2 + ch3;
    break;

   case '-':
    printf("What is %d - %d? ", ch2, ch3);
    result = ch2 - ch3;
    break;

   case '*':
    printf("What is %d * %d? ", ch2, ch3);
    result = ch2 * ch3;
    break;

   default: // case '/':
    printf("What is %d / %d? ", ch2, ch3);
    result = ch2 / ch3;
  }
  
  if(responses[r] == result)
  {
   printf("Your response is %d. It is correct. ", responses[r]);
  }
  else
  {
   printf("Your response is %d. It is incorrect. The correct answer is %d. ", responses[r], result);
  }
}
}

=====================================================================================

The previous instruction for the above program was:

A Program to test the mathematics for three grade students

The students are tested on Addition, Subtraction, Product, division. The student is given 20 questions, one at a time. The responses are stored. After the 20 questions are done, the student is given the summary of correct answers and incorrect answers. The report also contains the questions followed by the correct answer and the response given by the student.

For instance: The sample output of your program will be

Welcome to Standardized test.

Question 1: What is 3 + 4 ?

Question 2: What is 8 over 2

........

Question 20: What is 7 minus 2 ?

             5

Thank you. You have finished taking the test. Here is the report

You have answered 17 correctly out of 20 question.

Here are the individual questions and your responses.

Question 1: What is 3 + 4 ?

Your response 7 is Correct

Question 2: What is 8 over 2

Your response was 4 is Incorrect.

Question 3: What is 4 * 2 ?

Your response 2. It is Incorrect. The correct answer is 8.

Explanation / Answer

#include <stdio.h>

#define MAX_INPUT 60
#define MAX_RESPONSES 20


int PromptQuestions(char operator, char operand1, char operand2);
void PrintReport(char input[MAX_INPUT], int responses[MAX_RESPONSES]);

int add(int n, int m) {
return n+m;
}

int subtract(int n, int m) {
return n-m;
}

int multiply(int n, int m) {
return n*m;
}

int divide(int n, int m) {
return n/m;
}

int (*functionPtr)(int,int);

int main()
{
char input[MAX_INPUT] = {'+', 5, 2, '-', 4, 2, '*', 4, 2, '/', 6, 3, '+', 3, 4,
'*', 4, 2, '/', 8, 2, '-', 7, 2, '*', 3, -5, '+', 3, -5,
'*', 5, 2, '/', 14, 2, '/', 4, 2, '*', 6, 3, '-', 3, 4,
'+', 4, 2, '-', 8, 2, '*', 7, 2, '-', 3, -5, '/', 30, -5};

int responses[MAX_RESPONSES];

int r = 0, i;

for( i = 0; i < MAX_INPUT; i += 3)
{
char ch1 = input[i];
char ch2 = input[i + 1];
char ch3 = input[i + 2];

int resp = PromptQuestions(ch1, ch2, ch3);

responses[r] = resp;
r++;
}

PrintReport(input, responses);

return 0;
}


int PromptQuestions(char operator, char operand1, char operand2)
{
int res;

switch(operator)
{
case '+':
printf("What is %d + %d? ", operand1, operand2);
break;

case '-':
printf("What is %d - %d? ", operand1, operand2);
break;

case '*':
printf("What is %d * %d? ", operand1, operand2);
break;

default: // case '/':
printf("What is %d / %d? ", operand1, operand2);
}

scanf("%d", &res);   
return res;
}


void PrintReport(char input[MAX_INPUT], int responses[MAX_RESPONSES])
{
printf(" Welcome to Standardized test ");
int i,r;

FILE *fp=fopen("report.txt", "w");


for(i = 0, r = 0; i < MAX_INPUT; i += 3, r++)
{
char ch1 = input[i];
char ch2 = input[i + 1];
char ch3 = input[i + 2];

int result;

switch(ch1)
{
case '+':
printf("What is %d + %d? ", ch2, ch3);
fprintf(fp,"What is %d + %d? ", ch2, ch3);
   functionPtr = &add;
result = (*functionPtr)(ch2, ch3);
break;

case '-':
printf("What is %d - %d? ", ch2, ch3);
fprintf(fp, "What is %d - %d? ", ch2, ch3);
functionPtr = &subtract;
result = (*functionPtr)(ch2, ch3);
break;

case '*':
printf("What is %d * %d? ", ch2, ch3);
   fprintf(fp, "What is %d * %d? ", ch2, ch3);
functionPtr = &multiply;
result = (*functionPtr)(ch2, ch3);
break;

default: // case '/':
printf("What is %d / %d? ", ch2, ch3);
   fprintf(fp,"What is %d / %d? ", ch2, ch3);
functionPtr = &divide;
result = (*functionPtr)(ch2, ch3);
}
  
if(responses[r] == result)
{
printf("Your response is %d. It is correct. ", responses[r]);
   fprintf(fp, "Your response is %d. It is correct. ", responses[r]);
}
else
{
printf("Your response is %d. It is incorrect. The correct answer is %d. ", responses[r], result);
fprintf(fp,"Your response is %d. It is incorrect. The correct answer is %d. ", responses[r], 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