Question: How to apply these instructions to the code below with the same menu options?( use Arrays with For loop) *Create at least one fixed array
How to apply these instructions to the code below with the same menu options?( use Arrays with For loop)
*Create at least one fixed array declared at the top of main() with a size of three to hold the numbers inputted by the user
*Nest for loops into your code to process the array appropriately, for example: If the user selects from the menu to add the numbers, the numbers in the array get added together, e.g., 1.5 + 2 + -3 = 0.5
*Then, the numbers and the result will get displayed to the user based on the following format:
Answer: 1.500 + 2.000 + -3.000 = 0.500
*If the user enters: 1 0 3, and selects division from the menu, the array should only be processed up to 0
The Code :
int main() {
// Constants and Variables
double num1 = 0.0, num2 = 0.0;
char option = '\0';
int COUNT = 2;
// Input Two Numbers
printf("Enter two numbers separated by a space:");
scanf_s("%lf %lf", &num1, &num2);
// Output calculator menu options
printf(" Calculator Menu: ");
printf("(A)dd. ");
printf("(S)ubstract. ");
printf("(M)ultiply. ");
printf("(D)ivide. ");
printf("a(V)erage. ");
printf("(L)argest number ");
printf("D(I)splay if each number is negative, positive, or zero ");
printf("e(x)it ");
printf("Choice:");
// Input Choice
scanf_s(" %c ", &option);
// Menu Processing
switch (option) {
// Calculation - Addition
case 'A':
case 'a':
// Output - Addition
printf(" The sum is: %0.3f ", num1 + num2);
break;
// Calculation - Subtraction
case 'S':
case 's':
// output - Subtraction
printf("The substraction is: %0.3f ", num1 - num2);
break;
// Calculation - Multiplication
case 'M':
case 'm':
// Output - Multiplication
printf("The multiplication is: %0.3f ", num1 * num2);
break;
// Calculating - Division
case 'D':
case 'd':
if (num2 == 0) {
// Output Error Messege
printf("Error: Cannot divide by zero! ");
} else {
// Output - Division
printf("The division is: %0.3f ", num1 / num2);
}
break;
// Calculating The Average
case 'V':
case 'v':
// Output - The Average
printf("The average is %0.3f ", (num1 + num2) / COUNT);
break;
// Determining The Largest Number
case 'L':
case 'l':
if (num1 > num2) {
// Output The Largest Number
printf("The largest number is %0.3f ", num1);
} else {
printf("The largest number is %0.3f ", num2);
}
break;
case 'I':
case 'i':
//Determining If The Numbers Are Positive or Negative
if (num1 > 0 && num2 > 0) {
printf("Numbers are positive ");
} else if (num1 < 0 && num2 < 0) {
printf("Numbers are negative ");
} else if (num1 > 0 && num2 < 0) {
printf("First Number is positive and Second Number is Negative ");
} else if (num1 < 0 && num2 > 0) {
printf("The First Number is Negative and Second Number is Positive ");
} else if (num1 == 0 && num2 > 0) {
printf("The First Number is Zero and The Second Number is Positive ");
} else if (num1 == 0 && num2 < 0) {
printf("The First Number is Zero and The Second Number is Negative ");
} else if (num1 > 0 && num2 == 0) {
printf("The First Number is Positive and The Second Number is Zero ");
} else if (num1 < 0 && num2 == 0) {
printf("The First Number is Negative and The Second Number is Zero ");
} else {
printf("Both Numbers Are Zero ");
}
break;
// Exit
case 'X':
case 'x':
break;
default:
printf("Error: Invalid choice! ");
}
printf("Thank you for using simple calculator! Good-bye! ");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
