Write a C programming (Arrays) , which presents the user a menu of operations th
ID: 3813539 • Letter: W
Question
Write a C programming (Arrays), which presents the user a menu of operations that it can perform to manipulate an array floating point numbers. The choices are listed and a prompt waits for the user to select by entering a number. This kind of menu is a common feature of command-line programs. It can be implemented to execute in the main() function as we do in this lab or it can be implemented in a function known as the command-chooser that returns a numeric value for a choice that can be passed to another function known as the command-dispatcher.
Define symbolic constants for FALSE and TRUE
Use #ifndef FALSE to determine if these symbolic constants are already defined and if not define both
main() function
int done = FALSE; is a flag used to determine if the user has finished using the program or not
while-loop not done
/* command chooser */
printf() list of menu choices
The first part of the command chooser prints a menu that lists choices for the user. Each choice is a string that begins with the value to be entered on the keyboard to make the choice and ends with a brief description of an operation.
do-while scanf() has returns 0 integer values as choice
scanf() can be used to read a number, character, or string from the keyboard. scanf() also returns an integer whenever it is called usually this value is not important, but for the command chooser it is essential.
/* command dispatcher */
switch(choice) for the case that corresponds to the value of choice
A switch-case statement structure supports a compact command dispatcher.
one choice set done TRUE
The command choice to exit the program sets the done flag to TRUE
other choices are program operations
All other command choices perform an operation either inline or by calling a function with or without arguments. Operations that depend on other operations can use a flag to determine if the necessary prior operations have been completed.
default prints invalid choice message
The scanf() logic cannot stop all invalid choices, but the default case of the switch() structure can catch the others and print a message. The while-loop can then continue prompting the user to select an operation or to quit.
at end of while-loop say Goodbye
Explanation / Answer
#include<stdio.h>
#ifndef FALSE
#define FALSE 0
#endif // FALSE
#ifndef TRUE
#define TRUE 1
#endif // FALSE
void command_dispatcher(int choice,float array[], int *done)
{
float sum = 0.0, product =1.0, min, max;
int i;
switch(choice)
{
case 1:
*done=TRUE;
break;
case 2:
min=array[0];
for(i=1;i<10;i++)
if(array[i]<min )
min=array[i];
printf(" Smallest element in the array is : %d",min);
break;
case 3:
max=array[0];
for(i=1;i<10;i++)
if(array[i]>max )
max=array[i];
printf(" Largest element in the array is : %d",max);
break;
case 4:
for(i=0;i<10;i++)
sum+=array[i];
printf(" Sum of all the elements in the array is: %d", sum);
break;
case 5:
for(i=0;i<10;i++)
product*=array[i];
printf(" Product of all the elements in the array is: %d", product);
break;
default:
printf(" Invalid choice!");
}
}
void command_chooser(float array[], int *done)
{
int choice = 0;
do
{
printf(" Array Operations ");
printf("1.Exit");
printf("2.Smallest element ");
printf("3.Largest element ");
printf("4.Sum of elements ");
printf("5.Products of elements ");
printf("enter your choice:");
scanf("%d",&choice);
command_dispatcher(choice,array,done);
}while(choice!=5);
}
void main()
{
float array[10];
int done= FALSE,i;
for(i=0;i<10;i++)
{
scanf("%f", &array[i]);
}
command_chooser(array,&done);
printf(" Done= %d Terminating the program... ",done);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.