Welcome Guest ( Log In | Register )

IPB

 
RépondreNouveau sujet
» I need help with finding index of array!!!
whitey300
post Oct 22 2009, 07:13 PM
Post #1


Newbie


Group: Members
Posts: 3
Joined: 22-October 09
Member No.: 14,417




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
#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;
}
Go to the top of the page
 
+Quote Post
whitey300
post Oct 22 2009, 08:46 PM
Post #2


Newbie


Group: Members
Posts: 3
Joined: 22-October 09
Member No.: 14,417




can anyone helps please!!????
Go to the top of the page
 
+Quote Post
whitey300
post Oct 22 2009, 08:47 PM
Post #3


Newbie


Group: Members
Posts: 3
Joined: 22-October 09
Member No.: 14,417




QUOTE (whitey300 @ Oct 22 2009, 09:46 PM) *
can anyone helps please!!????

Go to the top of the page
 
+Quote Post
bayp
post Oct 22 2009, 09:21 PM
Post #4


RTFM kthx
Group Icon

Group: Support Staff Lv2
Posts: 3,788
Joined: 1-March 04
From: Pottstown, PA, USA
Member No.: 11




First off, please read the forum rules. Excessive bumping of threads is against it. You may only do it once every 24 hours.

Secondly, isn't there anyone in your class that you can call and ask?


--------------------
b4yp's latest Twitter update:
Go to the top of the page
 
+Quote Post

Reponse rapideRépondreNouveau sujet
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 




Lo-Fi Version
Time is now: 8th September 2010 - 10:44 PM
Skin by IPB FR - IPB Europe