Using vectors

Hi everyone. I've recently started doing some work with vectors, and I'm a little confused so I just have a few questions on this program I'm writing. So the program is supposed to create an endless loop of little particle bouncing around inside of a little box. I was able to do it pretty eaily for just one particle, but I need to use vectors in order to create more particles. You'll notice here:
vector<Particle*>Ball;
for(i;i<10;i++)
{
Ball.push_back(new Particle());
}
//Particle* Ball1=new Particle();
that I was trying to figure it out a bit, and commented what I was using to make it work with just one particle. Also another more broad question is that I was wondering if anyone could explain to me the whole pass by reference and pass by value thing? I don't really understand what it does, how it works, or what it's used for? Thanks! And sorry if I posted this maybe a little weird it's my first post here!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
  #include <iostream>
#include <ctime>
#include <vector>
#include <stdlib.h>
#include <unistd.h>
using namespace std;
const int WIDTH=20;
const int HEIGHT=20;
class Particle
{
public:
    double positionx, positiony, velocityx, velocityy;
    Particle()
    {
        srand(time(0));
        positionx=rand()%WIDTH;
        positiony=rand()%HEIGHT;
        velocityx=(rand()%5)-2;
        velocityy=(rand()%5)-2;
    }
    int Positionx()
    {
        return positionx;
    }
    int Positiony()
    {
        return positiony;
    }
    int Velocityx()
    {
        return velocityx;
    }
    int Velocityy()
    {
        return velocityy;
    }
    double Move()
    {
        srand(time(0));
        positionx=positionx+velocityx;
        if(positionx<1)
        {
            positionx=positionx-velocityx-velocityx;
            velocityx=(rand()%2)+1;
        }
        if(positionx>WIDTH-1)
        {
            positionx=positionx-velocityx-velocityx;
            velocityx=(rand()%2)-2;
        }
        positiony=positiony+velocityy;
        if(positiony<1)
        {
            positiony=positiony-velocityy-velocityy;
            velocityy=(rand()%2)+1;
        }
        if(positiony>HEIGHT-1)
        {
            positiony=positiony-velocityy-velocityy;
            velocityy=(rand()%2)-2;
        }
        return positionx && positiony;
    }
};
int main()
{
    int y=0, x=0, i=0;
    vector<Particle*>Ball;
    for(i;i<10;i++)
    {
        Ball.push_back(new Particle());
    }
    //Particle* Ball1=new Particle();
    while(true)
    {
        system("CLS");
        usleep(100000);
        y=0;
        for(y;y<HEIGHT;y++)
        {
            x=0;
            if(y==0)
            {
                cout<<"--------------------";
            }
            if(y==19)
            {
                cout<<"--------------------"<<endl;
                break;
            }
            for(x;x<WIDTH;x++)
            {
                if(Ball->Coordinate()==x,y)
                {
                    cout<<"*";
                }
                else
                {
                    if((x==0 || x==19) && y!=0)
                    {
                        cout<<"|";
                    }
                    else
                    {
                        cout<<" ";
                    }
                }
            }
            cout<<endl;
        }
        Ball->Move();
    }
    return 0;
}
Line 15,99: Do not call srand() within a loop or a random number function. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/

Pass by value: The function gets a copy of the argument. Any changes made to the argument within the function are not reflected in the caller's copy of the argument.

Pass by reference. The function references the caller's instance of the argument. Any changes made to the argument are reflected in the caller's instance of the argument.

Pass by reference is preferred for large objects as it avoids creating a copy of the argument on the stack.

Topic archived. No new replies allowed.