Write a program using c statement for calculating the sum of a list of float num
ID: 3852722 • Letter: W
Question
Write a program using c statement for calculating the sum of a list of float numbers
The program should greet the user and present the user with a menu with the following
options:
1: Enter 5 real numbers for which the program will calculate the sum.
2: Enter as many real numbers as you want for which the program will calculate
the sum.
3: Exit the program
Your program should use a while loop to handle the overall flow of the program (handle the menu and execute the option based on the user’s choice, or exit the program)
The condition of the while loop should be related to whether the user has selected option 3.
Options 1 and 2 should be executed by using an if else if statement, along with the code within each if block to complete the tasks.
Option number 1 should use a static array that is length 5. The value 5 should be stored as a named constant . If option 1 is selected, your program should do the following:
First, your program should read in exactly 5 float numbers and store those numbers in a static array.
Your program should then calculate the sum of the 5 numbers (using a for loop).
Your program should then display the sum to the user.
Option number 2 should use an array that is allocated dynamically . This array should first be initialized to NULL, and then dynamically allocated later in the program. If option 2 is selected, your program should do the following:
First, your program should ask the user how many numbers they wish to enter.
Your program should then dynamically allocate the array using this given number and the malloc function (remember to type cast, and the data type we are using
here is float ).
Your program should then validate that the array was allocated correctly
Your program should then read in the float numbers (using a for loop) from
the user.
Your program should then calculate the sum of the numbers (using a for loop).
Your program should then display the sum to the user.
Your program should then deallocate the memory for the dynamic array and
nullify the pointer
If option 3 is selected, your program should do the following:
Simply exit the program.
NOT by using an exit statement, but by getting out of the while loop due to
the condition evaluating to false, then returning 0 at the end of the program.
Explanation / Answer
The answer is as follows:
The answer is as follows:
The code is as follows:
#include<stdio.h>
#define FIVE 5
int main()
{
float data[FIVE];
float sum;
int choice;
float *ptr;
int n;
do {
printf("1: Enter 5 real numbers for which the program will calculate the sum ");
printf("2: Enter as many real numbers as you want for which the program will calculate the sum ");
printf("3: Exit the program ");
printf("Enter your choice:");
scanf("%d",&choice);
switch (choice) {
case 1:
sum = 0;
for (i=0; i<FIVE; i++){
printf("Enter number %d",i);
scanf("%f",&data[i]);
sum = sum + data[i];
}
printf("Sum : %f ", sum);
case 2:
printf("Enter how many float numbers you want to input:");
scanf("%d", &n);
ptr = (float *)malloc(n*sizeof(float));
sum = 0;
for (i=0; i<n; i++){
printf("Enter number %d",i);
scanf("%f",(ptr+i));
sum = sum + *(ptr + i);
}
printf("Sum : %d ", sum);
free(ptr);
}
} while (choice != 3)
return 0
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.