Write a C/C+ program that uses a for loop to input data points (e.g. a list of t
ID: 3864868 • Letter: W
Question
Write a C/C+ program that uses a for loop to input data points (e.g. a list of test grades) into a 1 dimensional vector called Grades [] from the keyboard using a for loop or while loop. Also use for loops to calculate the mean, standard deviation and minimum/maximum values in the input data set. Use the modulus function (Grades[i] %2) to find the number of even and odd numbers in the data set. Test run your script with following data set Grades[] = (98, 78, 67, 55, 69, 79, 91, 83, 86, 74, 77, 99, 58, 63, 66, 72, 82, 87, 93, 64, 59, 80, 68, 85, 75, 96, ]; For (int I = 0; IExplanation / Answer
/*
* C program to input numbers and find the mean, variance and standard deviation
*/
#include <stdio.h>
#include <math.h>
#define MAXSIZE 50
void main()
{
float x[MAXSIZE];
int i, n, even, odd, num;
float average,variance, std_deviation, sum = 0, sum1 = 0, minimum, maximum, location = 1;
printf("Enter the number of elements in array ");
scanf("%d", &n);
printf("Enter %d real numbers ", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for (i = 0; i < n; i++)
{
sum = sum + x[i];
}
average = sum / (float)n;
/* Compute variance and standard deviation */
for (i = 0; i < n; i++)
{
sum1 = sum1 + pow((x[i] - average), 2);
}
variance = sum1 / (float)n;
std_deviation = sqrt(variance);
/* Compute max of array */
maximum = x[0];
for (i = 0; i < n; i++)
{
if (x[i] > maximum)
{
maximum = x[i];
location = i+1;
}
}
minimum = x[0];
for ( i = 1 ; i < n ; i++ )
{
if ( x[i] < minimum )
{
minimum = x[i];
location = i+1;
}
}
printf("Minimum element is present at location %d and it's value is %d. ", location, minimum);
printf("Maximum element is present at location %d and it's value is %d. ", location, maximum);
printf("Average of all elements = %.2f ", average);
printf("variance of all elements = %.2f ", variance);
printf("Standard deviation = %.2f ", std_deviation);
printf("Even numbers in the array are : ");
for (i = 0; i < n; i++)
{
if (x[i]%2 == 0)
{
even++;
printf("%d ", x[i]);
}
}
printf(" Odd numbers in the array are: ");
for (i = 0; i < n; i++)
{
if (x[i] % 2 != 0)
{
odd++;
printf("%d ", x[i]);
}
}
printf(" There are %d even numbers and %d odd numbers out of %d given numbers. ",even,odd,num);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.