1 Replies - 1670 Views - Last Post: 26 October 2006 - 10:04 AM Rate Topic: -----

#1 sknox1  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • 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

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1