Get weird numbers when finding the sum

Just posted a different problem earlier today but now its something else. when I try to find the sum of the array I get random numbers that are obviously wrong any help would be appreciated.

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
#include <iostream>

using namespace std;

void mean(int [],int);

int main()
{

 const int arraySize = 25;
 const int patternSize = 10;

 

int* arr1 = new int[arraySize];

arr1[0]= 0;
arr1[1]= 1;
arr1[2]= 2;
arr1[3]= 3;
arr1[4]= 4;
arr1[5]= 5;
arr1[6]= 6;
arr1[7]= 7;
arr1[8]= 8;
arr1[9]= 9;

 

 for(int i=0; i<arraySize; i++)
 {	 
	 cout << arr1[i%patternSize] << " ";

 }

 mean(arr1, arraySize);

	delete[] arr1;

	system("PAUSE");
	return 0;
}

void mean(int arr1[], int arraySize)
{
	int sum=0;
	int mea;

	for(int i=0; i<25; i++){
		sum += arr1[i];
		cout << sum << endl;
	}
	
}
Last edited on
You fill the array ony up to the 10th element. On line 50 you are adding all the values, even the uninitialized ones.
Your array has a size of 25, and you did not initialize any of the elements. You assign numbers for the first ten indices. Then you proceed to sum all 25 numbers, including the uninitialized elements which will probably hold garbage data.
Topic archived. No new replies allowed.