Thread: [B]2 Dimensional Array [/B]

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    [B]2 Dimensional Array [/B]

    As of now my program as the user input an expression like *2x and then the program outputs it as

    *
    2
    x

    I am wanting to display zeros next to the elements, 2 columns of zeros so the output will be

    * 0 0
    2 0 0
    x 0 0

    This is my code
    Code:
    int main()
    {
    
    string s;
    cout << "Input the prefix expression.\n" ;
    cin >> s;
    cout << "The expression tree is:\n" ;
    
    int row = s.length();
    int col = 2;
    int array[row][col];
    int i, j, c;
    
    for (j = 0; j < row; j++)
    for (c = 0; c < col; c++)
    for (i = 0; i < s.length(); i++)
    {
    cout << s[i] << array[row][col] << endl ;
    }
    it outputs

    *2293584
    22293584
    x2293584
    *2293584
    22293584
    x2293584
    *2293584
    22293584
    x2293584
    *2293584
    22293584
    x2293584
    Edit/Delete Message

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can't define an array like that. You need to malloc one up.

    And, you have a 3X3 matrix, and you are treating it like one, but your (incorrect) array definition is 3X2.

    Finally, your outer loop should be the length iteration (or at least that's how 99% of programmers would do it)
    Last edited by Dino; 10-21-2009 at 03:28 PM.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Replies: 5
    Last Post: 11-20-2001, 12:48 PM