Question

In: Computer Science

A. Arrays An array is basically of collection of related variables of the same type. For...

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.

Solutions

Expert Solution

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:


Related Solutions

Arrays provide the programmer the ability to store a group of related variables in a list...
Arrays provide the programmer the ability to store a group of related variables in a list that can be accessed via an index. This is often very useful as most programs perform the same series of calculations on every element in the array. To create an array of 10 integers (with predefined values) the following code segment can be utilised: int valueList[10] = {4,7,1,36,23,67,61,887,12,53}; To step through the array the index '[n]' need to be updated. One method of stepping...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist...
Overlapping Arrays (C++) An array overlaps another array if all elements of the latter array exist in the former array. They need not necessarily be in the same order. For example, [1,7,3,4,2] overlaps [1,2,3] because 1,2 and 3 exist in [1,7,3,4,2]. To make the implementation easy, [1,7,3,4,2] overlaps [1,1,2,3] as well. We don’t need to check whether 1 appears twice in the first array. Write a program that lets the user enter two arrays and displays whether the first array...
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
Is it ethical for a company to sell water, which is basically all the same, in...
Is it ethical for a company to sell water, which is basically all the same, in a way that commands such high prices?
1. With respect to resizable arrays, establish the relationship between the size of the array and...
1. With respect to resizable arrays, establish the relationship between the size of the array and the time it takes to perform random access (read a the value stored at a given position somewhere in the array). Provide empirical evidence to support your answer.
There is a lab assignment. Basically includes array and some other basic stuff. Comment if you...
There is a lab assignment. Basically includes array and some other basic stuff. Comment if you have any questions. Thank you. I have got all classes here so that it might be more make sense. BUS CLASS package classes; //DO NOT ERASE THE TODO STUBS! WRITE YOUR SOLUTIONS BELOW THE STUBS! public class Bus extends Commercial {       private int numPassengers;       public Bus()    {        super();        numPassengers = 0;    }      ...
Description: One problem with dynamic arrays is that once the array is created using the new...
Description: One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This assignment asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have the following: • A private member variable called...
Read and print parallel array - How can this be made to read parallel arrays and...
Read and print parallel array - How can this be made to read parallel arrays and then print them? The program presented here is intended to read from the text file and build parallel arrays. Then it will print as shown. The first line will not be stored in the array. The second line will be stored in firstArray[] and the third line will then be stored in secondArray[] and the arrays will repeat until the file is read. begin...
The rationale for classical economic theory with Keynes on determining national output is basically the same...
The rationale for classical economic theory with Keynes on determining national output is basically the same because both theories state that a country tends to improve its economy. Therefore, there is no debate on these two economic theories. Discuss this!
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT