Thread: 2-D Arrays

  1. #1
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77

    2-D Arrays

    Hello.

    I have a few questions for everyone. We have a large programming assignment we are working on. We have to make a program which arranges rectangles on a giant x,y grid (a bigger rectangle). We have an objects.dat which looks like this:

    400 200
    20 30 5
    10 40 3
    25 30 4

    The top 2 lines are the width and heighth of our giant rectangle (the "grid"). So first we wrote a code to read in these 2 lines and turn the numbers into variables. We can't figure out how to setup a variable-length array to where the array size is of our variable lengths. We are setting up a large array to pretend it's our grid...so it's size will be the size of the top line in the program.

    The lines besides the top line are the smaller rectangles that are supposed to fit. The first number is the width, the next numbers it the heighth, and the final number is how many objects of that size there are. Our program is supposed to arrange each rectangle in an order symmetrical to the center of the grid. The lower left point on the grid is 0,0...the x axis increases with it goes right, and the y axis increases when it goes up.

    We would like someone to explain to us how to make an array of variable size...and if anyone has any better ideas of how to arrange rectangles to make them fit, please let us know.

    PLEASE HELP US!

  2. #2
    Unregistered
    Guest
    With a name like SavesTheDay, why are you asking us?

  3. #3
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    It's a band, you dolt. You really aren't funny.

  4. #4
    Unregistered
    Guest
    It may be a band, but it sounds really arrogant.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    int array[][]={ { } };

    But then you have to initialize the array with values.

    This is not C, its C++ but it works.

    int ArraySize=400;

    int *Array=new int[ArraySize];



    you could also use multiple indirection to create a list of arrays

    int **CollectionOfIntArrays;

  6. #6
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    if it works, and it is an assignment and also knowing that it is posted on a 'C' board then he probably needs the answer in
    C.

    therefore to change the C++ to C

    int arraysize=400;
    int *array = (int*) malloc( arraysize * sizeof(int));

    that should also do it.

    kwigibo

  7. #7
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    Sounding arrogant would be naming myself:

    "THESMARTESTPROGRAMMERINTHEWORLDWHOHASSEXWITHYOURM OTHEREVERYNIGHTANDWHOKNOWSEVERYTHING"

    or I could go with something as BOLD and chicken **** such as "Unregistered"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM