Program keeps crashing

Hello everyone, new c++ programmer here and my program keeps crashing when I run it. There are no errors so I'm completely lost. Anyways I'll give you the instructions I got on how to make this program and what I came up with. Really hope you can help me.

Write a method that calculates the average test score in an array of integer values using the following header:
public static int scores(int [ ] array)
Write a test program that prompts the user to enter ten test scores, invoke this method to return the average test score, and display the average test score


And here is my code:

#include <iostream>
using namespace std;

int static scores(int[], int);

int main()
{
const int ARRAY_SIZE = 10; // Array Size
int getScores[ARRAY_SIZE];
int average;

for (int count = 0; count < ARRAY_SIZE; count++)
{
cout << "Please enter test score "
<< (count + 1) << ": ";
cin >> getScores[count];
}

average = scores(getScores, ARRAY_SIZE);

cout << "Your average test score is " << average;


// Gets the 10 test scores from the user

return 0;
}


//**********************************************//
// *********Definition to get average**********//

int static scores(int scores[], int size)
{
int total = 0;
int average = 0;
for (int count = 0; count < size; count++)
{
total += scores[count];
average = (total / average);
}

return average;
}
Ok I figuring out why it was crashing.
average = (total / average);
should have been
average = (total / size);

But now I don't think its finding the average correctly. Here is my code after cleaning it up better:


#include <iostream>
using namespace std;

int static scores(int[], int); // Function Prototype

int main()
{
const int ARRAY_SIZE = 10; // Array Size
int getScores[ARRAY_SIZE]; // Array
int average;

// Prompts user to enter test scores
for (int count = 0; count < ARRAY_SIZE; count++)
{
cout << "Please enter test score "
<< (count + 1) << ": ";
cin >> getScores[count];
}

// Calling function
average = scores(getScores, ARRAY_SIZE);

// Displays average
cout << "Your average test score is " << average << endl;

return 0;
}


//**********************************************//
// *********Definition to get average**********//

int static scores(int scores[], int size)
{
int total = 0;
int average = 0;
for (int count = 0; count < size; count++)
{
total += scores[count];
average = (total / size);
}

return average;
}
Do you mean for average to be int? If not change your return type to double and change
int average = 0
to
double average = 0.0;
There's no need to repeatedly calculate average inside the loop:
1
2
3
4
5
for (int count = 0; count < size; count++)
{
    total += scores[count];
    average = (total / size); // no need to do this over  and over again
}


It should be calculated just once after the loop has completed.

Also, both total and size are type int, so integer division will be done.
Use int for size and count.
Use double for total, average and the return type of the function (declared in the function header).
Topic archived. No new replies allowed.