Completely lost Arrays

I have an assignment that requires me to calculate and print the sum, mean, variance, and standard deviation of a set of numbers.

This is the sample code given by my professor, I need to add the list of numbers in array 12,22,34,43,45,54,99

//average2.cpp

// This program calculates the average of any number of numbers.

// Using the for structure

#include <iostream>

using namespace std;



#include <iostream>



int main()

{

int n, count;

float x, sum, avg;



sum = 0;

cout << "How many numbers? ";

cin >> n;

for (count=1; count<=n; count++){

cout << "? ";

cin >> x;

sum = sum + x;

} //end for

avg = sum / n;

cout << "The average is " << avg << endl;

return 0; //successful termination

} //end main
Hey, you should do a little search on arrays, it's a pretty easy concept.

int numbers [] = {12,22,34,43,45,54,99};

The subindex starts at 0,1,2,3,4,5,6.

To call 12 for example: numbers[0];

That should help you a bit.
Last edited on
Why are you including <iostream> twice? It won't do any harm in this case since it has include guards, but it's a bad habit. ;)
Last edited on
If you're really interested in learning C++, you should look for alternatives (that is, a good book), as this sample code indicates that your professor is not very competent in C++.

What am i doing wrong, do I need to add another loop





//average2.cpp

// This program calculates the average of any number of numbers.

// Using the for structure

#include <iostream>
#include "math.h"

using namespace std;





int main()

{

int n [7] = {12, 22, 34, 43, 45, 54, 99};

float x, sum, avg;



sum = 0;

cout << "How many numbers? ";

cin >> ;

for (count=1; count<=n; count++){

cout << "? ";

cin >> x;

sum = sum + x;

} //end for

avg = sum / n;

cout << "The average is " << avg << endl;

return 0; //successful termination

} //end main
I formatted your code for you. You should put your code [code]inside code tags[/code] for better readability.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	int n [7] = {12, 22, 34, 43, 45, 54, 99};
	float x, sum, avg;

	sum = 0;
	cout << "How many numbers? "; //You already have the numbers in your n array, why are you doing this??
	cin >> ; //This is obviously wrong

	for (count=1; count<=n; count++)
	{
		cout << "? "; //This is not needed, because ----v
		cin >> x; //Again, you already have the numbers. Don't ask the user for numbers you already have!
		sum = sum + x; //Shouldn't you be adding up the numbers in your n array?
	}

	avg = sum / n; //You can't divide by an array like this!
	cout << "The average is " << avg << endl;

	return 0; //successful termination
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

using namespace std;

int main()
{
	float InputData[100];   //array to hold all of the data being input
	int numOfInputs = 0;  //A variable that holds how many inputs there will be

	cout << "How many numbers will be entered? : ";
	cin >> numOfInputs; //give numOfInputs the amount of numbers that will be input

	//Make sure that numOfInputs isn't greater than 100, or less than 1.
	//Checking to make sure that your values are in the appropriate range is always
	//good practice.
	if(numOfInputs > 100 || numOfInputs < 1)
	{
		//if the number is out of range, tell the user, and exit the program.
		cout << "You entered an invalid number" << endl;
		return 1;
	}

	//Now we need to assign data to our array. You do this almost the same way
	//you did originally
	for(int i = 0; i < numOfInputs; ++i)
	{
		//Each time a for loop runs, the variable 'i' will increment.
		//because of this, we can use it as our subindex (or position) that our
		//data will be assigned to.
		cin >> InputData[i];
	}

	//After you get all the data.. well the rest seems obvious. Do the math..
	float sum = 0;
	for(int i = 0; i < numOfInputs; ++i)
	{
		sum += InputData[i]; //add all of the data to sum
	}

	float average = sum / numOfInputs;

	return 0;
}



Well.. I typed a really detailed explanation to this, but i refreshed my page and now its all gone.. fail.
I'm gonna forward you to this page about arrays. read it, love it. http://cplusplus.com/doc/tutorial/arrays/

The program above is a nice way to get an average using arrays, assuming a fixed number of inputs. You can further add on to that to meet your programs needs.
Last edited on
Thanks Guys I really appreciate it......aspecial thanks to Thumper for taking the time to explain
Topic archived. No new replies allowed.