In: Computer Science
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.
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 :