Thread: Newbbie lookin' for help on two-dimensional arrays

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    Question help on two-dimensional arrays

    im learning C++ by myself. (my brother and I)
    yesterday a friend of us told us that we can make a multiple array..
    and he gave us an example with a times table ( 2x2=4)
    we tried to use it but it didn't work
    we used the
    int num[ ] [ ];

    of couse if we hard code it will work but we cannot do it, by prompting the user..
    can you help us solving it?
    thank you in advance for your help.

    Thank you for all your feed back..
    ------------------------------------------------------------------------------------
    but how can i make it interactive.. propting the user..
    like..
    cout<<"Please enter the first number"<<endl;
    cin>>num1;
    cout<<"Please enter the second number"<<endl;
    cin>>num2;
    and then display
    cout<<num1<<*<<num2<<"="result<<endl; //displaying both numbers and then the result
    ==============================================
    Last edited by moenia; 05-21-2002 at 11:15 AM.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You need to supply the dimensions within brackets.

    Code:
    #include <iostream>
    
    const int NUM_ROWS = 5; // dimensions need to be constants at compile time
    const int NUM_COLS = 5;
    
    int main() {
    	int timesTable[NUM_ROWS][NUM_COLS];  // create the matrix with sizes
    
    	for (int i = 0; i < NUM_ROWS; i++) {
    		for (int j = 0; j < NUM_COLS; j++) {
    			timesTable[i][j] = (i + 1) * (j + 1);  // fill the table
    		}
    	}
    
    	for (int r = 0; r < NUM_ROWS; r++) {
      		for (int c = 0; c <NUM_COLS; c++) {
    			std::cout << timesTable[r][c] << "\t"; // display the table
    		}
    		std::cout << std::endl;
    	}
    
    	return 0;
    }
    The catch is that the size of the matrix needs to be known at compile time, or else you need to use dynamic memory... but that stuff is a little rough for beginners.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Aside from the excellent help on the code, SilentStrike, may I add that MSVC++ 6 wouldn't compile the code as presented. Borland C++Builder5 did.

    Borland:
    Code:
    As presented by SilentStrike
    MSVC++:
    Code:
    #include <iostream.h> // Add the .h
    
    const int NUM_ROWS = 5; // dimensions need to be constants at compile time
    const int NUM_COLS = 5;
    
    int main() {
    	int timesTable[NUM_ROWS][NUM_COLS];  // create the matrix with sizes
    
    	for (int i = 0; i < NUM_ROWS; i++) {
    		for (int j = 0; j < NUM_COLS; j++) {
    			timesTable[i][j] = (i + 1) * (j + 1);  // fill the table
    		}
    	}
    
    	for (int r = 0; r < NUM_ROWS; r++) {
      		for (int c = 0; c <NUM_COLS; c++) {
    			//std::cout << timesTable[r][c] << "\t"; // display the table
    			cout << timesTable[r][c] << "\t"; // display the table
    		}
    		//std::cout << std::endl;
    		cout << endl;
    
    	}        
    	return 0;
    }
    Lots of opinions and they're all like...hmmmm.

    Thanks, again.

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    I didn't test it with MSVC, but I am sure it supports the std namespace. iostream.h isn't standard C++, anymore.

    http://www.cplusplus.com/doc/tutorial/tut5-2.html

    One of the best examples that we can find about namespaces is the standard C++ library itself. According to ANSI C++ standard, the definition all the classes, objects and functions of the standard C++ library are defined within namespace std.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Quite right, and the error was mine.

    In transferring the code from Borland, which requires #include <conio> for 'getch();', to MSVC, my fat fingers stopped paying attention to my brain.

    Or, was it the other way around...?

    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  2. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  3. dimensional arrays
    By ZakkWylde969 in forum C++ Programming
    Replies: 3
    Last Post: 08-04-2003, 04:49 PM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 12:53 PM