Write a program that initializes a vector with the following array values.
int arr[] = {1, 6, 2, 9, 12, 15, 33, 28};
Compute the average value, and then output each value along with its deviation (+/‐) from
the average.

What i have so far is only the average...

#include <iostream>
#include <conio.h>
using namespace std;


int main()
{
double sum = 0;
double average = 0;

double array[ ] = {1, 6, 2, 9, 12, 15, 33, 28};

for (int i = 0; i < 8; i++)
sum+= array[i];
average = sum/8;
cout << "Average:" << average << endl;
}

But i can't figure out the second part... to do the deviation of each number from the array and figure out how far it is from the average...
I'm really new to this and any assistance would be great!!!
Thanks

Now that you have the average, you again loop through the array, comparing each element to that average.

I understand what your saying, but i still can't figure it out... thanks for your time tho :)

What's giving you problems?

You already have a loop that visits each element:

for (int i = 0; i < 8; i++)
    sum+= array[i];

A lot of writing programs is just taking something that's been done and using it a bit differently.

So make a copy of that loop and change the body to do the new task. You'll probably want to do that as a couple statements - one to find the difference between the current array value and the average, and one to display that difference. So, fill in the blanks:

for (int i = 0; i < 8; i++)
{


}

And, when you post your solution, please wrap it with the code tags, like:

[code]

your code goes here

[/code]
This will make it more readable.

I really dont want to sound stupid but i can't figure out how to reuse that loop statement to make it work to compare each element to the average... i've been looking in my text book and online and can't find a solution... sorry to be a nuisance... but again thanks alot you've been very helpful!!!

actually i think i got it!!! I'm not sure i'll let ya know!!!

Okay last time i'm bothering you... but i have as followed:

#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;


int main()
{
	const int arraySize = 8;
	int A[arraySize] = {1, 6, 2, 9, 12, 15, 33, 28};
	double sum = 0;
	double average = 0;
	double dev = 0;
	int i;
	
	cout << "The array is as follows:\n\n";
	cout << "Element #" << setw(20) << "Element Value" << endl;
	cout << "~~~~~~~~~" << setw(20) << "~~~~~~~~~~~~~" << endl;
	for (i = 0; i < arraySize; i++)
	cout << setw(9) << i << setw(20) << A[i] << endl;

	for (int i = 0; i < 8; i++)
		sum+= A[i];
	average = sum/8;
	
	cout << "\nThe Average of the Element Value is => " << average << endl;

	for (int i = 0; i < 8; i++)
	{
		dev+= A[i] + average;
			cout << "The deviation is ==> \n" << dev << endl;
	}

}

I had to switch some minor things around so it would print out better... but Am i close for the deviation part??? Really my question is how can i make the elements to differ from the positive or negative of the average...

Deviation usually implies subtraction. Subtract each element from the average (or vice versa).

I believe your assignment is to show how each array value differs from the average, so your summing of the differences is not what you want. Just display the difference (which will be + or - ) for each element.

What you're doing by accumulating a sum in the second loop is sort of on the path to calculating Standard Deviation of the whole set. Not part of your current problem, as I read it.

THANKS SOO MUCH!!! I misunderstood the question!!!
I took your advice and filled in the blank with...

dev= A[0] - average;
		cout << "\nThe deviation is => " << dev << endl;
		dev= A[1] - average;
		cout << "\nThe deviation is => " << dev << endl;
		dev= A[2] - average;
		cout << "\nThe deviation is => " << dev << endl;
		dev= A[3] - average;
		cout << "\nThe deviation is => " << dev << endl;
		dev= A[4] - average;
		cout << "\nThe deviation is => " << dev << endl;
		dev= A[5] - average;
		cout << "\nThe deviation is => " << dev << endl;
		dev= A[6] - average;
		cout << "\nThe deviation is => " << dev << endl;
		dev= A[7] - average;
		cout << "\nThe deviation is => " << dev << endl;

You've been soo helpul and thanks for being patient!!!!!!!

Ohhhh, that hurts!

You should put all that tedious code into a loop, so you have only one line that calculates the dev, then displays it. Actually, you can do all of that in just one line - do the calculation in the spot where you're displaying dev.

commented: Couldn't agree more +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.