Incrementing array indexes in for loop.



 DEVELOP > c-Plus-Plus > Incrementing array indexes in for loop.

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Roger"
Date: 24 Aug 2007 11:16:03 PM
Object: Incrementing array indexes in for loop.
Is it possible to increment the array index in a for loop? I want to
make a program where the user inputs numbers individually and then
store them into an array. For each integer input, the array index will
increment to store the number.
Is this possible? I'm thinking of ArrayList in Java where the array
can dynamically expand, does C++ have anything like that?
make the arrray
for loop
print a question asking user for input
user inputs (if user inputs 50, then the loop exits)
inputted number stored into the array
array index is incremented
end loop
.

User: "Jim Langston"

Title: Re: Incrementing array indexes in for loop. 25 Aug 2007 01:35:11 AM
"Roger" <rogerngo@gmail.com> wrote in message
news:1188015363.864344.139830@e9g2000prf.googlegroups.com...

Is it possible to increment the array index in a for loop? I want to
make a program where the user inputs numbers individually and then
store them into an array. For each integer input, the array index will
increment to store the number.

Is this possible? I'm thinking of ArrayList in Java where the array
can dynamically expand, does C++ have anything like that?

make the arrray
for loop
print a question asking user for input
user inputs (if user inputs 50, then the loop exits)
inputted number stored into the array
array index is incremented
end loop

Best use is std::vector.
Untested code:
#include <vector>
#include <iostream>
int main()
{
std::vector Data;
int Number;
while ( std::cin >> Number && Number != 50 )
Data.push_back( Number );
return 0;
}
.
User: "Jim Langston"

Title: Re: Incrementing array indexes in for loop. 25 Aug 2007 01:36:07 AM
"Jim Langston" <tazmaster@rocketmail.com> wrote in message
news:B4Qzi.1726$qF4.1602@newsfe05.lga...

"Roger" <rogerngo@gmail.com> wrote in message
news:1188015363.864344.139830@e9g2000prf.googlegroups.com...

Is it possible to increment the array index in a for loop? I want to
make a program where the user inputs numbers individually and then
store them into an array. For each integer input, the array index will
increment to store the number.

Is this possible? I'm thinking of ArrayList in Java where the array
can dynamically expand, does C++ have anything like that?

make the arrray
for loop
print a question asking user for input
user inputs (if user inputs 50, then the loop exits)
inputted number stored into the array
array index is incremented
end loop


Best use is std::vector.
Untested code:

#include <vector>
#include <iostream>

int main()
{
std::vector Data;

std::vector<int> Data;
Told you it was untested :/ My bad.


int Number;
while ( std::cin >> Number && Number != 50 )
Data.push_back( Number );

return 0;
}

.
User: "Roger"

Title: Re: Incrementing array indexes in for loop. 25 Aug 2007 07:20:45 PM
On Aug 24, 11:36 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:

"Jim Langston" <tazmas...@rocketmail.com> wrote in message

news:B4Qzi.1726$qF4.1602@newsfe05.lga...



"Roger" <roger...@gmail.com> wrote in message
news:1188015363.864344.139830@e9g2000prf.googlegroups.com...

Is it possible to increment the array index in a for loop? I want to
make a program where the user inputs numbers individually and then
store them into an array. For each integer input, the array index will
increment to store the number.


Is this possible? I'm thinking of ArrayList in Java where the array
can dynamically expand, does C++ have anything like that?


make the arrray
for loop
print a question asking user for input
user inputs (if user inputs 50, then the loop exits)
inputted number stored into the array
array index is incremented
end loop


Best use is std::vector.
Untested code:


#include <vector>
#include <iostream>


int main()
{
std::vector Data;


std::vector<int> Data;

Told you it was untested :/ My bad.



int Number;
while ( std::cin >> Number && Number != 50 )
Data.push_back( Number );


return 0;
}

How would I output the contents of the vector onto the screen?
.
User: "John Harrison"

Title: Re: Incrementing array indexes in for loop. 26 Aug 2007 03:18:59 AM


int Number;
while ( std::cin >> Number && Number != 50 )
Data.push_back( Number );
return 0;
}


How would I output the contents of the vector onto the screen?

The traditional approach works too
for (int i = 0; i < Data.size(); ++i)
cout << Data[i] << '\n';
john
.

User: "Jerry Coffin"

Title: Re: Incrementing array indexes in for loop. 25 Aug 2007 09:57:36 PM
In article <1188087645.984164.52590@x40g2000prg.googlegroups.com>,
rogerngo@gmail.com says...
[ ... ]

int Number;
while ( std::cin >> Number && Number != 50 )
Data.push_back( Number );


return 0;
}


How would I output the contents of the vector onto the screen?

There are quite a few ways. Here's one possibility:
std::copy(Data.begin(), Data.end(),
std::ostream_iterator<int>(std::cout, "\t"));
The "\t" governs what's used to terminate each item being copied -- if
(for example) you wanted each number on its own line, you could use
"\n" instead.
--
Later,
Jerry.
The universe is a figment of its own imagination.
.

User: "Chris Sorenson"

Title: Re: Incrementing array indexes in for loop. 25 Aug 2007 09:14:17 PM
Roger wrote:


How would I output the contents of the vector onto the screen?

std::vector<int>const_iterator int_iter;
for (int_iter = Data.begin(); int_iter != Data.end(); int_iter++)
std::cout << (*int_iter) << std::endl;
(don't forget to #include <iostream>)
.




User: "Aggro"

Title: Re: Incrementing array indexes in for loop. 25 Aug 2007 12:11:43 AM
Roger wrote:

Is it possible to increment the array index in a for loop? I want to
make a program where the user inputs numbers individually and then
store them into an array. For each integer input, the array index will
increment to store the number.

Is this possible? I'm thinking of ArrayList in Java where the array
can dynamically expand, does C++ have anything like that?

#include <vector>
std::vector<int> storage;
storage.push_back( 1 );
storage.push_back( 2 );
storage.push_back( 3 );
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER