Code from project 5 #include <stdio.h> #include <stdlib.h> #define SIZE 100 #ifn
ID: 3825373 • Letter: C
Question
Code from project 5
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
#ifndef FALSE
#define FALSE 0
#define TRUE !FALSE
#endif
#include <math.h>
int main()
{
int done, choice, N, j, numbers;//declaration of variables
float n[SIZE], x[SIZE], arr[0], smallest, largest;//declaration of variables
float sum, avg, sum1, variance, standard_deviation, temp;//declaration of variables
done=FALSE;
printf("Program to process a list of numbers ");//display
while(!done){
printf("Select the desired opersation: ");
printf(" 0 Enter list of numbers ");
printf(" 1 Find the smallest number ");
printf(" 2 Find the largest number ");
printf(" 3 Find the average of the numbers ");
printf(" 4 Find the standard deviation of the numbers ");
printf(" 5 Reverse the list of numbers ");
printf(" 6 Rotate the list of numbers to the right ");
printf(" 7 Rotate the list of numbers to the left ");
printf(" 8 Exit the program ");
printf("choice? ");
scanf("%d",&choice);
switch(choice){
case 0:
printf("How many numbers?");
scanf("%d",&numbers);
for (int i =0; i<= numbers-1; ++i)
{
printf("n[%d]=",i);
scanf("%f",&n[i]);
sum +=n[i];
}
printf("The Entered array is: ");//display
for(int i=0; i <= numbers-1; i++)
{
printf("n[%d]=%.2f ", i, n[i]);//display results
}
break;
//////////////////////////////////////////////////////////////////
case 1:
smallest = n[0];
for (int i=0; i <= numbers-1; ++i){//smallest
if(n[i]<smallest)
{
smallest = n[i];
}
}
printf("The smallest number is: %.2f ", smallest);//display
break;
///////////////////////////////////////////////////////////////////
case 2:
largest=n[0];
for(int i=0; i <= numbers-1; ++i){//largest value
if(n[i]>largest)
{
largest = n[i];
}
}
printf("The largest number is: %.2f ", largest);//display
break;
///////////////////////////////////////////////////////////////////
case 3:
avg=sum/numbers;//average
printf("The average is: %.2f ", avg);//display results
break;
///////////////////////////////////////////////////////////////////
case 4:
for (int i=0; i <= numbers-1; ++i){
sum1+= pow((n[i]-avg),2);
}
N = numbers-1;//part of the standard deviation "N" value
standard_deviation=sqrt(sum1/N);//standard deviation equation
printf("Standard Deviation is: %.2f ",standard_deviation);//display
break;
///////////////////////////////////////////////////////////////////
case 5:
for (int i = numbers -1, j = 0; i >= 0; --i, ++j){
x[j]=n[i];//reverse numbers
}
for (int i=0; i <= numbers-1; ++i){
n[i]=x[i];
}
printf("array is: now "); // display results
for (int i = 0; i <= numbers-1; ++i){
printf("%.2f ", n[i]);
}
break;
///////////////////////////////////////////////////////////////////
case 6:
temp = n[numbers-1];
for (int i = numbers-2; i >= 0; --i){//switches position
n[i+1] = n[i];
}
n[0] = temp;
printf("array is: now ");//display results
for (int i =0; i <= numbers-1; ++i){
printf("%1.2f ", n[i]);
}
break;
///////////////////////////////////////////////////////////////////
case 7:
temp=n[0];
for(int i =1; i <= numbers-1; ++i){//switches position
n[i-1] = n[i];
}
n[numbers-1]=temp;
printf("array is: now ");//display results
for(int i =0; i <= numbers-1; ++i){
printf("%.2f ", n[i]);
}
break;
////////////////////////////////////////////////////////////////////
case 8:
puts("Good bye!");//quit
done=TRUE;
break;
}
}
return 0;
}
CODE: From project 5:
This lab assignment is derived from a requirement in Programming Project 5-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 a 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 mai n() 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 itifndef FALSE to determine if these symbolic constants are already defined and if not define both main0 function int done FALSE: is a flag used to determine if the user has finished using the program or not while-loop not done Pk command chooser printfo 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 scanf0 has returns 0 integer values as choice scanf() can be used to read a number, character, or string from the keyboard. scanfO 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 state structure supports a compact com mand 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 scant logic cannot stop all invalid choices, but the default case of the switc structure can catch the others and h() 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 GoodbyeExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
#define SIZE 100
#ifndef FALSE
#define FALSE 0
#define TRUE !FALSE
#endif
#include <math.h>
int command_chooser();
int command_disptcher(float n[],float x[],int choice,int *num);
int main()
{
printf("Program to process a list of numbers ");//display
int done, choice, numbers;//declaration of variables
float n[SIZE], x[SIZE], arr[0];//declaration of variables
done=FALSE;
while(!done){
choice = command_chooser();
done = command_disptcher(n,x,choice,&numbers);
}
return 0;
}
int command_chooser(){
int choice;
while(1){
printf("Select the desired opersation: ");
printf(" 0 Enter list of numbers ");
printf(" 1 Find the smallest number ");
printf(" 2 Find the largest number ");
printf(" 3 Find the average of the numbers ");
printf(" 4 Find the standard deviation of the numbers ");
printf(" 5 Reverse the list of numbers ");
printf(" 6 Rotate the list of numbers to the right ");
printf(" 7 Rotate the list of numbers to the left ");
printf(" 8 Exit the program ");
printf("choice? ");
scanf("%d",&choice);
if(choice<0 || choice>8){
printf("Invalid Choice, try again! ");
continue;
}
break;
}
return choice;
}
int command_disptcher(float n[],float x[],int choice,int *num){
float sum, avg, sum1, variance, standard_deviation, temp, smallest, largest;//declaration of variables
int N, j,numbers;
numbers = *num;
int done = FALSE;
switch(choice){
case 0:
printf("How many numbers?");
scanf("%d",num);
numbers = *num;
for (int i =0; i<= numbers-1; ++i)
{
printf("n[%d]=",i);
scanf("%f",&n[i]);
sum +=n[i];
}
printf("The Entered array is: ");//display
for(int i=0; i <= numbers-1; i++)
{
printf("n[%d]=%.2f ", i, n[i]);//display results
}
break;
//////////////////////////////////////////////////////////////////
case 1:
smallest = n[0];
for (int i=0; i <= numbers-1; ++i){//smallest
if(n[i]<smallest)
{
smallest = n[i];
}
}
printf("The smallest number is: %.2f ", smallest);//display
break;
///////////////////////////////////////////////////////////////////
case 2:
largest=n[0];
for(int i=0; i <= numbers-1; ++i){//largest value
if(n[i]>largest)
{
largest = n[i];
}
}
printf("The largest number is: %.2f ", largest);//display
break;
///////////////////////////////////////////////////////////////////
case 3:
avg=sum/numbers;//average
printf("The average is: %.2f ", avg);//display results
break;
///////////////////////////////////////////////////////////////////
case 4:
for (int i=0; i <= numbers-1; ++i){
sum1+= pow((n[i]-avg),2);
}
N = numbers-1;//part of the standard deviation "N" value
standard_deviation=sqrt(sum1/N);//standard deviation equation
printf("Standard Deviation is: %.2f ",standard_deviation);//display
break;
///////////////////////////////////////////////////////////////////
case 5:
for (int i = numbers -1, j = 0; i >= 0; --i, ++j){
x[j]=n[i];//reverse numbers
}
for (int i=0; i <= numbers-1; ++i){
n[i]=x[i];
}
printf("array is: now "); // display results
for (int i = 0; i <= numbers-1; ++i){
printf("%.2f ", n[i]);
}
break;
///////////////////////////////////////////////////////////////////
case 6:
temp = n[numbers-1];
for (int i = numbers-2; i >= 0; --i){//switches position
n[i+1] = n[i];
}
n[0] = temp;
printf("array is: now ");//display results
for (int i =0; i <= numbers-1; ++i){
printf("%1.2f ", n[i]);
}
break;
///////////////////////////////////////////////////////////////////
case 7:
temp=n[0];
for(int i =1; i <= numbers-1; ++i){//switches position
n[i-1] = n[i];
}
n[numbers-1]=temp;
printf("array is: now ");//display results
for(int i =0; i <= numbers-1; ++i){
printf("%.2f ", n[i]);
}
break;
////////////////////////////////////////////////////////////////////
case 8:
puts("Good bye!");//quit
done=TRUE;
break;
}
return done;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.