Returning index of an array!!!

  • whitey300
  • Born
  • Born
  • whitey300
  • Posts: 2

Post 3+ Months Ago

I am having trouble figuring out how to return the index of an element in an array or string. My program is an example of an expression tree in a left child-right child array. The user inputs an expression like +2x and output shoud be as follows:

+ 1 2
2 0 0
x 0 0

The 1 in the 2nd column represents the row the left child is in and the 2 in the 3rd column represents the row the right child is in. Another example would be +/43*34

+ 1 4
/ 2 3
4 0 0
3 0 0
* 5 6
3 0 0
4 0 0

this is my code so far:
Code: [ Select ]

#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[9][4];
  int i, j, c;
  int index = 1;
 
  for (j = 0; j < row; j++)
  {
    a[j][0] = s[j];

  for(i = 1; i <= col; i++)
  {
  
   
    if ( s[j] == '+' || s[j] == '-' || s[j] == '*' || s[j] == '/')
    {
      a[0][i] = index;
    }
    else
    {
      a[j][i] = '0';
    }
    index++;
  }
  }

  for (j = 0; j < row; j++)
  {
    cout << "\n";
  for(i = 0; i <= col; i++)
  {
    cout << a[j][i] << setw(2);

  }
  }

  cout << "\n\nThe postfix expression is " << endl;
 
  cout << "The infix expression is " << endl;
 
  system ("PAUSE");
  return 0;
}
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. int main()
  5. {
  6.   char s[50];
  7.   cout << "Input the prefix expression.\n";
  8.   cin >> s;
  9.   cout << "\nThe expression tree is:";
  10.  
  11.   int row = strlen(s);
  12.   int col = 2;
  13.   char a[9][4];
  14.   int i, j, c;
  15.   int index = 1;
  16.  
  17.   for (j = 0; j < row; j++)
  18.   {
  19.     a[j][0] = s[j];
  20.   for(i = 1; i <= col; i++)
  21.   {
  22.   
  23.    
  24.     if ( s[j] == '+' || s[j] == '-' || s[j] == '*' || s[j] == '/')
  25.     {
  26.       a[0][i] = index;
  27.     }
  28.     else
  29.     {
  30.       a[j][i] = '0';
  31.     }
  32.     index++;
  33.   }
  34.   }
  35.   for (j = 0; j < row; j++)
  36.   {
  37.     cout << "\n";
  38.   for(i = 0; i <= col; i++)
  39.   {
  40.     cout << a[j][i] << setw(2);
  41.   }
  42.   }
  43.   cout << "\n\nThe postfix expression is " << endl;
  44.  
  45.   cout << "The infix expression is " << endl;
  46.  
  47.   system ("PAUSE");
  48.   return 0;
  49. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • whitey300
  • Born
  • Born
  • whitey300
  • Posts: 2

Post 3+ Months Ago

can anyone help please!!!

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 108 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.