I couldn’t understand why I am getting a SIGSEGV for the Transform the Expression (Problem code: ONP) problem.Its providing the same output on my machine as in the example.Here is my code-
#include <stdio.h>
#include <string.h>
char pop(int t);
void push(int c, int t);
int main()
{
int z;
scanf("%d", &z);
char a[100][400];
int i = 0;
int q = z;
while (q-- > 0)
{
scanf("%s",&a[i][0]);
int t;
for (t = 0; t < strlen(a[i]); t++) //loop to put the values and signs in the 2 stacks
{
if ((a[i][t] == '*') || (a[i][t] == '+') || (a[i][t] == '-') || (a[i][t] == '^'))
{
push(a[i][t], 2);
}
else if (a[i][t] == ')')
{
int y = pop(2);
push(y, 1);
}
else
{
push(a[i][t], 1);
}
}
int k = 0;
char c;
while ((c = pop(1)) !='\0') //loop to put elements in the array v
{
if (c != '(')
{
a[i][k++] = c;
}
}
a[i][k--] = '\0';
int m;
for (m=0; m != k; m++, k--)
{
char t = a[i][m];
a[i][m] = a[i][k];
a[i][k] = t;
}
}
int p;
for (p = 0; p <z ; p++)
printf("%s\n",a[i]);
return 0;
}
char ac[400];
char as[400];
int ic = 0;
int is = 0;
void push(int c,int t)
{
if (t == 1 && ic != 400)
ac[ic++] = c;
else if (t == 2 && is != 400)
as[is++] = c;
}
char pop(int t)
{
if (t == 1 && ic != 0)
return ac[--ic];
if (t == 2 && is != 0)
return as[--is];
return '\0';
}
Please help in finding the problem in my solution.