# Writing Vectors

Page 1 of 1

## 1 Replies - 1902 Views - Last Post: 26 October 2006 - 10:04 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=20124&amp;s=a6c811daa61d744ba63c8374c8682bed&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 sknox1

Reputation: 0
• Posts: 29
• Joined: 30-September 06

# Writing Vectors

Posted 26 October 2006 - 08:59 AM

```#include<iostream>
#include <iomanip>
#include<vector>
#include "d_util.h"

using namespace std;

template <typename T>
void writeVector(const vector<T>& v);

int main()
{

vector<int>v;

for (int n=0; n<=9; n++)

{
printf("A number between 0 and 9: %d\n", rand()%9);;

v.push_back(n);

n = v.size();

for(int i = 0; i < n; i++)
cout << v[i] << "  ";
cout << endl;
}
writeVector(v);
return 0;
}

```

This program runs and executes perfectly, however i need v.push_back to push back the random numbers and the writeVector to write the random numbers.

can anybody point in that direction.

This post has been edited by Videege: 26 October 2006 - 09:54 AM

Is This A Good Question/Topic? 0

## Replies To: Writing Vectors

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Writing Vectors

Posted 26 October 2006 - 10:04 AM

Do you mean you want to push the random number onto the stack? As it stands now, your simply pushing the loop iteration number on. You need to push the correct value...something like:
```var = rand()%9;
v.push_back(var);

```

http://www.cplusplus...tdlib/rand.html
http://www.cplusplus...dlib/srand.html

And do you want the writeVector method to write the the numbers in a random sense, or in the order you push them? Another potential problem is that you are using n as both a counter variable for the first loop, then assigning it the value of the vector size and using it in the second loop. Finally, you've not seeded the rand function (see links), which you'll need to do for pseudorandomness.