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

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++ Vectors
Click For Summary

Discussion Overview

The discussion revolves around writing a for loop to populate a vector (or array) with a specified number of integers in C++ and Java. Participants explore the implementation details, variable declarations, and the necessity of printing results.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant shares a partial implementation of a for loop in C++ and expresses confusion about variable declarations for input.
  • Another participant suggests using "cin >> userGuesses[i];" to read integers into the vector.
  • There is a discussion about whether a count statement is necessary for printing results, with some participants believing it should always be printed for verification.
  • Questions arise about whether a constant is always needed to reference the number of values in a vector, with differing opinions on this point.
  • A participant presents a similar problem in Java and encounters a compilation error regarding variable declaration, seeking input on the issue.
  • Another participant suggests that the error is due to declaring the loop variable 'i' twice and recommends removing the 'int' keyword from the for loop declaration.

Areas of Agreement / Disagreement

Participants express differing views on the necessity of printing results and whether a constant is required for vector size. The Java-related issue also highlights a lack of consensus on best practices for variable declarations.

Contextual Notes

Some participants mention the need for clarity in code structure, such as the use of braces in loops, but there is no consensus on whether they are strictly necessary.

ineedhelpnow
Messages
649
Reaction score
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
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)
 
i don't know if i said this before but ILU ILS! :o question tho...why no count statement? (Thinking)
 
ineedhelpnow said:
i don't know if i said this before but ILU ILS! :o question tho...why no count statement? (Thinking)

We only use count if we want to print something.
 
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. :o
 
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. :o

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 count.
 
when doing vectors will there always be a constant in the program referencing the number of values?
 
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.
 
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
1
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K