Answer to Question #195825 in C++ for Shuvo

Question #195825

Create an int array called gamesWon[] of capacity 100, that stores the number of games won in different chess tournaments in the last two years. and fill n elements of this array by prompting a player to enter data and ask to end the data by entering Q. You use a while loop to read the user entered data and assign to the elements of the array. So it is important to use a new variable and initialize it to 0 and use as the index of the array as well. The value of this variable when the sentinel value is read is the actual size of the array. Now you have a partially filled array. Print the array you created using the variable you used as index variable as the size of the array.

1. Compute and print the average number of games won by the player by find the sum of all numbers and dividing by the actual size of the array. The actual size is the actual number of elements filled. We work with that size in a partially filled data.


1
Expert's answer
2021-05-20T00:31:27-0400
#include <iostream>
#include <string>

using namespace std;

void printArray(int *arr, int n) 
{
    cout << "gamesWon=[";
    for (int i = 0; i < n; i++) {
        cout << arr[i];
        if (i < n - 1) cout << ",";
    }
    cout << "]\n";
}

int sumOfArray(int *arr, int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    return sum;
}

int main()
{
    int gamesWon[100];
    int n = 0;
    while (true) {
        string x;
        cin >> x;
        if (x != "Q") gamesWon[n++] = stoi(x);
        else break;
    }
    printArray(gamesWon, n);
    
    cout << "avg=" << sumOfArray(gamesWon, n) / n;
    
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog