3 Replies - 860 Views - Last Post: 23 June 2011 - 06:39 PM Rate Topic: -----

#1 Tweekler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-June 11

Need help filling vector with unknown amount of ints

Posted 23 June 2011 - 05:14 PM

Hi, I just started a C++ class and I'm already having troubles with some parts. The instructions are as follows:

"Create a C++ console applications to compile the following statistics on a list of integers:
minimum value;
maximum value;
median value;
arithmetic mean;
variance (population variance);
standard deviation (population standard deviation);
mode (including multi-modal lists).
frequency distribution (fixed to 10 evenly distributed intervals);
histogram (of the frequency distribution);

Your program must handle any length of list. The list will be piped in from the command line and terminated with end-of-stream (^Z). "



Now I assumed I would have to order the ints into a vector in order to calculate the different stats, but I'm having trouble getting started. I'm not sure how to fill a vector if I don't know in advance how many numbers there will be. If some1 could help me get started with filling the vector with user input I would be most grateful.

Is This A Good Question/Topic? 0
  • +

Replies To: Need help filling vector with unknown amount of ints

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Need help filling vector with unknown amount of ints

Posted 23 June 2011 - 05:24 PM

most of the time i just use vector::push_back

std::vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);



would create the array [10, 20, 30, 40, 50]

or if you are more familiar with arrays you can use a more array like approach.
std::vector<int> v(5);
v[0] = 10;
v[1] = 20;
v[2] = 30;
v[3] = 40;
v[4] = 50;



this would create the same array as above, however to add more than 5 elements you would have to re-size it or use push_back.
std::vector reference
a tutorial on vectors

This post has been edited by ishkabible: 23 June 2011 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 Tweekler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-June 11

Re: Need help filling vector with unknown amount of ints

Posted 23 June 2011 - 06:25 PM

View Postishkabible, on 23 June 2011 - 05:24 PM, said:

most of the time i just use vector::push_back

std::vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);



would create the array [10, 20, 30, 40, 50]

or if you are more familiar with arrays you can use a more array like approach.
std::vector<int> v(5);
v[0] = 10;
v[1] = 20;
v[2] = 30;
v[3] = 40;
v[4] = 50;



this would create the same array as above, however to add more than 5 elements you would have to re-size it or use push_back.
std::vector reference
a tutorial on vectors

Thanks,
But I don't see how this helps me when I don't know the number of elements that there is going to be. I know how to do it if i need, say 5 elements, but it could be 5, it could be 3702052 elements for all i know, depending on how many numbers are entered in the input.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,329
  • Joined: 08-August 08

Re: Need help filling vector with unknown amount of ints

Posted 23 June 2011 - 06:39 PM

One advantage of vectors over arrays is that you don't need to know how many elements you're going to have. You can simply push_back() each element as you find them, and then use size() to determine how many you have.
http://www.cplusplus...nce/stl/vector/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1