# Thread: Cars moving in a d-array

1. ## Cars moving in a d-array

Hello everyone,

i am new to the forum and would like to ask a question. I would like to write a program where i have 2 cars and they are moving in a d-array. I had made a same program where i had 1-d array only. Basicaly the moves are +1, 0, +1 just to know. I want to have d dimensions and do some things...

The only thing i know is that the 'd' number is given by user and i must have a class Car whos code must be the same with main's. Also i must have one class Position and Dimension. That was an exercise where i dont want you to solve but only to help me understand what i must do. I dont know what private and public data should i use. :/

thx for anyone that helps me...cya

2. So what your asking is that if the user enters a 9, you want then the cars to be able to move around in 9 dimensions?

3. Basically this is what i first thought and this is logical...but i really dont know what exactly i must do. The proffessor is so strange so i cant ask him...nevermind this is another part...

Any ideas how i could set up my program?

4. Can you post the exact question?
Maybe you misunderstood parts of it ?

5. Well, does the user move the cars around as well within the x dimensional space? Lol, seems like a silly exercise.

6. Well, i' ll try to translate it caz its in greek language :P

We have made in class a program that simulate 2 cars that they move randomly in a 1-dim array untill they collide. Use this program so it works in d-dimensions. In the d-dimensions for every move of a car for every dimension the value is +1, 0, or -1. The value of a dimension can't be more than 2 or less than -2. If we have d dimensions this means that the car moves in the d-cube [-2,2]^d. the cars start from 0. you must print the position that they collide. print the position of the cars in every time the move. d is given from the user. you must have a class Car which code is same as mains and one class Position and one Dimension....

(10 days...)

7. Originally Posted by PenLix
Well, i' ll try to translate it caz its in greek language :P

We have made in class a program that simulate 2 cars that they move randomly in a 1-dim array untill they collide. Use this program so it works in d-dimensions. In the d-dimensions for every move of a car for every dimension the value is +1, 0, or -1. The value of a dimension can't be more than 2 or less than -2. If we have d dimensions this means that the car moves in the d-cube [-2,2]^d. the cars start from 0. you must print the position that they collide. print the position of the cars in every time the move. d is given from the user. you must have a class Car which code is same as mains and one class Position and one Dimension....

(10 days...)
Interesting..(If I understand it correctly)
Make a coordinate class having a vector of N elements, initialized to 0 (take an n dimensional vector in a 1d array)
Include a coordinate object in your car class.
For each turn.. change each element of the coordinate by +1,-1,0r 0..(But it abs value can't exceed 2) ...randomly.
At the end of the turn.. if the coordinates of the two cars match..record it.

[I think you could do the whole thing more simply by some linear algebra tricks....but I'm not sure]

8. Can you use vectors? If so, I think I would use Position to hold the vector of Dimension (rather a waste of time to make a class to hold a linear position, just 1 variable member) and to do the boundry checking within the given cube of [-2,2]^d).

Not sure what you meant by
i must have a class Car whos code must be the same with main's

I'm certainly not even an intermediate in this stuff yet, but thats how I think I would do it. Unless something has been lost in translation.

9. mmm...ok! so, i think i will have 3 classes. can you help a bit with the private/public data/methods ? :/

Code:
```class Car{
private:

public:
void InitializePos();
void Move();
bool Collide;
};

class Dimension {
private:
int _dim;
public:
int GetDim();
}

class Position {
private:
int *Pos; // 1-d array for the d dimensions
public:
int GetPos();
}```

10. Originally Posted by Incantrix
Can you use vectors? If so, I think I would use Position to hold the vector of Dimension (rather a waste of time to make a class to hold a linear position, just 1 variable member) and to do the boundry checking within the given cube of [-2,2]^d).

Not sure what you meant by in your first post.

I'm certainly not even an intermediate in this stuff yet, but thats how I think I would do it. Unless something has been lost in translation.
this is exactly what is says: "the program must have a class Car and you must make this way so the main's code and the Cars code will be the same regardless from the dimensions we have"

11. In your code there, where are you using the user entered # of dimensions within any of your classes?

12. In the Dimension class i can get this # by the method GetDimension()...no?

13. Erm, so your going to allocate memory and use your pointer in Position as an array?

I don't think I could guide you through this without simply writing something that I think you need. Do you even know how to allocate memory with malloc()? If you don't then it most likely will not work, as memory is not always (often not) congruent and you would have no way to index through it to get your next coordinate (as far as I know).

Haha, I could of course be ignorant of this.

14. sure! i know quite well C(past 2 years) so this is super easy. Look, the real problem i have with C++ is when defining the classes and the data(private), mehtods(public)... :/ i stack there and cant go on...thats why i ask for a "plan"! :/ anyway thx for the time...

15. So, write it in C using structs and then change the structs over to classes.