Problem: Write a C program that requests a count of floating point numbers, read
ID: 3813176 • Letter: P
Question
Problem: Write a C program that requests a count of floating point numbers, reads these numbers from the terminal, stores these values in an array of float, and performs the following functions:
Finds the smallest number
Finds the largest number
Calculates the mean or average of the numbers
Calculates the standard deviation from the mean
Reverses the array of numbers
Rotates the list of number to the right
Rotates the list of numbers to the left
The program should be constructed to process an array of 2 to 100 elements and to be changed in the source code by adjusting one value. The program should first prompt the user for the number of elements in the list. After the user enters this integer value, the program should ask for floating point numbers until all elements in the array are valued. If the user enters an invalid number (out of range or not a number), the program should warn the user and continue to prompt for a valid number. You are allowed to define one array in your program and all operations must use that one array only.
The program should be constructed to prompt the user for the next operation, perform it, print out the result until the user selects the exit operation. Do not clear the screen. The input and output from your program should be allowed to scroll as if the terminal is a teletype. The initial startup of the program should look like this:
I:cmdlinepgm5>pgm5
Program to process a list of numbers
Select the desired operation:
0 - Enter list of numbers
1 - Find the smallest number
2 - Find the largest number
3 - Find the average of the numbers
4 - Find the standard deviation of the numbers
5 - Reverse the list of numbers
6 – Rotate the list of numbers to the right
7 - Rotate the list of numbers to the left
8 - Exit the program
Choice?
The function of each of the choices should be as follows:
Choice 0 – this choice asks the user for the number of elements to read. If the number not on the interval [2,100], it should ask again until the user responds with a correct number. Once a valid number is entered, the program should prompt the user to enter a floating point number for each element. After all of the numbers are read, the program should print out the array so that the user can verify its contents. The program then prints the main menu and asks the user for the next choice. An example is given below.
Choice?0
How many numbers? 5
x[0] = 1.25
x[1] = 3.5
x[2] = -1.75
x[3] = 2.125
x[4] = 5.0
The Entered array is:
x[0] = 1.25
x[1] = 3.50
x[2] = -1.75
x[3] = 2.13
x[4] = 5.00
Select the desired operation:
0 - Enter list of numbers
1 - Find the smallest number
2 - Find the largest number
3 - Find the average of the numbers
4 - Find the standard deviation of the numbers
5 - Reverse the list of numbers
6 – Rotate the list of numbers to the right
7 - Rotate the list of numbers to the left
8 - Exit the program
Choice?
Choice 1 – this choice looks through the array, finds the smallest element and prints it. Llarger negative numbers are considered to be smaller than smaller negative numbers and all negative numbers are smaller than positive numbers.
Choice?1
The smallest number is: -1.75
Select the desired operation:
0 - Enter list of numbers
1 - Find the smallest number
2 - Find the largest number
3 - Find the average of the numbers
4 - Find the standard deviation of the numbers
5 - Reverse the list of numbers
6 – Rotate the list of numbers to the right
7 - Rotate the list of numbers to the left
8 - Exit the program Choice?
Choice 2 – this choice looks through the array to find the largest element and print it.
Choice?2
The largest number is: 5.00
Select the desired operation:
0 - Enter list of numbers
1 - Find the smallest number
2 - Find the largest number
3 - Find the average of the numbers
4 - Find the standard deviation of the numbers
5 - Reverse the list of numbers
6 – Rotate the list of numbers to the right
7 - Rotate the list of numbers to the left
8 - Exit the program Choice?
Choice 3 – this choice calculates and prints the average of all of the numbers.
Choice?3
The average is: 2.03
Select the desired operation:
0 - Enter list of numbers
1 - Find the smallest number
2 - Find the largest number
3 - Find the average of the numbers
4 - Find the standard deviation of the numbers
5 - Reverse the list of numbers
6 – Rotate the list of numbers to the right
7 - Rotate the list of numbers to the left
8 - Exit the program Choice?
Choice 4 – this choice calculates and prints the standard deviation of the values in the array. Calculation of the standard deviation requires the mean (average) of the numbers. This average should be calculated by choice 3 as shown above. If the user asks
for choice 4 to be performed before the average is calculated by choice 3, the program prints a message to the user indicating that the average must be calculated first. It then prints the main menu and asks the user for the next choice. Once the choice 4 task is successfully completed, the program prints the standard deviation.
Statisticians have developed a theoretically sound method for standard deviation using a random sample of a population. Most statistical analysis is done using a random sample. The formula for the mean and standard deviation are shown below.
Mean or average:
Standard Deviation of a Sample:
The average or mean ( ) is calculated by Choice 3 and can be reused in the calculation of the sum of the squares ( ).
Choice?4
The standard deviation is: 2.54
Select the desired operation:
0 - Enter list of numbers
1 - Find the smallest number
2 - Find the largest number
3 - Find the average of the numbers
4 - Find the standard deviation of the numbers
5 - Reverse the list of numbers
6 – Rotate the list of numbers to the right
7 - Rotate the list of numbers to the left
8 - Exit the program Choice?
Choice 5 – this choice reverses the order of the elements in the list. That is, the first and last elements of the array should swap values, the second and next-to last elements should swap values, etc. Note that for an array with an odd number of elements, the middle element of the array will keep its same value. An example, based on the original list shown in the choice 0 example above, is given below.
Choice?5
array is: now
x[0] = 5.00
x[1] = 2.13
x[2] = -1.75
x[3] = 3.50
x[4] = 1.25
Select the desired operation:
0 - Enter list of numbers
1 - Find the smallest number
2 - Find the largest number
3 - Find the average of the numbers
4 - Find the standard deviation of the numbers
5 - Reverse the list of numbers
6 – Rotate the list of numbers to the right
7 - Rotate the list of numbers to the left
8 - Exit the program Choice?
Choice 6 – this choice shifts each element in the array beginning with element 0 to the right (towards the last element in the array). The last element is then moved to element 0 at the top of the list. An example, based on the list as entered shown in the choice 0 example above, is given below.
Array rotate right
5.00 2.13 -1.75 3.50 1.25
Element 0 to 1; Element 1 to 2; Element 2 to 3; Element 3 to 4; Element 4 to 0
1.25 5.00 2.13 -1.75 3.50
Choice?6
array is: now
x[0] = 1.25
x[1] = 5.00
x[2] = 2.13
x[3] = -1.75
x[4] = 3.50
Select the desired operation:
0 - Enter list of numbers
1 - Find the smallest number
2 - Find the largest number
3 - Find the average of the numbers
4 - Find the standard deviation of the numbers
5 - Reverse the list of numbers
6 – Rotate the list of numbers to the right
7 - Rotate the list of numbers to the left
8 - Exit the program
Choice?
Choice 7 – this choice shifts each element in the array beginning with the last element in the array to the left (towards the first element in the array). The first element is then moved to the bottom of the list taking the place left vacant by shifting the previous last element. An example, based on the list as entered shown in the choice 0 example above, is given below.
Choice?7
array is: now
x[0] = 5.00
x[1] = 2.13
x[2] = -1.75
x[3] = 3.50
x[4] = 1.25
Select the desired operation:
0 - Enter list of numbers
1 - Find the smallest number
2 - Find the largest number
3 - Find the average of the numbers
4 - Find the standard deviation of the numbers
5 - Reverse the list of numbers
6 – Rotate the list of numbers to the right
7 - Rotate the list of numbers to the left
8 - Exit the program
Choice?
Choice 8 – this choice should print an exit message and exit the program as shown below.
Choice?
8 Good bye!
What is wrong with my code?
#include <stdio.h>
#include <stdlib.h>
#define SIZE 98
#ifndef FALSE
#define FALSE 0
#define TRUE !FALSE
#endif //maximum size of array
#include <math.h>
int main ()
{
int n[98];//n is an array of 98 integers
int choice;
int number;
int i, sum = o, reverse =0, j;
int done;
int smallest;
int largest;
float Average, standardDeviation;
while (!done)
printf ("Program to process a list of numbers ");
printf("Select the desired operation: ");
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)
{
//choice 0
case 0:
printf("How many numbers? ");
scanf("%d", &number);
for (i=0; i<number; ++i) {
printf ("x [%d] = %.2f", &number, number);
}
break;
//choice one
case 1:
scanf("%d", &number);
for(i=0; i<number; ++i) {
if (n[i]<smallest){
smallest = n[i];
}
}
printf ("The smallest number is: %.2f ", &smallest);
break;
//choice two
case 2:
scanf("%d", &size);
for (i=0; i<size; i++)
scanf("%d", &n[i]);
largest = n[0];
for (i=1; i<size; i++) {
if (largest <n[i]){
largest = n[i];
}
}
printf ("The largest number is: %.2f ", &largest);
break;
//choice three
case 3:
scanf("%d", &number);
for (i=0; i<number; ++i){
scanf ("%d", &number);
sum = sum + number;
}
Average = sum/number;
printf ("The avaerage is: %.2f ", Average);
break;
//choice 4
case 4:
printf("The average must be calculated first"
break;
//you have to print the list here again!
case 5;
scanf ("%d", &number);
for (i=0; i<number; +=i)
scanf ("%d", &n[i]);
for (i=0; i<number; ++i) {
Standard Deviation += pow (n[i]-Average, 2);
}
printf("The standard deviation is: %.2f ", standardDeviation);
break;
//choice 5
case 6:
scanf("%d", &number);
for(i=0; i<number; i++) {
scanf("%d". &n[i]);
}
j = i - 1; //j is last element
i = 0;
while (i<j) {
reverse = n[i];
n[i] = n[j];
n[j] = reverse;
i++; //increment
j--; } //decrement
printf("Array is:now ");
print ("x[%d] = %.2f ", standardDeviation);
break;
//choice 6
case 6:
scanf("%d", &number);
//rotating arrays
}
break;
//choice 8
case 9:
printf("Good bye! ");
return 0;
}
Explanation / Answer
Following are the problems I found out by initially sseing your code
1.macro SIZE is defined but not used anywhere and there is no need for defining the macro.
2. size of array should be 100 not 98 and its type should be float.
3. sum should be equal to 0 not o.
4. while loop is not put in a block{} so it only executes 1 line again and again.
5. done is declared but not defined and is used directly without any updation
6. The program is asking the user the size of the array in case 0,1,2,3,5,6 but it should be asking the size in case 0 only and not other choices.
7. In case 0 array is not entered it is only printed without getting entered.
8. In case 2 the array is not to be entered again, it should print the largest number among the existing array as well as size is not declared.
9. case 4 has syntax error and it is not doing anything to calculated standard mean
variable StandardDeviation should not have space
10. There is no way to check if case 3 was called once or not.
11. case 5,6,7,8 not completed and some have syntax error as well.
12. case 6 is made 2 times
variable jreverse are not declared
13. array is reversed but not printed
print is not a function printf is
14. There shoud have not been any case 9.
15. The user is not prompted to whether the program should continue or not.
16. Many variables are declared int but should be float.
I am proving the code for case 0,1,2,3,4,5,8
#include <stdio.h>
#include <stdlib.h>
#define SIZE 98
#ifndef FALSE
#define FALSE 0
#define TRUE !FALSE
#endif //maximum size of array
#include <math.h>
int main ()
{
float n[98];//n is an array of 98 integers
int choice;
int number;
int i, j;
int done=0;
float smallest;
float largest;
float reverse;
float sum=0,Average, standardDeviation;
int flag =0; //flag=0 means average has not been calculated yet
while (!done)
{
printf ("Program to process a list of numbers ");
printf("Select the desired operation: ");
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)
{
//choice 0
case 0:
flag=0;
printf("How many numbers? ");
scanf("%d", &number);
for (i=0; i<number; ++i) {
printf("x[%d] = ",i);
scanf ("%f", &n[i]);
}
printf("The entered array is ");
for(i=0;i<number;++i)
{
printf("x[%d] = %.2f ",i,n[i]);
}
break;
case 1:
smallest = n[0];
for(i=1; i<number; ++i) {
if (n[i]<smallest){
smallest = n[i];
}
}
printf ("The smallest number is: %.2f ", smallest);
break;
//choice two
case 2:
largest = n[0];
for (i=1; i<number; i++) {
if (largest <n[i]){
largest = n[i];
}
}
printf ("The largest number is: %.2f ", largest);
break;
//choice three
case 3:
sum=0;
for (i=0; i<number; ++i){
sum = sum + n[i];
}
Average = sum/number;
printf ("The average is: %.2f ", Average);
flag =1;
break;
//choice 4
case 4:
if(flag==0)
{ printf("The average must be calculated first ");
}
else
{
standardDeviation =0;
for(i=0;i<number;++i)
{
standardDeviation += pow(n[i] - Average, 2);
}
standardDeviation/=number;
printf ("The standard deviation is: %.2f ", standardDeviation);
}
break;
case 5:
j = number - 1; //j is last element
i = 0;
while (i<j) {
reverse = n[i];
n[i] = n[j];
n[j] = reverse;
i++; //increment
j--; } //decrement
printf("Array is:now ");
for(i=0;i<number;++i)
printf ("x[%d] = %.2f ", i,n[i]);
break;
case 8:
printf("Good bye! ");
done =1;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.