Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,374 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,170 people online right now. Registration is fast and FREE... Join Now!




Writing Vectors

 
Reply to this topicStart new topic

Writing Vectors

sknox1
post 26 Oct, 2006 - 07:59 AM
Post #1


New D.I.C Head

Group Icon
Joined: 30 Sep, 2006
Posts: 29



Dream Kudos: 150
My Contributions


CODE


#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 Oct, 2006 - 08:54 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 26 Oct, 2006 - 09:04 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,171



Thanked 33 times

Dream Kudos: 25
My Contributions


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:
CODE

var = rand()%9;
v.push_back(var);

http://www.cplusplus.com/ref/cstdlib/rand.html
http://www.cplusplus.com/ref/cstdlib/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.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 06:04AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month