Creating a new array from adjusting an existing one.

For the part of the code that I am on my current struggle is find out how to take my original array and creating a new array of deviation values from the average of the numbers in the first array. I'm stuck on this question and any help given would be greatly appreciated. I tried a couple different things but they didn't work so my code looks a little obscure right now.

The total question is:
Write a program to input or initialize the following integer numbers into an array called grades: 89,95,72,83,99,55,86,75,92,73,97,75, 82,73,88. Calculate the average of the numbers and use the average to determine the deviation of each value from the average. [i]Store each deviation in an array named deviation.[/i] Each deviation is obtained as the element value less the average of all the data. Have your program display each deviation alongside its corresponding element from the grades array.
Calculate the variance of the data. The variance is obtained by squaring each
individual deviation and dividing the sum of the squared deviations by the number of
deviations.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main()
{
	const int arraysize= 15;
	double average, sum=0, dev=0; 
	int deviation[arraysize];
	
	int grades[arraysize]= {89, 95, 72, 83, 99, 55,86,75,92, 73, 97,75, 82, 73, 88};
	for (int i=0; i<15; i++)
		sum = sum+ grades[i];
		average= sum/arraysize;
	for (int i=0; i<15; i++)
		{deviation[i]
	
a few things to note:
You average will most likely NOT be an integer, so you'll have to do this:

1
2
3
4
5
6
7
8
9
        const int arrSIZE = 15;
	for (int i=0; i<arrSIZE; i++)
	{
                // using braces is good, even with one line in the for.
		sum = sum+ grades[i];		
	}

	// cast the denominator so we don't get division by integer issues.
	average= sum/static_cast<double>(arrSIZE);


which in turn means your deviation array should be an array of doubles, not integers.

1
2
3
4
5
6
7
        // good to initialise your array with zero's.
        double deviation[arrSIZE] = {0.0};
	for (int i=0; i<arrSIZE; i++)
	{
		deviation[i] = grades[i]-average;

	}
Last edited on
Alright perfect thats exactly what I was looking for, thanks alot!
You're welcome dude.
Topic archived. No new replies allowed.