Answer to Question #198117 in C++ for Shuvo

Question #198117

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. Finally swap the contents of gamesWon[0] and gamesWon[minIndex] and print the swapped values.


1
Expert's answer
2021-05-25T00:10:19-0400
#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 minIndex = 0;
	for (int i = 0; i < n; i++)
	{
		if (gamesWon[minIndex] > gamesWon[i]) {
		    minIndex = i;
		}
		cout << gamesWon[i]<< " ";
	}
//swap the contents of gamesWon[0] and gamesWon[minIndex] and print the swapped value
    int temp2=gamesWon[0];
    gamesWon[0]=gamesWon[minIndex];
    gamesWon[minIndex]=temp2;
    
    cout<<"\nThe swapped vaues are: \n";
    cout<<"GamesWon[0]: "<<gamesWon[0]<<endl;
    cout<<"GamesWon[minIndex]: "<<gamesWon[minIndex]<<endl;
	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