#help with problem

22 messages · Page 1 of 1 (latest)

unreal cypress
#

hi, i have written a parenthesis program where you can write a sequence of parenthesis for example {{[()]}} and the program determines if that sequence is valid or not.
these are the rules;

* Square brackets [ ] can contain only [ ] and ( ) brackets.
* Curly braces { } can contain { }, [ ] and ( ) brackets```. i have tested it and it works for the test cases but when i put in [[{()}]] it says valid even tho it is supposed to be invalid so i dont really know what is wrong.
deft hatchBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

unreal cypress
#
#include "Parenthesis.c"

int main()
{
    char expr[MAX_SIZE];
    printf("Enter expression: ");
    scanf("%s", expr);

    if (is_balanced(expr))
    {
        printf("Valid expression\n");
    }
    else
    {
        printf("Invalid expression\n");
    }

    return 0;
}``` this is the code in my main file
#
#include <stdlib.h>

#define MAX_SIZE 100

typedef struct
{
    char arr[MAX_SIZE];
    int top;
} Stack;

void init_stack(Stack *s)
{
    s->top = -1;
}

int is_full(Stack *s)
{
    return s->top == MAX_SIZE - 1;
}

int is_empty(Stack *s)
{
    return s->top == -1;
}

void push(Stack *s, char c)
{
    if (is_full(s))
    {
        printf("Error: Stack Overflow\n");
        exit(EXIT_FAILURE);
    }
    s->arr[++s->top] = c;
}

char pop(Stack *s)
{
    if (is_empty(s))
    {
        printf("Error: Stack Underflow\n");
        exit(EXIT_FAILURE);
    }
    return s->arr[s->top--];
}

char peek(Stack *s)
{
    if (is_empty(s))
    {
        printf("Error: Stack is empty\n");
        exit(EXIT_FAILURE);
    }
    return s->arr[s->top];
}

int is_balanced(char exp[])
{
    Stack s;
    init_stack(&s);

    for (int i = 0; exp[i] != '\0'; i++)
    {
        if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[')
        {
            push(&s, exp[i]);
        }
        else if (exp[i] == ')' || exp[i] == '}' || exp[i] == ']')
        {
            if (is_empty(&s))
            {
                return 0;
            }
            char top = pop(&s);
            if ((exp[i] == ')' && top != '(') ||
                (exp[i] == '}' && top != '{') ||
                (exp[i] == ']' && top != '['))
            {
                return 0;
            }
        }
    }

    while (!is_empty(&s))
    {
        char top = pop(&s);
        if (top == '{')
        {
            return 0;
        }
    }

    return 1;
}``` and this is my parenthesis.c file
upbeat crater
#

what debugger do you use

unreal cypress
#

do you mean when i compile?

upbeat crater
#

no once you compiled and want to test the code, you usually use a debugger (commonly included in an IDE or just by itself)

unreal cypress
#

i dont think i have ever done that, we have not been tought to do that

upbeat crater
#

what are you using to write/compile your code?

unreal cypress
#

vsc

upbeat crater
#

hm, so I guess your on windows. if you are a student don't you have access to visual studio c++?

unreal cypress
#

yes i am, i dont know since i am writing in c and dont have to use c++

upbeat crater
#

it works for c and c++. its the better more powerful tool and it comes with ways to step through the execution of your code, to see what its actually doing.

unreal cypress
#

oh okay maybe they will teach us that later but i since they have not mentioned it i dont need to use it now

upbeat crater
unreal cypress
#

okay thank i will look in to that later on

#

but i still dont know what is wrong with the code

upbeat crater
#

something something people say about "teach a man how to fish". you are basically asking for someone to debug for you 😅

unreal cypress
#

i am not asking you to fix the code i just want help to see where the problem is

upbeat crater
#

my guess would be that you are not actually enforcing the restrictions of your rule-set. you just match the brackets, which in your given "invalid" example, they do.

deft hatchBOT
#

This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.

unreal cypress
#

!solved