(c++)

perkeyone

something clever
Reaction score
71
i still need some help with this
i fixed a logic error in my code just now,
i was using ands instead of ors
 

Samael88

Evil always finds a way
Reaction score
181
Man could you make it a bit more readable? I can barely tell when your if's are starting or ending:(

I did it with your first function, I think that I have spaced it out.
Code:
void children(int k)
{
	if(lcrc[0][k]!="+"||lcrc[0][k]!="-"||lcrc[0][k]!="*"||lcrc[0][k]!="/")
                {
		lcrc[1][k]=n;
		n++;
		children(lcrc[[1][int k]-"0");
		lcrc[[2][k]=n;
		n++;
		children(lcrc[[2][k]-"0");
               }
	else
               {
		lcrc[1][k]=0;
		lcrc[[2][k]=0;
               }
}

And please give an example on what you are getting for results and what the result should be for that given example. That would also help a lot.

Edit: Oh, and btw, your code is really badly commented, thus making it pretty hard to tell what everything is:(
 

perkeyone

something clever
Reaction score
71
im sorry that its not very readable i will try to space it out, and im also sorry for the lack of comments.

the reason i didnt give examples of what i am getting is because i couldnt even get it to compile.

but i did give examples of what it should do.
 

Samael88

Evil always finds a way
Reaction score
181
the reason i didnt give examples of what i am getting is because i couldnt even get it to compile.

No prob dude. Then I have these questions:
What compiler are you using and what are the errors that you get?
 

perkeyone

something clever
Reaction score
71
i am using "bloodshed dev c++"
i added a picture to clarify how i am using the 2d array
its kinda big and stretches the screen a bit, sorry
i also added comments which i hope will make things understandable

i will try to compile in a moment and tell you what errors i am getting
although i dont think they are small syntax errors like i forgot a semicolon or something
i think im doing something wrong completely
so the error messages might not be useful.

EDIT here they are
errors.jpg
 

Samael88

Evil always finds a way
Reaction score
181
You have these variables declared in main:
Code:
char lcrc[3][9], n="1";

But they are not declared in the other functions!

If you want them to be used in all functions you should declare them as global variables instead of locals.

Edit: "so the error messages might not be useful." sorry to say that that is a load of bull****:p
The error messages are always useful, if you know what can cause the different error messages then it is easier to locate the error;)
 

Samael88

Evil always finds a way
Reaction score
181
An example:

Code:
#include <iostream.h>

int test = 5; //global

int main()
{

   int other = 10 // local
   std::cout<<test<<" "<<other;
   return 0;
}

locals can only be used within the function they are declared while globals can be used everywhere.
 

perkeyone

something clever
Reaction score
71
i tried initializing them outside of main
but i got some more errors, ill have to post them later though because i dont have my laptop with me at this time.
 

perkeyone

something clever
Reaction score
71
here are the errors, the only change i made was that i put this line
Code:
char lcrc[3][9], n="1";			//this is my 2d array where all the magic happens
outside of main, up near the top after "using namespace std;"
errors2.jpg
 

Samael88

Evil always finds a way
Reaction score
181
Code:
children(lcrc[[2][k]-"0");

This line seems odd to me.
How can you do math with a char?
I have never seen that and I don't even think that it is possible to do.

I think that it can be some problems with the function calling itself as well.
I can't seem to understand why you would want it do so, I for one would just add another function or something.

Oh, and can you really set a char to int like you are trying to do with n in children?
If you got the header <string.h> included you could try atoi(). like this:
lcrc[1][k]=atoi(n);

Oh, and perhaps if you moved this: char lcrc[3][9], n="1";
to global, then you got both a char and a int that is named n. That could cause some problem as well. Rename one of them and try again:)
 

perkeyone

something clever
Reaction score
71
i figured out i had a lot of errors from extra brackets which i pasted a couple of times accidentally. i think i removed all of them but there are still many errors
Code:
//Calvin Perkey
//OLA2
//October 19 2009
#include <iostream>
#include <string>
using namespace std;
char lcrc[3][9];					//this is my 2d array where all the magic happens
int n=1;						//this is an index used in the children function
void children(int k)
{							//this function is supposed to fill up the 2nd and 3rd columns of my array
							//first it checks if the first column contains an operator character
	if(lcrc[0][k]!="+"||lcrc[0][k]!="-"||lcrc[0][k]!="*"||lcrc[0][k]!="/")
	{						//if it is an operator then there must be operands, so is puts the next index into left child
							//the next index starts as "1" but is increased each time it gets used
							//the index is also a character since it is being put in a char array but will never be greater than 9
							//since my teacher specified that she would not be using more than 9 characters for input
		lcrc[1][k]=itoa(n);			//first it sets the left child index
		n++; 
		children(atoi(lcrc[1][k]));		//here the function calls itself on the right child of the current index
		lcrc[2][k]=n;				//the it sets the right child index
		n++;
		children(atoi(lcrc[1][k]));		//here the function calls itself on the right child this time
	}
	else						//if it was not an operation character then there are no children
							//so we set them to the character "0"
	{
		lcrc[1][k]="0";
		lcrc[2][k]="0";
	}
}
void postfix(int k)
{							//after all the data is put into the array this function outputs the data in postfix order
	if(lcrc[1][k]!="0")				//if the index has a right child the function calls itself on the right child
	{
		postfix(atoi(lcrc[1][k]));
	}
	if(lcrc[2][k]!="0")				//if the index has a left child it calls itself on the left child
	{
		postfix(atoi(lcrc[1][k]));
	}
	cout<<lcrc[0][k];				//finally after testing both left and right, it outputs the character stored at the index
}
void infix(int k)
{							//this function works similar to the postfix but goes in a different order and adds parenthses
	cout<<"(";
	if(lcrc[1][k]!="0")				//it calls itself for the left child
	{
		postfix(atoi(lcrc[1][k]));
	}
	cout<<")"<<lcrc[0][k]<<"(";			//it outputs the character at the index
	if(lcrc[2][k]!="0")
	{
		postfix(atoi(lcrc[1][k]));		//then it calls itself for the right child
	}
	cout<<")";
}
int main()
{
	cout<<"Input prefix expression.\n";		//promts for input
	for(int i=0;i<9;i++)
	{
		cin.get()>>lcrc[0][i];			//put each character of the input into the first column of the 2d array
	}
	children(0);					//calls the children function which i described above
	cout<<"The expression tree is\n";		//this outputs all the data in the array
	for(int i=0;i<9;i++)
	{
		for(int j=0;j<3;j++)
		{
			cout<<lcrc[j][i]<<" ";
		}
		cout<<"\n";
	}
	cout<<"The postfix expression is ";
	postfix(0);					//this calls the postfix function i described above
	cout<<"\nThe infix expression is ";
	infix(0);					//this calls the infix functionion from above
	return 0;
}
errors3.jpg
 

perkeyone

something clever
Reaction score
71
wooot!
i reverted back to my old code
and changed the double quotes to single quotes
omg im so dumb lol
thanks a ton for trying to help me out sam +rep to you

ok so i finaly compile and now my output is a little messed up i have edited the first post to reflect my current code

Code:
//Calvin Perkey
//OLA2
//October 19 2009
#include <iostream>
#include <string>
using namespace std;
char lcrc[3][9],n='1';					//this is my 2d array where all the magic happens
void children(int k)
{							//this function is supposed to fill up the 2nd and 3rd columns of my array
							//first it checks if the first column contains an operator character
	if(lcrc[0][k]=='+'||lcrc[0][k]=='-'||lcrc[0][k]=='*'||lcrc[0][k]=='/')
	{						//if it is an operator then there must be operands, so is puts the next index into left child
							//the next index starts as "1" but is increased each time it gets used
							//the index is also a character since it is being put in a char array but will never be greater than 9
							//since my teacher specified that she would not be using more than 9 characters for input
		lcrc[1][k]=n;			//first it sets the left child index
		n++; 
		children(lcrc[1][k]-'0');		//here the function calls itself on the right child of the current index
		lcrc[2][k]=n;				//the it sets the right child index
		n++;
		children(lcrc[2][k]-'0');		//here the function calls itself on the right child this time
	}
	else						//if it was not an operation character then there are no children
							//so we set them to the character '0'
	{
		lcrc[1][k]='0';
		lcrc[2][k]='0';
	}
}
void postfix(int k)
{							//after all the data is put into the array this function outputs the data in postfix order
	if(lcrc[1][k]!='0')				//if the index has a right child the function calls itself on the right child
	{
		postfix(lcrc[1][k]-'0');
	}
	if(lcrc[2][k]!='0')				//if the index has a left child it calls itself on the left child
	{
		postfix(lcrc[2][k]-'0');
	}
	cout<<lcrc[0][k];				//finally after testing both left and right, it outputs the character stored at the index
}
void infix(int k)
{							//this function works similar to the postfix but goes in a different order and adds parenthses
	cout<<'(';
	if(lcrc[1][k]!='0')				//it calls itself for the left child
	{
		infix(lcrc[1][k]-'0');
	}
	cout<<')'<<lcrc[0][k]<<'(';			//it outputs the character at the index
	if(lcrc[2][k]!='0')
	{
		infix(lcrc[2][k]-'0');		//then it calls itself for the right child
	}
	cout<<')';
}
int main()
{
	string m;
	cout<<"Input prefix expression.\n";		//promts for input
	cin>>m;
	for(int i=0;i<m.size();i++)
	{
		m[i]>>lcrc[0][i];			//put each character of the input into the first column of the 2d array
	}
	children(0);					//calls the children function which i described above
	cout<<"The expression tree is\n";		//this outputs all the data in the array
	for(int i=0;i<m.size();i++)
	{
		for(int j=0;j<3;j++)
		{
			cout<<lcrc[j][i]<<" ";
		}
		cout<<"\n";
	}
	cout<<"The postfix expression is ";
	postfix(0);					//this calls the postfix function i described above
	cout<<"\nThe infix expression is ";
	infix(0);					//this calls the infix functionion from above
	cin>>lcrc[0][0];
	return 0;
}
 

perkeyone

something clever
Reaction score
71
hey i just wanted to update to let anyone who was still looking at this this that i solved the last little problem that i was having.

at one point in time i wanted change my code from a cin.get>>lcrc[0]
to
cin>>m
lcrc[0]=m

but i didnt do it correctly
but now it is fixed along with a few other minor things

anyways thanks again
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top