Here is my current program that needs to be modified: #include<stdio.h> void men
ID: 3533165 • Letter: H
Question
Here is my current program that needs to be modified:
#include<stdio.h>
void menu();
void sumIntegers ();
void sumDoubles();
void calcFactorial ();
int main(){
char choice;
printf("Welcome To Assignment #2 ");
do{
menu();
scanf(" %c",&choice);
switch (choice){
case '1'://sum even int
printf(" I want to sum up some even integers. ");
sumIntegers();
break;
case '2'://sum $ amounts
printf(" I want to sum up dollar amounts. ");
sumDoubles();
break;
case '3'://factorial num
printf(" I want to calculate the n factorial. ");
calcFactorial();
break;
case 'Q':
printf("Thank you for playing! ");
break;
default:
printf("*** You entered an invalid choice!!! *** ");
}
}while(choice!='Q');
printf("Press any key to continue . . .");
scanf(" %c",&choice); //stall console from closing for 1 character read
return 0;
}
void menu(){
printf(" Please choose from the following options: ");
printf("1 - Sum of even integers ");
printf("2 - Sum of dollar amounts ");
printf("3 - Factorial of a number ");
printf(" ");
printf("Q - Quit ");
printf(" ");
printf("Enter your choice (1, 2, 3, Q):");
}
void sumIntegers (){
int n;
int sum=0,i;
do{
printf("Enter an integer: ");//Prompt the user to enter a positive integer.
scanf("%d",&n);//Get the integer value from the user.
if(n<0)//Check to make sure the value is positive.
printf(" *** Negative numbers are invalid! *** ");//If the value is negative, display an error message and repeat steps 1 through 3.
}while(n<0);
//Otherwise, add up all of the even values between 0 and the value that the user entered (including that number if it is also an even value).
for(i=0;i<=n;i+=2){
sum+=i;
}
//Print a message telling the user what this sum is.
printf(" The even numbers up to %d sum to %d. ",n,sum);
return;//Exit the function (return nothing)
}
void sumDoubles(){
// This function asks the user to enter a series of dollar amounts, displays their sum.
double sum=0;
double input;
do{
printf("Enter an dollar amount(-1 to exit): ");
scanf("%lf",&input);
if(input!=-1)//Check to make sure the value is positive.
sum+=input;
}while(input!=-1);
printf("The sum of the dollar amounts is $%.2lf",sum);
return;
}
void calcFactorial (){
//This function asks the user to enter a positive integer value,
//and calculates the factorial of that value.
int n,i;
int factorial=1;
do{
//Prompt the user to enter an integer value.
printf("Enter a postive integer");
//Get the integer value from the user.
scanf("%d",&n);
//Check to make sure the value is positive.
if(n<0)
printf(" *** Negative numbers are invalid! *** ");
}while(n<0);
//calculate the factorial of this value.
for(i=1;i<=n;i++){
factorial*=i;
}
//Print a message telling the user what the factorial result is.
printf("The factorial of 1 to %d is %d. ",n,factorial);
return;
}
This is what needs to be done to the program above:
I. Assignment Details:
A. Convert functions to use arrays (50 points)
For each function executed from your menu, store all interim values in an array before totaling them. Use an additional loop structure after the first, to sum values in the array. The last thing you do is to display the total amount.
1. void sumIntegers();
In sumIntegers, you will store each of the even numbers in an array of integers. Once this is completed, code a second loop (after the first) that accumulates each of the integers in the array before displaying the total.
The behavior and output of this function should not change otherwise.
2. void sumDoubles();
In sumDoubles, you will do the same, except with an array of doubles. Do not store the sentinel value (-1) in the array, nor include it in the total.
The behavior and output of this function should not change otherwise.
3. void calcFactorial();
In calcFactorial, store each component of the calculation in an array as you calculate it. Example: When calculating 5!, store 1, 2, 3, 4 & 5 in the array.
After the function, use a for loop and the contents of your array to display a message showing each of the numbers you multiplied together. Your output should look something like this, depending on the number entered.
5! = 5 * 4 * 3 * 2 * 1
The behavior and output of this function should not change otherwise.
IMPORTANT: Your output should look identical to the output of your original program except where specifically noted. The idea here is that you are making mostly structural changes/improvements
Explanation / Answer
I have modified the methods suminteger and sumdouble...check it out. If you have any query contact me on timacs12@gmail.com ----------------------------------------------- #include void menu(); void sumIntegers (); void sumDoubles(); void calcFactorial (); int main(){ char choice; printf("Welcome To Assignment #2 "); do{ menu(); scanf(" %c",&choice); switch (choice){ case '1'://sum even int printf(" I want to sum up some even integers. "); sumIntegers(); break; case '2'://sum $ amounts printf(" I want to sum up dollar amounts. "); sumDoubles(); break; case '3'://factorial num printf(" I want to calculate the n factorial. "); calcFactorial(); break; case 'Q': printf("Thank you for playing! "); break; default: printf("*** You entered an invalid choice!!! *** "); } }while(choice!='Q'); printf("Press any key to continue . . ."); scanf(" %c",&choice); //stall console from closing for 1 character read return 0; } void menu(){ printf(" Please choose from the following options: "); printf("1 - Sum of even integers "); printf("2 - Sum of dollar amounts "); printf("3 - Factorial of a number "); printf(" "); printf("Q - Quit "); printf(" "); printf("Enter your choice (1, 2, 3, Q):"); } void sumIntegers (){ int n; int sum=0,i; int even[100]; do{ printf("Enter an integer: ");//Prompt the user to enter a positive integer. scanf("%d",&n);//Get the integer value from the user. if(nRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.