Assignment Write a C program that performs the five basic integer arithmetic ope
ID: 3629903 • Letter: A
Question
AssignmentWrite a C program that performs the five basic integer arithmetic operations on two input numbers. The program should begin by accepting two integer inputs with values on the closed interval [-1000, 1000]. This restraint should be enforced by performing the appropriate bounds checking. The program should then present a menu that allows the user to select an integer operation (addition, subtraction, multiplication, division or modulus). Again, appropriate bounds checking should be performed on this menu selection. The program performs the indicated operation on the two input numbers and displays the result in an attractive display.
Use the following set of data when creating the three screenshots submitted with the program:
Operation
Operand #1
Operand #2
Trial #1
Division
10
3
Trial #2
Modulus
-23
4
Trial #3
Subtraction
999
-1024
Your program must include all of the following aspects:
1.) at least one if, if-else, or if-else chain flow control structure
2.) at least one switch-case flow control structure
3.) at least one example of integer bounds checking
4.) at least one example of explicit type-casting
5.) at least one example of handling the phantom enter key
Your program must conform to the following restrictions:
1.) You may only use the printf() and scanf() functions for input and output.
2.) You must include at least one example of accepting integer input and at least one example of accepting character input (use both the %d and %c conversion characters with scanf() ).
3.) When invalid input is supplied to the program, it should exit gracefully (via a return statement).
Additional Notes:
1.) You may assume that the user will type in integer values when prompted to do so. You may not make any assumptions about the values of the integers the user types in.
2.) You should include logic that handles special cases. For example, division by the integer “0” is not allowed in mathematics. Your program should account for this.
3.) Although the inputs to the programs are integers, the result of the operation may contain a decimal value (for example 9/2 = 4.5). Your program should display decimal answers with 4 digits following the decimal point.
QUESTIONS
1.) Use your program to determine the rules about the modulus operator and signs. Obviously, the modulus operation is allowed on both positive and negative integers. How does the computer determine the sign of the answer to a modulus expression based on the signs of the operands? Based on your discovery, evaluate the following expressions:
a.) 9 % 2 =
b.) -9 % 2 =
c.) 9 % -2 =
d.) -9 % -2 =
Place your answers at the conclusion of your program's course code in a multiple line comment.
Explanation / Answer
#include <stdio.h>
int addition(int x, int y);
int modulus(int x, int y);
double division(int x, int y);
int subtraction(int x, int y);
int multiplication(int x, int y);
bool checkNum(int num);
int main(int argc)
{
bool isTrue;
int num1=0;
int num2=0;
double ans=0;
printf("Enter 2 numbers between -1000 and 1000: ");
scanf("%i",&num1);
scanf("%i",&num2);
if(!checkNum(num1)||!checkNum(num2)){
return 0;
}
printf(" Select an operation for %d and %d (case sensitive): A = Addition S = Subtraction 3.M = Multiplication D = Division O = Modulus ",num1,num2);
char str;
const char EOL = ' ';
scanf("%c",&str);
while (str==EOL){
scanf("%c",&str);
}
printf("%c",str);
switch(str){
case 'A':
ans=(double)addition(num1,num2);
if(checkNum(ans)){
printf(" %i + %i = %i",num1,num2,ans);
} else {
printf(" The addition of %i and %i is out of range.",num1,num2);
return 0;
}
break;
case 'D':
if (num2!=0){
ans=(double)division(num1,num2);
if(checkNum(ans)){
printf(" %i / %i = %.4f",num1,num2,ans);
} else {
printf(" The division of %i and %i is out of range.",num1,num2);
return 0;
}
} else{
printf("Cannot divide by 0!");
}
break;
case 'S':
ans=(double)subtraction(num1,num2);
if(checkNum(ans)){
printf(" %i - %i = %i",num1,num2,ans);
} else {
printf(" The addition of %i and %i is out of range.",num1,num2);
return 0;
}
break;
case 'M':
ans=(double)multiplication(num1,num2);
if(checkNum(ans)){
printf(" %i * %i = %i",num1,num2,ans);
} else {
printf(" The multiplication of %i and %i is out of range.",num1,num2);
return 0;
}
break;
case 'O':
ans=(double)modulus(num1,num2);
printf(" %i mod %i = %i",num1,num2,ans);
break;
default:
printf(" You have entered and incorrect letter.");
break;
}
return 0;
}
bool checkNum(int num){
if(num>1000 || num<-1000 ){
printf("%i is out of the range -1000 to 1000!",num);
return false;
} else {
return true;
}
}
int addition(int x, int y){
return (x+y);
}
int modulus(int x, int y){
return (x%y);
}
double division(int x, int y){
return ((double)x)/((double)y);
}
int subtraction(int x, int y){
return (x-y);
}
int multiplication(int x, int y){
return (x*y);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.