[B]2 Dimensional Array [/B]

This is a discussion on [B]2 Dimensional Array [/B] within the C++ Programming forums, part of the General Programming Boards category; As of now my program as the user input an expression like *2x and then the program outputs it as ...

  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
    Katy, Texas
    Posts
    2,309
    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 04:28 PM.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-10-2008, 12:53 AM
  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21