this is what it should look like, a while loop that repeatedly reads goals,assists, stops when a negative is entered. at the end it calcuates sum, average. donot know how to calcuate sum or average if i dont know how many times the user will enter.

Enter a number of goals: 0
Enter a number of assists: 0

Enter a number of goals: 1
Enter a number of assists: 0

Enter a number of goals: 2
Enter a number of assists: 1

Enter a number of goals: 0
Enter a number of assists: 3

Enter a number of goals: 4
Enter a number of assists: 1

Enter a number of goals: 0
Enter a number of assists: 0

Enter a number of goals: 2
Enter a number of assists: -3

Total Goals: 7
Total Assists: 5
Average Goals: 1.16667
Average Assists: 0.833333
Yes, there was a hat trick!

Recommended Answers

All 3 Replies

which programming language do you prefer?

in c++ I would use std::vector to keep the numbers, and sum them when breaking from the while loop.

#include <vector>

int ccmain()
{
    std::vector<int> all_goals;
    std::vector<int> all_assists;

    int goals = 0;
    int assists = 0;
    while (goals >= 0 && assists >= 0)
    {

//      ... some code for getting the numbers ...
        all_goals.push_back(goals);
        all_assists.push_back(assists);
    }

    int sum_of_elems = 0;
    for (std::vector<int>::iterator j = all_assists.begin(); j != all_assists.end(); j++)
        sum_of_elems += *j;

    return 0;
}

I wouldn't.
Simply increase a counter by one each time a valid number is entered and add this value to a variable sum. After the loop you can divide the sum by the counter and you get your result.
This will be faster and lighter on ram.

What language are you using?

Whichever language you are using, I agree with "sDJh": just use a counter and increment it each time you input a pair of numbers. You already have one loop to input the data, so use that loop. Creating a second loop just to count the entries in the array is unnecessary.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.