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.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
void randNums(int randomArray[], int i);
float gettotal(int (&randomArray)[20], int i );
int main(){
srand(time(0));
int i = 0;
constint 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;
}
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.