then there must be some bug with the function
BST_t * infixtree(char* infix){...}
do dry run of the code using pen and paper with a small example with '(' and check where it started going wrong......
then there must be some bug with the function
BST_t * infixtree(char* infix){...}
do dry run of the code using pen and paper with a small example with '(' and check where it started going wrong......
see the syntax of "if" statement in C.
u r doing
if(condition)
statement;
statement;
which is actually
if(condition)
statement;/*will execute depending on the condition*/
statement;/*will always execute*/
do it as
if(condition)
{
statement;
statement;
}/*use curly brace*/
needs to be .rtf because thats what apple uses as its texteditor
hi, I have tested it as .rtf also. And its still working.
I am using xcode to run my program and it doesn't work it, i might have to try another program to run mine. what program are you using?
i am doing it in Linux using the default c++ compiler (c++ <sourcefile>) and its running smoothly. by the way i have changed the input file extension to "txt" and then tested.
u can do that by keeping a counter or as follows:
class mymap
{
map<.....> m;
int maxsize=5;//say
/*if(m.size()<maxsize) then only do more addition to it. Else dont.*/
}
hi, i run your program and got the following output:
230.12we are in the loopwe are in the loopwe are in the loopwe are in the loop4
with input file as:
"input.txt":
23
.12
3
35
3
2
1
4
3
2
3
67
1
4
DO the following at the very begining:
1) push a '(' to the stack.
2) append ')' to the inputted expression string
3) now proceed with your remaining code.
This should solve your problem.
while(1)
{
printf("Select from the following operation:");
printf("1]Addtion");
printf("2]Subtraction");
printf("3]EXIT"); /*new statement*/
printf("Enter your choice here:");
scanf("%i",&z);
if(z==3)
break;
switch(z)
{
case 1:
/*your code*/
/*more cases*/
}
}
look at the following
if( tmp1->surname > tmp2->surname)
thats not a way for comparing two strings. U r comparing the address of the string instead.
use strcmp() for that purpose
hi Guyz...
need some help here...
i want to input an integer but it can be either in hexadecimal form or in integer form.
the problem is that i would like to store that input in that particular form and separate all hexadecimal values in to an array.for ex: if input is 0x3ff24e, then i want to create an array that stores the values 3,f,f,2,4,e in a an array of 6 variables.
now the problem is, as soon as i input the above number it gets converted into decimal form and hence i am unable to create an array of hexadecimal values??
so can someone please guide...
use a char * or string to read user input
try it as follows
Window *w=(Window *)it->second;
/*or (Window *)(it->second) if the former gives error*/
w->getTitle();
IS it possible to have text in the command line delimited by "" rather than white space?
i guess nothing's impossible....
U just have to parse the input on your own rather than using the inbuilt stuffs
dont go for dynamic memory allocation if u really need efficiency and u know the range (approx) of the elements u r going to have.
u can declare your array as
#define MAXTRANSITION 100
int pIntId[MAXTRANSITION];
int pTransistion[MAXTRANSITION];
throw an exception when the no. of transitions exceeds MAXTRANSITION.
Hi!
I m getting difficulty in pointers can u help ?
follow the link
http://www.exforsys.com/tutorials/c-language/c-pointers.html
Hello,
I am trying to make a compiler. Don't ask me why because I am making it for fun and experience. I have made a lexical analyser in c++ (I didn't use flex). I now want to make the parser for which I want to use Bison. But I am having problems.1) how do you specify which function is the lex analyser (I saw the manual, made a yylex function and made it return the numeric value for each token (it is enumerated).
2) Can you please give me some kind of base code on which I can build upon, like putting new rules etc.
if u want to use your yylex() in there add it as
extern int yylex(); in the .y file (.y for yacc).
U have to specify your grammer in the .y file.
make the following correction in main()
for(i=0;i<n;i++)
{
scanf("%d",&array[i]); /*& added*/
}
Thanks A Million!!!:cool:
u r welcome.
n plz mark this thread as solved if u think u got your answer :)
if i add *i there it pushes even the space also and every non null character.
and why to use continue, i dont understand.
1> add if((*i)&&( isdigit(*i) || isalpha(*i))
its because if u reach the end of the string then it will givevv segmentation fault when u try to access that location.
2> 'continue' skips the remaining code and moves the execution to the beginning of the loop.
is the java 100% poratble?
i dont have idea of java.
can you document few details/ differences about java and c with respect to compilation , code generation and portability.
thanks in advance.
Java has something called a JVM (Java Virtual Machine).
A java compiler compiles and generates target code for the JVM. (Google about JVM). The same JVM is used in different machines.
Java has an interpreter which takes the target instructions (made for JVM) and executes them according to the host machine specification. The interpreter is smart enough to handle the execution.
Just google about compiler design and its portability.
BST_t * infixtree(char* infix)
{
char *i,*p;
char n1;
BST_t *temp,*r,*l,*root,*t;
i = &infix[0];
while(*i)
{
// skip spaces
while(*i == ' ' || *i == '\t' && *i != '\0')
{
i++;
}
if( isdigit(*i) || isalpha(*i))
{
while( isdigit(*i) || isalpha(*i))/*add (&&*i)*****/
{
push_exp(*i);
i++;
}
/*add continue**********************/
}
if(isoper(*i))
{
push_exp(*i);
}
if( *i == '(' )
{
push_exp(*i);
}
if( *i == ')')
{
while( (n1 = pop_exp()) != '(')
{
if(isoperator(n1))
{
temp = create_node(n1);
t = pop_tree();
if(t)
temp->right = t;
}
else
{
r=create_node(n1);
push_tree(r);
}
}
l=pop_tree();
if(l){
temp->left=l;
root=temp;
push_tree(root);
}
}
i++;
}
return root;
}
Review your code using gdb debugger.... u will get the exact location where segmentation fault occured.
the compiler generates a machine code which is dependant on the platform where the compiler is installed.
Read about how Java attained platform independance. That will give a brief idea of why C is not like that.
Here is another problem..for the purpose to inverse an array.
void reverse(const int list[], int newlist[], int size)
{
for(int i = 0, j = size - 1; i<size; i++, j--)
{
newlist[j] = list
}
}i am not understand this function especially the for loop..anyone can kindly explain?
Thanks for advance ^^.
thats so simple.
U are assigning the elements in the newlist from its end to start from the other array 'list' from first to last.
i am doing for the 2nd highest. Do for the 3rd highest on your own. U can do it in a similar way as follows.
int score; double score1st = -1; double score2nd = -1; for (int i = 0; i < numberOfStudents; i++) { cout << "Enter a student score: "; cin >> score; if (score1st < score) { score1st = score; } else if(score2nd < score) score2nd = score; }
hi
making some correction to the earlier code
int score;
double score1st = -1;
double score2nd;
for (int i = 0; i < numberOfStudents; i++)
{
cout << "Enter a student score: ";
cin >> score;
if (score1st < score)
{
score2nd = score1st;/*newly added*/
score1st = score;
}
else if(score2nd < score)
score2nd = score;
}
previos code wold not give u correct answer all the time. Use this code for that.
******* thanks to JasonHippy ...........
Hmm..i tried but can't.
Perhaps can u give some hint? LOLThanks
i am doing for the 2nd highest. Do for the 3rd highest on your own. U can do it in a similar way as follows.
int score;
double score1st = -1;
double score2nd = -1;
for (int i = 0; i < numberOfStudents; i++)
{
cout << "Enter a student score: ";
cin >> score;
if (score1st < score)
{
score1st = score;
}
else if(score2nd < score)
score2nd = score;
}
Hello everyone,
I have to write a code to creat a pseudo assembler..I have an input file from which i read the assembly instructions from there i get the register numbers ...i have to convert that into machine code..i.e binary form...
Now my problem is that i can find the register values convert them into integer.....i can convert the integer into binary using itoa.......
but if my register number is 5 i am getting the binary as 101....but i need the binary to be 00101....can anyone please explain how to get the zero padding done......
for adding those extra zeroes u can just write a function which which check the lenght of the inputted binary string and accordingly will put extra zeroes at the beginning to make it of the particular length that u want.
void zeroPadding(char *bin, char *paddedNum, int requiredLen)
{
/*paddedNum should have enough memory allocated(==requiredLen+1)*/
int inLen = strlen(bin);
int i;
for(i=0;i<requiredLen-inLen;i++){
*paddedNum = '0'; paddedNum++;
}
while(*bin){
*paddedNum = *bin;
paddedBin++;
bin++;
}
*paddedNum = 0;//string termination//
return;
}
use it as
char *bin = "101"; //say
char *newBin = (char *)malloc(6); //e.g. if u want the binary of length 5
zeroPadding(bin, newBin, 5);
hope that helps................
I dont know what u r asking but a liked your algo for finding prime factors.
Explain your query in a little more detail.
use
c++ -o test TEST.cpp TestDefine.cpp
for compiling your code.
if u want need second highest score, 3rd highest n so on, the best thing is just sort the scores.
If u need only 1st, 2nd and 3rd u can keep two more variables like 'score'. Do it in similar way as u did for the highest score.
prob 1.
Remove all the elements of the queue and push it in the stack.
Now pop from the stack one element at a time and add it to the new queue until the stack is empty. Now return the new queue.
prob 2.
the answer is there in your question itself. Just do as its said.
***IMP : Dont expect readymade code in here.
If u want them then u r in the wrong place.
Can anyone help me to get the program code for printing the transpose of a matrix as output......pls help as soon as possible......
> we r not here to do ur homework.........
> U have to give some effort and then we can help........
Try out the problem yourself. We can help if u r stuck somewhere.
ya....i ll do that....i have turbo c++ compiler 3.0 version....is tat a problem??
that surely not an issue i guess
i am not sure exactly where is the problem because when I run that program i got correct results.
Do one thing, post the two programs that u are using. I will have look at it
also check the manual of the function
waitpid(.......)
type
man waitpid
in linux shell.
i have turboc++ compiler.......running and executing is not a problem.......but i am not able to access it randomly in the second program.............
can u send me a snapsot of the input/output for the read.c program.
Also send me a snapshot of the inputs given to the write.c program.
i m sorry if i m being stupid......i just have a doubt.....how should i run a program from dos shell??
Do u have turboC/borlandC in your machine.
If so open your file in it. In the main menu there are options for compiling and running your program.
Otherwise u have to get a copy of either turboC or borland C and put it in your machine.
After compilation u can directly run from there using the menu. Or u can exit turboC and in the command promt type the exe created by the compiler and press enter.
In my code, the parent process does not need to wait these children processes. And I need to know which child process finishes first to do rest of the code.
In the parent process u already have the child process id's.
U can repeatedly use the 'ps' command to ckeck which processes are running. Whenever u get one process missing break the loop and continue. And now u have the process id which just finished.
I dont know how to check the status of the processes in other way (there must be some).
u can do
system("ps>processes");
//this will store the output to the file 'processes'
//parse from the file whatever u need.
Hope thats of some help
did read.c work??it isn't working here.....its not abt syntax errors.....but.....the records aren't getting saved in the disc....if i try to read the records from the dos shell its showing some junk values.....what could be the problem???
I have tested the read.c file in my machine which was working properly.
You can't read the doc like that since I used fwrite() which writes the stuff in binary format. U can read them using the read.c file only. Run it and see.
If u want them to be readable like that then u have to use fprintf() for writting and fscanf() for reading and in that case u wont be able to do a random access because u dont know how many bytes is taken by each data.
Read more on file operations in C/C++. U need to know more about them.
hello, can somebody help me in learning file i/o in c++?
i would be very grateful
just google it
Why not :
bool isEmpty() { return ( position.size == 0 ); } //make it position.size()
yes thats the perfect solution
ok. i got ur point. but one more question plz. only one client needs to connect with the other client. or both of the clients need to call the command connect() ? in other words i need to send that socket to only 1 client or to the both?
don't do a connect() from a client to client directly.
A client will always connect to the central server. Design your protocol in a manner as follows:
client ----------- connect() ---------> server
........................................(add the incoming client socket to a list)
client <------(client socket list)--- server
client --------particular socket----> server
(choose from other socket list)
client <----------ack------------------ server
(now this client have the particular socket where to write which the other client will read from, so just start writting to this socket.)
**** The above is not a full fleged protocol because it is not sending any info to the other client.
So first design your protocol properly, then only u should go for the implementation.
U have to design different message formats, say MSG_ACK, MSG_SOCKLIST, MSG_TEXT, etc.
May that help.
oh thanks man i thought addresses is same as indexes but i guess i was wrong
u r welcome.......
1) suggest you define macros STDIN and STDOU instead of hardcoding 0 and 1 in the read/write statements
#define STDOUT 1 #define STDIN 0
2) use sizeof operator to get the size of data
write( STDOUT, buf4, strlen(buf4) ); read( STDIN, myArr[i].gender, sizeof(myArr[i].gender) ); write( STDOUT, buf5, strlen(buf5) ); read( STDIN, myArr[i].status, sizeof(myArr[i].status) );
[edit] And what they said above ^^^^ [/edit]
Hi,
Please consider the following example code
char a, c;
scanf("%c", &c);
scanf("%c", &a);
//printf("[c:%c, a:%c]\n", c, a);
when we run it the second scanf() statement takes the '\n' that is entered while entering a character for the first scanf().
It may be because the input buffer is not reset after the first scanf().
But I tried doing an fflush(stdin) before the second scanf() and it still doesn't take the second input.
Can u put some light on this issue.
Thanks in advance
its because u r taking only one character in the read for the gender. After u enter m/f when u hit enter key it actually takes the '\n' as an input for the next read statement. Hence the next read() is skipped.
write( 1, buf4, strlen(buf4) );
read( 0, myArr[i].gender, 1 );
write( 1, buf5, strlen(buf5) );
read( 0, myArr[i].status, 1 );
correct your code as
write( 1, buf4, strlen(buf4) );
read( 0, myArr[i].gender, 2 );// replaced 1 with 2
write( 1, buf5, strlen(buf5) );
read( 0, myArr[i].status, 2 );// replaced 1 with 2
Also increase the buffer size for gender and status by 1.
it will work...........
i m making a messenger in linux. a command line application. here is a server and 2 clients.both the clients are connected to the server but not with each other. i want to connect those 2 clients with each other. i m the server. how can i do that?
plz tel me a way out.thanx
In the server u have to maintain a list of the connected client sockets. U can send this list(with some alias for display as name say) to the clients. The clients will chose from the list and then u can try writting to that socket from the client itself.
I have never tried this in C/C++ though.
But i think u can give a try.
This was just a suggestion. I am not very sure about its workability.
yes but that should be copy the index which isn't space or tab but see now i changed to use index array members rather than addresses it worked fine i want to know why ?
#include <stdio.h> #include <ctype.h> #define STOP -1 int GetLine(char *SzBuff) { int i=0; char c; for( i=0;(c=getchar())!='\n';i++) { SzBuff[i]=c; if(c==EOF) { SzBuff[i]='\0'; return -1; } } SzBuff[i]='\0'; return i; } int IsDel(char c) { if(c==' ' || c=='\t') return 1; return 0; } void RemoveBlanks(char * SzString) { int j=0; for(int i=0;SzString[i]!=0;i++) if(!IsDel(SzString[i])) SzString[j++]=SzString[i]; SzString[j]='\0'; return; } int main(void) { char SzName[100]; while(GetLine(SzName)!=STOP) { RemoveBlanks(SzName); if(SzName[0]=='\0')//skip blank lines continue; puts(SzName); } return 0; }
hi i got the problem. u used
SzString++;
previously which moves the char * by one character. If previously u had
p = "this";
p++;
// p will now point to the string "his" only............
that was the only problem........
u are modifying the input string before checking the whole of it in the method RemoveBlanks() as follows:
while(*SzString)
{
if(!IsDel(*SzString))
{ //wtf why the hell does it keep incrementing !!!
SzString[j++]=*SzString;
//putchar(SzString[j]);//used for debug
//getchar();
//j++;
}
SzString++;
//this should ****en end the loop why the hell it keep going on incrementing
}
better take a temp string and put the characters in there and finlly modify the inputted string
i want solution program for this program......
Given a string and a non-empty substring sub, compute the largest substring which starts and ends with sub and return its length.
test cases:
strDist("catcowcat", "cat") → 9
strDist("catcowcat", "cow") → 3
strDist("cccatcowcatxx", "cat") → 9
1> we are not here to do your homework
2> use strncmp() to solve your problem.
3> Try to solve it yourself. Its not very hard.
4> if u are stuk somewhere just post your problem here. We can help.
gud luck............
i tried doing it....but still its showing garbage values.,....what else can be done??
check the attachments.........
It does what u r trying to do. I have tested them
#include<stdio.h>
typedef struct
{
char name[20];
int no;
float price;
}product;
product read_rec()
{
product p;
printf("enter the name, number and price\n");
scanf("%s%d%f",&p.name,&p.no,&p.price);
return p;
}
void main()
{
FILE *fp;
product *p;
p = (product *)malloc(sizeof(product));
int index;
fp = fopen("records","r");
if(fp==NULL)
{
printf("error in opening file\n");
exit(0);
}
printf("\nEnter index(>=0)=", index);
scanf("%d", &index);
fseek(fp, (index-1)*sizeof(product), SEEK_SET);
if(feof(fp))
{
printf("index out of bound(feof)");
return;
}
if(fread(p,sizeof(product), 1, fp)!=1)
{
printf("index out of bound(less)");
return;
}
printf("%d:%s, price:%.2f\n", p->no, p->name, p->price);
fclose(fp);
}
#include<stdio.h>
typedef struct
{
char name[20];
int no;
float price;
}product;
product read_rec()
{
product p;
printf("enter the name, number and price\n");
scanf("%s%d%f",&p.name,&p.no,&p.price);
return p;
}
void main()
{
FILE *fp;
product p;
int size,i,k;
fp = fopen("records","w");
if(fp==NULL)
{
printf("error in opening file\n");
exit(0);
}
printf("enter the number of records\n");
scanf("%d",&size);
printf("enter the records\n");
for(i=0;i<size;i++)
{
p=read_rec();
fwrite(&p, sizeof(product), 1, fp);
}
// fwrite(p,(size*sizeof(p)),size,fp);
fclose(fp);
}
use a typecast in the throw tag
throw (char *)"Value is zero";
it should work. I have tried in my machine by using it.
1>
int number = rand() % 100 + 1;
produces no. from 1 to 100.
u need no. in the range 65-122
so u have to do
int number = rand() % (122-65) + 65;
2>
while ((number >= 65)&&(number <= 90)||(number >= 97)&&(number <= 122))
{
newNode = new nodeType;
newNode->info = char(number);
newNode->link = NULL;
if (first == NULL)
{
first = newNode;
last = newNode;
}
else
{
last->link = newNode;
last = newNode;
}
}
where have u modified "number".