How to Populate a Vector Using a For Loop in C++?

  • C/C++
  • Thread starter ineedhelpnow
  • Start date
  • Tags
    C++ Vectors
In summary: Then you are using the same variable i in both the declaration and the for loop.Edit: To clarify, remove "int" from the for loop so it is:for (i=0;i<NUM_GUESSES;i++) { // something}In summary, to populate an array with a specified number of integers, a for loop can be used with the help of a constant or variable to specify the number of iterations. In C++, the vector data structure can be used to store the integers, while in Java, an array can be used. The for loop will read in the integers using cin or Scanner, and store them in the data structure one by one. Extra caution should be taken to avoid re-defining variables in
  • #1
ineedhelpnow
651
0
ok I am working on some activities and i can't get past this one.
Write a for loop to populate vector 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}.

Sample program:

Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int NUM_GUESSES = 3;             
   vector<int> userGuesses(NUM_GUESSES); 
   int i = 0;                         

   <STUDENT CODE>

   return 0;
}

this is all i got so far
Code:
for(i=0;i<NUM_GUESSES;++i)

now I am stuck (Blush). do i need to define any new variables? (Thinking) like "x" or something. a variable for the integers entered? :confused:
 
Technology news on Phys.org
  • #2
ineedhelpnow said:
ok I am working on some activities and i can't get past this one.
Write a for loop to populate vector 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}.

Sample program:

Code:
#include <iostream>
#include <vector>
using namespace std;

int main() {
   const int NUM_GUESSES = 3;             
   vector<int> userGuesses(NUM_GUESSES); 
   int i = 0;                         

   <STUDENT CODE>

   return 0;
}

this is all i got so far
Code:
for(i=0;i<NUM_GUESSES;++i)

now I am stuck (Blush). do i need to define any new variables? (Thinking) like "x" or something. a variable for the integers entered? :confused:

No need.
Try [m]cin >> userGuesses;[/m]
(Nerd)
 
  • #3
i don't know if i said this before but ILU ILS! :eek: question tho...why no cout statement? (Thinking)
 
  • #4
ineedhelpnow said:
i don't know if i said this before but ILU ILS! :eek: question tho...why no cout statement? (Thinking)

We only use cout if we want to print something.
 
  • #5
yeah. shouldn't the statement be printed?
i thought it always had to be printed (Blush)
i guess this is the first example i had where it wasnt printed...sorry. :eek:
 
  • #6
ineedhelpnow said:
yeah. shouldn't the statement be printed?
i thought it always had to be printed (Blush)
i guess this is the first example i had where it wasnt printed...sorry. :eek:

Well... the problem statement doesn't ask for anything to be printed...

But yeah, it does make sense to print the result to verify if the program worked properly.
So then I suggest to put in another for-loop in which the values in the vector are printed with cout.
 
  • #7
when doing vectors will there always be a constant in the program referencing the number of values?
 
  • #8
ineedhelpnow said:
when doing vectors will there always be a constant in the program referencing the number of values?

Nope. There won't be.
We can read as many values as there are on the input, and put them in the vector one by one.
Afterwards, we can "ask" the vector how many values are in there.
 
  • #9
Same Question but using Java. Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using Scanner. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.

I have :

for(int i =0;i<NUM_GUESSES;i++)
userGuesses = scnr.nextInt();

output:

StoreGuesses.java:10: i is already defined in main(java.lang.String[])
for(int i =0;i

Any input would help, thank you :)
 
  • #10
dinklebuurg said:
Same Question but using Java. Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using Scanner. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.

I have :

for(int i =0;i<NUM_GUESSES;i++)
userGuesses = scnr.nextInt();

output:

StoreGuesses.java:10: i is already defined in main(java.lang.String[])
for(int i =0;i

Any input would help, thank you :)
I'm sorry, here's the full code if it might help. My input is above.
 
  • #11
It looks like you need to define [m]NUM_GUESSES[/m] first, and your for loop needs braces, doesn't it (I assume since the languages I use all require them).

edit: Perhaps you don't need braces since the body of the loop is one statement. I tend to always use them for clarity. :)
 
  • #12
dinklebuurg,
From the error message, I'm close to 100% certain your main looks something like:
Code:
int i;
for (int i=0;i<NUM_GUESSES;i++) {
  // something
}
The Java compiler doesn't like your to declare variable i twice. In the for loop when you say int i, you are declaring i as well as initializing it to 0. The above code declares i before the for loop and so i is declared twice. So just remove the word int in your for loop.
 

FAQ: How to Populate a Vector Using a For Loop in C++?

What is a vector in C++?

A vector in C++ is a data structure that stores a sequence of elements in a contiguous block of memory. It is similar to an array, but with additional functionalities such as dynamic resizing and insertion/deletion of elements at any position.

How do I declare and initialize a vector in C++?

To declare and initialize a vector in C++, you can use the following syntax:

vector<data_type> variable_name; //declaration

vector<data_type> variable_name = {element1, element2, element3}; //initialization

How do I access and modify elements in a vector?

You can access and modify elements in a vector using the square bracket notation, similar to arrays. For example:

vector<int> numbers = {1, 2, 3, 4, 5};

int first_element = numbers[0]; //access first element

numbers[3] = 10; //modify fourth element

Can I use vectors to store different data types in C++?

Yes, vectors in C++ can store elements of any data type, as long as they are of the same type. For example, you can have a vector of integers, a vector of strings, or a vector of custom objects.

How do I iterate through a vector in C++?

You can iterate through a vector in C++ using a for loop or a range-based for loop. For example:

vector<int> numbers = {1, 2, 3, 4, 5};

for (int i = 0; i < numbers.size(); i++) { //iterate using index

cout << numbers[i] << " ";

}

for (int num : numbers) { //iterate using range-based for loop

cout << num << " ";

Similar threads

Replies
2
Views
5K
Replies
22
Views
3K
Replies
5
Views
2K
Replies
1
Views
1K
Back
Top