Thread: 2 dimensional array

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

    2 dimensional array

    My current program takes user input for an expression tree, example *2x and outputs

    * 0 0
    2 0 0
    x 0 0

    *2x is stored in a string.
    the 2nd and 3rd columns represent a left-child right-child array. When one of the following operands are input "+ - * / " the zeros need to be changed to the locations of their child's. Like in this example 2 would be the left child of * and x would be the right child so the output should look like

    * 1 2
    2 0 0
    x 0 0

    The only way I have thought to do this is to use a switch statement to look for the operands but I don't know how to call on the index that it's child is in.
    This is my code:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
       char s[50];
       cout << "Input the prefix expression.\n";
       cin >> s;
       cout << "\nThe expression tree is:";
    
       
       int row = strlen(s);
       int col = 2;
       char a[10][10];
       int i, j, c;
       int pos;
    
       for (j = 0; j < row; j++)
       {
           a[j][0] = s[j];
    
       for(i = 1; i <= col; i++)
       {
           a[j][i] = '0';
           
           switch (s[j]) {
             case '+':
             case '-':
             case '*':
             case '/':
             
               a[j][i] =;
               break;
               }
       }
       }
    
       for (j = 0; j < row; j++)
       {
           cout << "\n";
       for(i = 0; i <= col; i++)
       {
           cout << a[j][i] << setw(2);
       }
       }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by whitey300 View Post
    My current program takes user input for an expression tree, example *2x and outputs

    * 0 0
    2 0 0
    x 0 0

    *2x is stored in a string.
    the 2nd and 3rd columns represent a left-child right-child array. When one of the following operands are input "+ - * / " the zeros need to be changed to the locations of their child's. Like in this example 2 would be the left child of * and x would be the right child so the output should look like

    * 1 2
    2 0 0
    x 0 0

    The only way I have thought to do this is to use a switch statement to look for the operands but I don't know how to call on the index that it's child is in.
    You won't know until you get there. You can't be guaranteed that the next two items will be your operands (if it was something like *+3x2, for instance, I'm betting you would want to print out
    Code:
    * 1 4
    + 2 3
    3 0 0
    x 0 0
    2 0 0
    right?)

    So, you'll need to set up something very much like a stack so that, as you read along you can see where the thing you are reading belongs.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    yes that is exactly how I want it to output. How do I set up a stack for it to work though?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    however I'm not allowed to use classes, pointers, or structs

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by whitey300 View Post
    however I'm not allowed to use classes, pointers, or structs
    Not allowed to, eh? Then you'll have to mock up a stack using an array (keep track of the "top" of the stack with an index counter).

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