Program has stopped working

I need to create a program that generates 20 random values and stores them in an array. Then adds those values in a separate function. My program stops working when it gets to the function that is supposed to add all of the values together and return a value. Having an issue finding where the error is in the function.

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;



void randNums(int randomArray[], int i);
float gettotal(int (&randomArray)[20], int i );


int main(){
	srand(time(0));
	int i = 0;
	const int size = 20; 
	int randomArray[size];
	
	
	randNums( randomArray, i);
	
	 
	// just to test i am getting random numbers
	cout << randomArray[1] << endl;
	cout << randomArray[2] << endl;
	cout << randomArray[20] << endl;
	
	
	float total = gettotal( (&randomArray)[20], i );
	//this is where my program stops
	// test to see the same random numbers are being added
	cout << randomArray[1] + randomArray[2] << endl;






return 0;
}
void randNums(int randomArray[], int i ){
			
  
		for(i= 0; i < 20; i++){
		
			randomArray[i] = (rand() % 100);
                  
              }
       }
       
float gettotal(int (&randomArray)[20], int i ){
	
	float total = 0;
	
	for(i= 0; i < 20; i++){
		
		total = total + randomArray[i];
		
		
		}
	return total;
}
Last edited on
In what way does it not work?

Your function randNums writes to randomArray[20], which is really bad because the last element is randomArray[19].
It gives me a program.exe has stopped working error message. A problem caused the program to stop working correctly. And only outputs up until the second function. I fixed the randNums function I believe and am still getting the error.
Your program works fine with me.

The output:

1
2
3
4
5
87
53
-858993460
140
Press any key to continue . . .


EDIT:

I fixed your code.

First, please don't pass 'i' to the functions, just do the for loop this way:

for(int i = 0; i < ......; i++)

There is really no point in passing i to a function.

And, you did a horrible mistake in line 28:

float total = gettotal( (&randomArray)[20], i );

Here, you're passing the 20th element of the array to the function getTotal.

You want to pass the WHOLE array to the function, so just pass its name.

It will be like this: float total = gettotal( (randomArray), i );

Here is the working code:

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
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void randNums(int randomArray[20])
{
	for (int i = 0; i < 19; i++)
	{

		randomArray[i] = (rand() % 100);
	}
}

float gettotal(int randomArray[20])
{

	float total = 0;

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

		total = total + randomArray[i];
	}
	return total;
}


int main()
{
	srand(time(0));
	const int size = 20;
	int randomArray[size];

	randNums(randomArray);

	cout << "Random numbers are: ";
	for (int i = 0; i < 19; i++)
	{
		cout << randomArray[i] << ", ";
	}

	float total = gettotal((randomArray));

	cout << "Your total is: " << total << endl; 

	system("PAUSE");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.