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.Find and print the index of the element where the smallest value is held.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int gamesWon[100];
int n = 0;
string temp;
cout << "Enter number of games won. Enter Q to exit" << endl;
while (true)
{
cout << "\nEnter num " <<n + 1 << ": ";
cin >> temp;
if (temp == "Q") {
break;
}
gamesWon[n] = stoi(temp);
n++;
}
cout << "Entered array: ";
int min = gamesWon[0];
for (int i = 0; i < n; i++)
{
if (min > gamesWon[i]) {
min = gamesWon[i];
}
cout << gamesWon[i] << " ";
}
cout << "\nThe smallest number of games won in a tournament by the player is: " << min << endl;
return 0;
}
Comments
Leave a comment