[try Beta version]
Not logged in

 
vector with 100 ints

Apr 25, 2014 at 5:59am
Write a program to create a vector of 100 ints. Fill it with 100 small, random
integers between 1 and 100, inclusive, by using an appropriate call from the
math library. Then display the contents of the vector. (Your program should
use rand() and the modulo operator, at least.)..

........................................................................




#include <vector>

#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
float f;
vector<string> list(100);
for( int i=0;i<100;i++)
{


f=rand() % 10;

list[i]=f;
}

for(i=0;i<100;i++)
cout<<"your 100random number are"<<endl;


cout<< list[i];
return 0;
}



can someone see what wrong in this code.??thkss
Apr 25, 2014 at 6:49am
f is not an int. list is not a vector<int>. cout<<list[i] is outside the loop that defines i.
Apr 25, 2014 at 6:52am
#include <vector>

#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
int f;
vector<int> list(100);
for( int i=0;i<100;i++)



f=rand() % 10;

list[i]=f;


for(i=0;i<100;i++)
cout<<"your 100random number are"<<endl;


cout<< list[i];
return 0;
}


this one either is nt working
Apr 25, 2014 at 7:04am
list[i]=f; is outside the loop that defines i.
cout<<list[i] is outside the loop that defines i.

You need to enclose the sections of code you want the for loops to loop over with curly braces { }
Apr 25, 2014 at 7:14am
#include <vector>

#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
int f;
vector<int> list(100);

for( int i=0;i<100;i++)

{


f=rand() % 10;

list[i]=f;


for(i=0;i<100;i++)
cout<<"your 100random number are"<<endl;


cout<< list[i];
}
return 0;

}

it is stil nt good... it is nt display as it shud though it is compiling
Apr 25, 2014 at 7:18am
Would you like us to code it for you, or are you going to do some actual work yourself?
Apr 25, 2014 at 7:18am
I think you need to try a little harder...

You didn't even fix the errors that they mentioned.

You need to brace-enclose the for loop if you want multiple things.
Here is an example:

1
2
3
4
5
6
for(int i = 0; i < 100; ++i)
{
    //do something
    //do something else
    //do something else
}
Apr 25, 2014 at 7:40am
Also when posting code enclose it in code tags
[code] /*Code goes here*/ [/code]
Apr 25, 2014 at 7:41am
i have solve it.. in fact the 2nd for loop was nt necessary...
thks guys...
Topic archived. No new replies allowed.