Balanced parathesis program

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void push(char a[], char ele, int *p);
char pop(char a[],int **q);
int check(char a[],int *p, char b1, char b2 );
int main()

{ int top =-1,i=0,x ;
char arr[100],a[100];
printf("enter the expression");

scanf("%s",&arr);
// printf("\n\n%c\n\n",arr[3]);

while(arr[i]!='\0')
{
push(a,arr[i],&top);
i++;
}
x=check(a,&top,'(',')');
x=check(a,&top,'[',']');
x=check(a,&top,'{','}');
if(x==1)
printf("balanced");
else
printf("unbalanced ");
return 0;
}
void push(char a[],char ele,int *p)
{
if((*p)>99)
{
printf("overflow!!");
return ;
}
(*p)++;
a[(*p)]=ele;
return ;
}
char pop(char a[],int **q)
{ //printf("%d\n\n",(**q));
if((**q)==-1)
{
printf("underflow");
return 0;
}
char x;
x = a[(**q)];
(**q)--;
return x;
}

int check(char a[],int *p,char b1, char b2)
{
int f1=0,f2=0,i;
while((*p)>=0)
{
if(b1==pop(a,(&p))&&b2==pop(a,(&p)))
{
f1=1;
}
else
f1=0;
}
return f1;
}

This is my code can someone help debug this ?
First off, please state exactly what kind of problem you are having.

Second, your program won't execute.
check is an undefined external.
You declare check at line 6 and subsequently call it at lines 20-22, but you did not implement the function.


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.

edit: lines 20-22, you call check three times in a row checking for (), [], and {}.
The second and third calls overlay the previous result (x).

edit: You have a simliar problem inside your while loop in check. You set f1 each time through the loop ignoring the previous condition of f1.
Last edited on
Topic archived. No new replies allowed.