Question

In: Computer Science

Design a program that will ask the user to input two integer numbers and then perform...

Design a program that will ask the user to input two integer numbers and then perform the basic arithmetic operations such as addition and subtraction. Each calculation is done by a separate function. The main function gets the input from the user, then calls the addition function and the subtraction function one at a time to perform the calculations. Each calculation function (addition or subtraction function) performs an arithmetic operation and then returns the calculation results back to where it was called. The main function then calls the display function to display the results for the user on the screen. Note, the display function is already developed for you. For each set of numbers the user entered, the program produces the sum and the difference of the values. You may assume the difference is calculated by the first number subtracts the second number that the result may yield a negative number.

Draw an IPO chart and a Pseudocode program for each function . Besides, apply all the techniques you've learned, be sure to document your programs and apply good programming styles.

I want to ask if I need to declare additional variables to hold the arguments being sent to the display function.

Function void main()

// Declare the Variables.

  

Declare integer num1, num2, total, total2

  

// Display Greeting message.

  

Display "Input two integer numbers."

Display "And I will tell you the sum and difference of the two numbers"

// Prompt User for the first number.

  

Display "Enter the first number: "

Input num1

  

// Prompt User for the second number.

  

Display "Enter the second number: "

input num2

  

// Get the sum of both numbers.

  

Set total = addition(num1, num2)

  

// Get the difference of both numbers.

  

Set total2 = subtraction(num1, num2)

// Send the results to the display function.

  

Set total = display() ---- I don't know if i need a variable in here?

Set total2 = display() ---- I don't know if I need a variable in here?

  

End Function

Function void display(Integer arg1, Integer arg2)

  // Display the sum.

  Display "The sum is",arg1

  

  Display the difference.

  Display "The difference is",arg2

End Function

P.S: I have already done the addition and subtraction modules.

Solutions

Expert Solution

After calculating sum and Difference call display function by passing total and total2 as function argument no need to declare any additional variable to hold argument of display function.

please refer to the corrected Pseudo code below :

Function void main()

// Declare the Variables.

  

Declare integer num1, num2, total, total2

  

// Display Greeting message.

  

Display "Input two integer numbers."

Display "And I will tell you the sum and difference of the two numbers"

// Prompt User for the first number.

  

Display "Enter the first number: "

Input num1

  

// Prompt User for the second number.

  

Display "Enter the second number: "

input num2

  

// Get the sum of both numbers.

  

Set total = addition(num1, num2)

  

// Get the difference of both numbers.

  

Set total2 = subtraction(num1, num2)

// Send the results to the display function.

display(total,total2)  // call to display function to print the sum and difference
  

End Function

Function void display(Integer arg1, Integer arg2)

  // Display the sum.

  Display "The sum is",arg1

  

  Display the difference.

  Display "The difference is",arg2

End Function

IPO Chart :

Below is the implementation of above Psedo code in C Language for your reference :

#include <stdio.h>

//addition function
int addition(int num1, int num2) {
    //Calculate and return sum back to where it was called.
    return num1 + num2;
}
// subtraction function
int subtraction(int num1, int num2) {
    //Calculate and return difference back to where it was called.
    return num1 - num2;
}

// display function
void display(int agr1,int agr2) {
    //Display the sum
    printf("The sum is %d",agr1);
    //one line break
    printf("\n");
    //Display the difference
    printf("The difference is %d",agr2);
}

int main()
{
    // Declare the Variables.
    int num1, num2, total, total2;
    
    // Display Greeting message.

  

   printf("Input two integer numbers.\n");

   printf("And I will tell you the sum and difference of the two numbers\n");
   
   // Prompt User for the first number.
   printf("Enter the first number: ");
   scanf("%d",&num1);
   
   // Prompt User for the second number.
   printf("Enter the first number: ");
   scanf("%d",&num2);
   
   // Get the sum of both numbers.
   total = addition(num1, num2);
   // Get the difference of both numbers.
   total2 = subtraction(num1, num2);
   
   //Call display function to display result.
   display(total,total2);

    return 0;
}

Sample Output :


Related Solutions

Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers...
IN C++ PLEASE Requirements Write a program that takes in user input of two integer numbers for height and width and uses a nested for loop to make a rectangle out of asterixes. The creation of the rectangle (i.e. the nested for loop) should occur in a void function that takes in 2 parameters, one for height and one for width. Make sure your couts match the sample output (copy and paste from those couts so you don't make a...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Question: Conversion Program * This program will ask the user to input meters * After input,...
Question: Conversion Program * This program will ask the user to input meters * After input, a menu will be displayed: * 1. Convert to kilometers * 2. Convert to inches * 3. Convert to feet * 4. Quit * * The user will select the conversion and the converted answer will be displayed. * The program should redisplay the menu for another selection. * Quit program when user selects 4. Hi, my instructor requirement fix the bug in the...
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
• Write a C++ program that asks the user to input two integer values, then calls...
• Write a C++ program that asks the user to input two integer values, then calls a void function "swap" to swap the values for the first and second variable. • As we mentioned before, in order to swap the valors of two variables, one can use the following: temp= variable1; variable1 = variable2; variable2 = temp; • Display the two variables before you call swap and after you call that function. Comment in code would be greatly appreciated to...
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
python: ask the user to input a sequence of positive numbers. the end of the sequence...
python: ask the user to input a sequence of positive numbers. the end of the sequence is determined when the user enters a negative number. print out the maximum number from the sequence. output: keep entering positive numbers. to quit, input a negative number. enter a number: 67 enter a number: 5 enter a number: 8 enter a number: -3 largest number entered: 67 (note: i do not want to ask the user how many numbers they will input)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT