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
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 users 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.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
