136k views
1 vote
Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using cin. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}. for C++

User Brad Parks
by
4.5k points

1 Answer

1 vote

Answer:

Follows are the code to this question:

#include <iostream>//defining header file

using namespace std;

int main() //defining main method

{

const int NUM_GUESSES = 3;//defining an integer constant variable NUM_GUESSES

int userGuesses[NUM_GUESSES];//defining an array userGuesses

int i = 0;//defining integer variable i

for(i=0;i<NUM_GUESSES;i++)//defining for loop for input value

{

cin>>userGuesses[i];//input array from the user end

}

for (i = 0; i < NUM_GUESSES; ++i)//defining for loop fore print value

{

cout <<userGuesses[i] << " ";//print array value with a space

}

return 0;

}

Output:

9

5

2

9 5 2

Explanation:

In the above-given program, an integer constant variable NUM_GUESSES is defined that holds an integer value, in the next step, an integer array "userGuesses" is defined, which holds NUM_GUESSES value.

In the next step, two for loop is defined, in the first loop, it is used for an input value, and in the second loop, it prints the array value.

User Sravan
by
4.7k points