Need Help Finishing a Program

I'm so close in finishing, could someone figure out what I can't ~
All the coding is there, but need to get it to work without crashing.. Needs to show the average, variance, mean, etc...

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<iostream>

using namespace std;

	// Prototypes
	int getArray(int[]);
	int calculateAverage(int[], int);
	int variance(int, int, int);
	void display(int[], int, int);

int main()
{
	int numArray[10];												// user can enter up to 10 numbers
	int variance;													// adding numbers
	int arrayLength;
	int total;
	int average;

	cout << "*~~**~~**~~**~~**~~**~ Simple Statistical Analysis ~**~~**~~**~~**~~*~~**~~*" << endl << endl;
	cout << "	Max of numbers being entered is 10, -1 will end entering numbers" << endl << endl;


	arrayLength = getArray(numArray);								// read in array from user
	total = calculateAverage(numArray, arrayLength);				// get total of numbers
	display(numArray, variance, total);							// show values and results

	
	system("pause");
	return 0;
}

	int getArray(int numArray[])	
		// Purpose: To ask for Numbers
		// Parameters: NumArray - up to 10
		// Return: N/A
	{
		int num = 0;
		int numofScores = 0;

		cout << "		Enter Number: "; 
		cin >> num; 

		while ((num != -1) && (numofScores < 10))
		{
			numArray[numofScores] = num;
			numofScores++;
		
			if (numofScores < 10)
			{
			cout << "		Enter Number: "; 
			cin >> num; 
			}
		}	
		return numofScores;
	}

		int calculateAverage(int numArray[], int arrayLength)
		// Purpose: To calucate the average of numbers given
		// Parameters: Average
		// Return: Sum of all Numbers
	{
		int num = 0;
		int numofScores = 0;
		int array[10];
		int average;
		int times;

		for (numofScores = 0; numofScores < num; ++numofScores)
		{
		num += array[numofScores]; 
		}

		average = num / arrayLength;

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

		cout << endl;
		return num;
	}



	int variance(int num[], int length, int mean)
		// Purpose: To calucate the average of numbers given
		// Parameters: Average
		// Return: Sum of all Numbers
	{
		int variance = 0;
		
	variance = ((num[0]-mean)*(num[0]-mean)+(num[1]-mean)*(num[1]-mean)+(num[2]-mean)*(num[2]-mean)+(num[3]-mean)*(num[3]-mean)+
		(num[4]-mean)*(num[4]-mean)+(num[5]-mean)*(num[5]-mean)+(num[6]-mean)*(num[6]-mean)+(num[7]-mean)*(num[7]-mean)+(num[8]-mean)*(num[8]-mean)
		+(num[9]-mean)*(num[9]-mean)) / 7;

	return variance;

	}

	void display(int numArray[], int average, int variance)
		// Purpose: Writes out the items wthin the array and the sum
		// Parameters: Accepts an array of integars
		// Return: Main 
	{
		int num = 0;
		int numofScores = 0;
		int arrayLength;

		cout << "Variance: " << variance << endl;
		cout << "Average: " << average << endl;
		cout << "Array: "; 

		for(numofScores = 0; numofScores < arrayLength; ++numofScores)
		{
			num = numArray[numofScores];
			cout << num << " , ";
		}

		return;
	}

	// 		cout << "Length: " << arrayLength << endl; 
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
int GetTotal(int [], int);
double GetAverage(int [], int);
double GetVariance(int [], int);

//Definitions
int GetTotal(int array[], int length)
{
   int total = 0;

   for(int i = 0; i < length; i++)
      total += array[i];

   return total;
}

double GetAverage(int array[], int length)
{
   return GetTotal(array, length) / length;
}

double GetVariance(int array[], int length)
{
   double average = GetAverage(array, length);
   double variance = 0;

   for(int i = 0; i < length; i++)
      variance += pow(array[0] - average, 2);

   return variance;
};


As you're not allowed to use pointers/references/templates, this would fit the bill.

There are many issues you'd have with your code, such as, returning an average as an integer. This would make that number rather meaningless, especially for large quantities of values. An integer just holds a whole number, without the fractional part.

double 10 / 3 == 3.3333....
int 10 / 3 == 3
Last edited on
This was due last week, so I'm way behind in work. Our teacher gave us that line to write out, I'll just write out the question to make it easier...

"Write a program that will read in up to 10 numbers, ending with -1. After the numbers have been read in, your program should compute and display their mean and variance.

Mean: Adding up all the numbers and divide by how many there are. This is what you'd normally call the "average."

Variance: The variance is obtained by adding together the squares of the differences of each element and the mean. This total is then divided by the number of numbers (n). So, if the numbers were in X[0] through X[6]:

variance = (X[0] - mean) [b](basically what is written in line 90)[/b]

Your program should used at lteast three functions: one to get the array of data values from the user, and one each for calculating the mean and variance. Of course, you'll need to pass the array of values to these functions so they can do the computations."
I'll update my previous post, got variance to go.

I think you need to really think about the way you should go about coding your assignments, if you ever got your code working, you'd probably be marked down for various reasons.
Last edited on
What is suppose to be removed from the code if that's added ?
I received a bunch of errors~
It's a beginners class and my teacher knows I'm the dumbest one there to put it bluntly in this subject.
I should have tested it, check that above code now, It's fixed.

Remember to: #include<math.h>
We haven't learned " #include<math.h> " so I can't use it. Only know " #include<ctime> "
I handed the work as is, I didn't understand any help but thanks for trying!
Topic archived. No new replies allowed.