Question: C Programming Homework Question 1. The task is to demonstrate the knowledge of using conditional statements and loops. The following incomplete command line-based program was

C Programming Homework

Question 1.

The task is to demonstrate the knowledge of using conditional statements and loops.

The following incomplete command line-based program was written to mimic a simple calculator.

At the current state, the program reads the first number (operand-1) digit by digit and allows the user to select the type of operation.

#include

int main() {

int count1=0, value=0, number1=0, operation=0;

printf("Enter the first operand, one digit at a time ... or enter -1 to stop "); scanf("%d", &value);

while(value != -1) {

number1=number1*10+value; // new digit is added to the existing operand count1+=1;

printf("Enter the next digit of first number or 0-1 to stop ... "); scanf("%d", &value);

} printf("the first operand = %d ",number1);

//code for the selection of operation printf("Enter the number corresponding to the operation type ... ");

printf("1 printf("2 printf("3 printf("4 scanf("%d",&operation);

Addition "); Subtraction "); Multiplication "); Division ");

return 0; }

The task is to complete the above code by adding steps to read the second operand. Then implement the code to calculate the answer based on the selected operation (Addition, Subtraction, Multiplication or Division).

Question 2.

The task is to demonstrate the knowledge of creating a user-defined function and passing arguments to a function.

a. This task is an extension of task 6.1. Your task is to implement user-defined functions to the Task 6.1 code. Using the following function prototype, shift the number reading section of the code from the main to the function.

Hint: You might have to use global variables to complete this step. Function prototype:

void read_number()

{

} Reuse the function to read number-2.

b. Using the following function prototype, shift the operator selection section of the 6.1 code to the function.

Hint: You might have to use global variables to complete this step

Function prototype:

void read_symbol()

{

}

c. Combine the functions developed in tasks 6.2.a & 6.2.b with main(), complete the implementation of the calculator program.

Hint: You might have to use global variables to complete this step.

submit a working code and screenshots of output.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!