How to convert a decimal to binary in C programming using stack

user11231270

I'm trying a C programming code which will convert a decimal number into binary number and the binary values will be stored in a stack

According to my code, when running it shows some error responses. When trying to display the binary number using peek method, the application runs without an end.

#include<stdio.h>
#include<stdlib.h>
#define MAX 50
typedef enum{FALSE, TRUE} boolean;
typedef struct stack{
    int top;
    int a[MAX];
} stack;
void CreateStack(stack *s){
    s->top = -1;
}
boolean isEmpty(stack *s){
    return (s->top == -1);
}
boolean isFull(stack *s){
    return(s->top == MAX - 1);
}
void push(stack *s, int data){
    if(isFull(s)){
        exit(1);
    }
    else{
        s->top = s->top + 1;
        s->a[s->top] = data;
    }
}
int pop(stack *s){
    if(isEmpty(s)){
        exit(1);
    }
    else{
        return s->a[s->top];
        s->top = s->top - 1;
    }
}
int peek(stack *s){
    return s->a[s->top];
}
void binary(stack *s, int num){
    int n;
    while(num != 0){
        if(!isFull(s)){
             n = num % 2;
            push(s,n);
            num = num / 2;
        }
        else{
            exit(1);
        }
    }
}
void main() {
    stack s;
    CreateStack(&s);
    int num,n;
    printf("Enter the decimal number: ");
    scanf("%d",&num);
    binary(&s,num);
    printf("Top = %d\n",peek(&s));
    while(!isEmpty(&s)){
        printf("%d ",pop(&s));
    }
}
ReAl

pop() contains dead code — it returns value but top decrement placed after the return operator and will be newer reached.

int pop(stack *s){
    if(isEmpty(s)){
        exit(1);
    }
    else{
        return s->a[s->top];
        s->top = s->top - 1; // <------------
    }
 }

Change it in following manner:

int pop(stack *s){
    if(isEmpty(s)){
        exit(1);
    }
    else{
        int temp = s->a[s->top];
        s->top = s->top - 1;
        return temp;
    }
 }

p.s. As for me, else keyword can be omitted here but it is taste issue:

int pop(stack *s) {
    int temp;

    if(isEmpty(s))
        exit(1);

    temp = s->a[s->top];
    s->top = s->top - 1;
    return temp;
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C program to convert a decimal number to binary string

How to convert binary fraction to decimal

how to convert binary string to decimal?

How to convert binary string value to decimal

How to convert a decimal number to binary in Swift?

How to convert a binary to decimal in Swift?

Decimal to Binary convert in C

How to convert binary floating points to decimal fractions?

How to Convert Decimal to Binary in Assembly Language

Convert continues binary fraction to decimal fraction in C

How to convert binary to decimal using a for loop?

Decimal fraction conversion to binary - how to convert 0.1?

how to convert decimal into string using C#

How to convert binary into decimal using <bitset> library?

How to convert a decimal number into a binary number using STACK in python

how to convert decimal to binary by using repeated division in python

How to convert large binary to decimal?

C program to convert Decimal to Binary

How to convert Binary Coded Decimal to int in C++

how to convert decimal value to binary bits?

How to convert a decimal number to a binary number using recursive functions?

Binary to Decimal using recursion in C

Convert decimal to binary with billions number in C

Decimal to binary using c language

Kotlin: How to convert from binary to decimal

Convert decimal to binary and let binary length always eight using C++

How to convert binary value to decimal in hive/impala

Stuck in decimal to binary using C

How to solve binary to decimal with fraction in c#? Using winform, Im trying to convert binary numbers into decimal. I used the input function