Thread: Arrays of user-defined size?

  1. #1
    Captain_Penguin
    Guest

    Arrays of user-defined size?

    In my C++ class I've been writing C++ code based on matrix manipulation.

    My first program was simple enough, using a class to multiply a 2x2 matrix by a 2d column vector and display the results.

    Today I wrote a program using an array and a class to find the determinant of a user-inputed 2x2 matrix.

    BUT I want the user to be able to define the size of the matrix - the array.

    I tried this:

    int a;
    int b;
    int matrix[a][b];

    But, not surprisingly, that doesn't work.

    What needs to be done?

    Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Oh, just had an idea -

    Would it be better to make the array 10x10 or so, and then have the user choose a size and only put in #'s for the size the user wants? So when I'm using a for loop it will use the variables representing the size the user chose and will leave the rest of the values as 0?

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    To define the size of an array at runtime I always end up using malloc ( or one of its equivalents ) to get the size of memory needed to create an array the size a user wants. You could try that

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    See - "http://cboard.cprogramming.com/showthread.php?s=&threadid=24651"
    http://uk.geocities.com/ca_chorltonkids

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int x;
    int y;
    
    int* Matrix = new int[x * y];
    
    ...
    
    delete[] Matrix;
    or...
    Code:
    int x;
    int y;
    
    int** Matrix = new int*[y]; //<-- Is this true???
    for(int i = 0; i < y; i++) Matrix[i] = new int[x];
    
    ...
    
    for(int i = 0; i < y; i++) delete[] Matrix[i];
    delete[] Matrix;
    I prefer the first one. It is actually a 1D array, but can be handled as a 2D. The last example (not even sure if it's correct) is really easy to do something wrong at...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Well since I don't fully understand the proposed code (and I don't like using code unless I completely understand it), I just wrote this and it works fine -

    Code:
    // This program will make a matrix of user defined size and add it to another matrix of the same size.
    // By Will Herrick
    // last updated 9-12-02
    
    #include <iostream.h>
    #include <stdlib.h>
    class matrix
    {
          public:
          matrix();   // default constructor - fills the matrix with 0's
          void ShowMatrix();
          int a;
          int b;
          int x;
          int y;
          int matrix_ar[8][8]; // 8x8 array
    };
    matrix::matrix()
    {
                    a = 5;
                    b = 6;
                    for(x = 0; x<8 ; x++)
                    {
                          for(y = 0; y<8; y++)
                          {
                                matrix_ar[x][y]=0;
                          }
                    }
    }
    
    void matrix::ShowMatrix()
    {
         for(x = 0; x<a; x++)
         {
               cout << "| ";
               for(y = 0; y<b; y++)
               {
                     cout << matrix_ar[x][y];
               }
               cout << " |\n";
         }
    }
    
    int main(int argc, char *argv[])
    {
      matrix matrix_A;
      matrix_A.matrix_ar[0][1] = 5;
      matrix_A.ShowMatrix();
      system("PAUSE");
      return 0;
    }

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Salem
    > int** Matrix = new int*[y]; //<-- Is this true???
    > for(int i = 0; i < y; i++) Matrix[i] = new int[x];
    Yes, that's the way to do it
    But the result is accessed as [y][x] not [x][y]

    > int matrix_ar[8][8]; // 8x8 array
    Also a good idea providing the max size of the array isn't too large, and you don't have too many matrix objects in your program.
    Yea, when I actually go and write the full-fledged program I will determine a maximum size (I don't see any reason why anything bigger than 5x5 or 6x6 would be needed), and put in an if-statement to make sure the user doesn't put in anything too large.


    On another note, how do I create objects on-the-fly?

    For example:
    "Would you like to create a new matrix? Y/N"

    would this have to do with the "new" keyword? (which I USED to know how to use, but I stopped programming for 3 months - and I never programmed much anyways )

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Salem
    > would this have to do with the "new" keyword?
    Yeah, read the example posted above

    Just make the int **arr a member of the class (your data members of your class should be private by the way), and perhaps have a constructor which takes the x,y size parameters
    Hmm good idea about the constructor there!

    And yea, I know that those should be private - but I havn't had the courage to jump back into pointers just yet. (like I said, I returned from my minimal experience in coding after 3 months!)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Heapsort
    By xENGINEERx in forum C Programming
    Replies: 2
    Last Post: 03-30-2008, 07:17 PM
  2. Generic heapsort
    By Sephiroth1109 in forum C Programming
    Replies: 15
    Last Post: 12-07-2007, 06:14 PM
  3. Array Help Please (user defined size)
    By Planetx33 in forum C++ Programming
    Replies: 9
    Last Post: 04-07-2007, 04:36 PM
  4. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 03:06 PM
  5. Assign array size by user
    By Brown Drake in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2001, 06:45 AM