Thread: Cars moving in a d-array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    10

    Post 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. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    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. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    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. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Can you post the exact question?
    Maybe you misunderstood parts of it ?

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Well, does the user move the cars around as well within the x dimensional space? Lol, seems like a silly exercise.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    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...)
    Last edited by PenLix; 11-26-2011 at 03:56 PM.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by PenLix View Post
    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]
    Last edited by manasij7479; 11-26-2011 at 04:17 PM.

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    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
    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.
    Last edited by Incantrix; 11-26-2011 at 04:27 PM.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    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. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    Quote Originally Posted by Incantrix View Post
    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. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    In your code there, where are you using the user entered # of dimensions within any of your classes?

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    In the Dimension class i can get this # by the method GetDimension()...no?

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    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. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    10
    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...
    Last edited by PenLix; 11-26-2011 at 06:15 PM.

  15. #15
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    So, write it in C using structs and then change the structs over to classes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average (from and into an array)
    By browser in forum C Programming
    Replies: 4
    Last Post: 01-24-2010, 03:36 PM
  2. moving from one array adress to another
    By jrb47 in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2006, 05:32 PM
  3. Moving to the next structure array
    By mattz in forum C Programming
    Replies: 2
    Last Post: 11-30-2001, 03:43 PM
  4. Moving values in an array
    By venomgts550 in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2001, 06:41 PM