Question

C Programming Homework Question 1. The task is to demonstrate the knowledge of using conditional statements...

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 <stdio.h>

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 \n"); 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 ...\n"); scanf("%d", &value);

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

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

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

Addition \n"); Subtraction \n"); Multiplication \n"); Division \n");

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here the code and screenshot of the running program is given below:::

all the input are given in the same format as described in the problem statement.

#include<stdio.h>
//function to read the number digit by digit
int choice;//used for what type of operation it is
int first_num,second_num;//used as global variable for the operation
int read_number(){
   int digit,number=0;
   printf("Enter the operand, one digit at a time ... or enter -1 to stop \n");
   scanf("%d",&digit);
   while(digit!=-1){
       number=number*10+digit;
       printf("Enter the next digit of number or 0-1 to stop ...\n");
       scanf("%d",&digit);
   }
   return number;
}
void read_symbol(){
   printf("1.addition\n 2.subtraction\n 3.multiplication\n 4.division\n");
   printf("Enter the number corresponding to the operation\n");
   scanf("%d",&choice);
}
void calculate(){
   printf("your answer for the corresponding operation is: ");
   switch(choice){
       case 1:printf("%d\n",first_num+second_num);break;
       case 2:printf("%d\n",first_num-second_num);break;
       case 3:printf("%lld\n",first_num*second_num);break;//here result is displayed in long long int because result of the multiplication might exceed the range of the int range.
       case 4:printf("%f\n",(float)first_num/second_num);break;//here type conversion is used because division may be in float
   }
}
int main(){
   first_num=read_number();
   second_num=read_number();
   read_symbol();
   calculate();
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C Programming Homework Question 1. The task is to demonstrate the knowledge of using conditional statements...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS...

    Programming Assignment #2 EE 2372 MAKE SURE TO FOLLOW INSTRUCTIONS CAREFULLY, IF YOU HAVE ANY DOUBTS PLEASE EMAIL ME! This programming assignment will just be a small extension to programming assignment 1. In the first assignment each of the functions could only take a predefined number of inputs. This time the objective is to be able to complete these functions with any amount of inputs. The user will pass arguments through the command line (this means you will have to...

  • Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int...

    Solve using C programming 3. lf you are given this code. #include <stdio.h> int main() int var1, var2; int sum; printf("Enter number 1:\n "); scanf("%d",&var1); printf("Enter number 2:In ); scanf("%d",&var2); sum-var1+var2; printf ("Vnsum of two entered numbers : %d", //printf ("Output: %d", res); sum); return e; Modify this code by creating a function called "addition". Make the arguments of the functions numberl and number 2. Add the two values in the function. Return a value called "result". Print "result" in...

  • ***In C Programming*** TASK 1: Complete itinerary.h Create a struct Destination that represents a node in...

    ***In C Programming*** TASK 1: Complete itinerary.h Create a struct Destination that represents a node in a liked list. It contains the three letter airport code and a pointer to the next node int the linked list.  You will need to write this struct. This file also includes the prototypes. They have all been written for you. There is no need to add the function comments for the prototypes. TASK 2: Write all functions in itinerary.c Write the following functions, which...

  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • Hello, I need help with the following C code. This program asks the user to enter...

    Hello, I need help with the following C code. This program asks the user to enter three numbers and outputs the sum on the screen , all im trying to do is have the program reprompt the user for a number if anything other than a number is entered for each of the entries. I wanted to use do while for each of the functions but that did not work for example: Enter Number 1: 2 Enter Number 2: Hello...

  • Please help me with this question (in C programming language and Unix) computer system book 3 edi...

    please help me with this question (in C programming language and Unix) computer system book 3 edition int *fun (int *x)f int value *xtt return &value; int main) int pl; int **p2; int a; char str [5] printf ("Enter a number:") scanf("%d", a) ; printf ("Enter a line:") gets (str); p1-malloc(sizeof (int)) *pl-p2; p2-malloc(sizeof (int)); pl-fun (a); n-0; for (pl-stripl<-str+5;pl++) if (isdigit (pl)) at+; return 0; Construct a table that shows for each line of code, what may be the...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...

    Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) {    print_table(user_interface());    return 0 ; } int user_interface(int val){    int input = 0;    printf("This function takes in x and returns an output\n");    printf("Enter Maximum Number of X:");    scanf("%d", &input);...

  • In C code 1. Write and submit the algorithm OR flowchart to indicate you understand the...

    In C code 1. Write and submit the algorithm OR flowchart to indicate you understand the problem 2. Create a project and name the source code lastname_firstname_prog5.c 3. Use the prog5.c as a guide, copy/ paste into your project source code *** build run and test, the outline code - You will need to declare an integer variable called again and initialize it 4. Add the function prototype and implement the function definition for the Greeting function 5. Add the...

  • C program help: Write a C program that uses three functions to perform the following tasks:...

    C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () {    int numDucks,numCats;    getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT