In: Computer Science
A. Arrays An array is basically of collection of related variables of the same type. For example, a collection of grades of students, a collection of salary of employees of a company, etc. When working with student grades, for example, we can then perform statistical computations on that set of data to obtain more meaningful information. In this part of the lab, you will write a complete C++ program named Lab6A.cpp that allows the user to enter the grades of 10 students in a class in an array called grades and you will calculate the standard deviation of those grades using the following formula for standard deviation, �: � = #1 �&(�! − �)" # !$% where � is the number of grades, or ����, �! are the individual grades, and � is the mean. Let’s go ahead and start: • First, declare a constant integer called SIZE and initialize it to 10. • Now, declare an array of floating-point numbers called grades that can hold 10 grades, using the constant that you just declared for the size. 2 • Declare three additional floating-point numbers to store the mean (i.e., average), sum, and standard deviation. Initialize both the sum and standard deviation variables to 0. • Process the array of grades using a for loop as follows to read in the grades and calculate the mean: o Prompt for and read in grades into the array. o Add each grade to the current sum. • Calculate the mean by taking the sum of all the grades and dividing by SIZE. • Now, use another for loop to sum the square of the difference of the grades and the mean as follows: o Add the square of the difference of the grade and the mean to the standard deviation. To make things clearer, use the pow() function with the first argument being the current grade in the array minus the mean, and the second argument being 2 for the square. Then, simply add that result to the current standard deviation. • Finally, take the square root of the standard deviation divided by SIZE and print the result. As an example, the standard deviation of 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10 is 2.87228. Once you complete your program, save the file as Lab6A.cpp, making sure to compile and run the program to verify that it produces the correct results. Note that you will submit this file to Canvas.
Here is the C++ code with detailed comments to the given question.
Sample output is added at the end.
Code: Lab6A.cpp :
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const int SIZE=10; /*declares a constant integer called SIZE and initializes it to 10*/
float grades[SIZE]; /*declares an array of floating-point numbers called grades that can hold 'SIZE' number of grades*/
float mean,sum=0,standardDeviation=0; /*declares three floating-point numbers to store the mean, sum, and standard deviation. sum and standardDeviation areinitialized to 0*/
for(int i=0;i<SIZE;i++){ /*runs the loop from i=0 to i=SIZE-1*/
cout<<"Enter grade "<<i+1<<": "; /*asks user to enter grade*/
cin>>grades[i]; /*takes the value of entered grade and stores it ib grades[i]*/
sum+=grades[i]; /*adds the current grade to sum*/
}
mean=sum/SIZE; /*calculates mean by divideing sum with SIZE*/
for(int i=0;i<SIZE;i++){ /*runs the loop from i=0 to i=SIZE-1*/
standardDeviation+=pow(grades[i]-mean,2); /*computes the square of difference between current grade and mean and adds it to standardDeviation*/
}
standardDeviation=sqrt(standardDeviation/SIZE); /*computes standardDeviation by taking square root of standardDeviation divided by SIZE*/
cout<<"\nStandard deviation of the entered data is "<<standardDeviation; /*prints the standard deviation */
}
Sample output: