Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,169 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,883 people online right now. Registration is fast and FREE... Join Now!




Program solves Digital logic Gates problems

 
Reply to this topicStart new topic

Program solves Digital logic Gates problems

Hector6008
15 Dec, 2007 - 03:05 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 25


My Contributions
would you plz tell why does the program crash????
CODE

#include<iostream>
#include<conio.h>
#define num 10
using namespace std;
//===============================================================================
void DecToBin(int a,int b,int **pi,int e,int &k)
{
    int x=a;
    int y=b;
    int i=num-1;
    int j=num-1;
    int rx,ry;
    while(x>0)
    {
        rx=x%2;
        pi[e][i]=rx;
        i--;
        x=x/2;
    }
    while(y>0)
    {
        ry=y%2;
        pi[e+1][j]=ry;
        j--;
        y=y/2;
    }
    if(j>i)
    {
        k=i+1;
        for(int m=i+1;m<j+1;m++)
        {
            pi[e+1][m]=0;
        }
    }
    if(i>j)
    {
        k=j+1;
        for(int n=j+1;n<i+1;n++)
        {
            pi[e][n]=0;
        }
    }
    if(i==j)
    {
        k=i+1;
    }

    cout<<"A= ";
    for(int q=k;q<num;q++)
    {
        cout<<pi[e][q];
    }
    cout<<endl;
    cout<<"B= ";
    for(int l=k;l<num;l++)
    {
        cout<<pi[e+1][l];
    }
    cout<<endl;
}
//===============================================================================
void OR(int **pi,int e,int **po,int f,int k)
{
    int x[num];
    for(int m=k;m<num;m++)
    {
        if(pi[e][m]==0&&pi[e+1][m]==0)
        {
            x[m]=0;
        }
        else
        {
            x[m]=1;
        }
    }
    for(int p=k;p<num;p++)
    {
        po[f][p]=x[p];
    }
    cout<<"F= ";
    for(int n=k;n<num;n++)
    {
        cout<<po[f][n];
    }
    cout<<endl;
}
//===============================================================================
void AND(int **pi,int e,int **po,int f,int k)
{
    int x[num];
    for(int m=k;m<num;m++)
    {
        if(pi[e][m]==1&&pi[e+1][m]==1)
        {
            x[m]=1;
        }
        else
        {
            x[m]=0;
        }
    }
    for(int p=k;p<num;p++)
    {
        po[f][p]=x[p];
    }
    cout<<"E= ";
    for(int n=k;n<num;n++)
    {
        cout<<po[f][n];
    }
    cout<<endl;
}
//===============================================================================
void NAND(int **pi,int e,int **po,int f,int k)
{
    int x[num];
    for(int m=k;m<num;m++)
    {
        if(pi[e][m]==1&&pi[e+1][m]==1)
        {
            x[m]=0;
        }
        else
        {
            x[m]=1;
        }
    }
    for(int p=k;p<num;p++)
    {
        po[f][p]=x[p];
    }
    cout<<"G= ";
    for(int n=k;n<num;n++)
    {
        cout<<po[f][n];
    }
    cout<<endl;
}
//===============================================================================
void NOR(int **pi,int e,int **po,int f,int k)
{
    int x[num];
    for(int m=k;m<num;m++)
    {
        if(pi[e][m]==0&&pi[e+1][m]==0)
        {
            x[m]=1;
        }
        else
        {
            x[m]=0;
        }
    }
    for(int p=k;p<num;p++)
    {
        po[f][p]=x[p];
    }
    cout<<"H= ";
    for(int n=k;n<num;n++)
    {
        cout<<po[f][n];
    }
    cout<<endl;
}
//===============================================================================
int Menu()
{
    int c;
    cout<<"<Select from the menu>\n";
    cout<<"1) AND gate\n";
    cout<<"2) OR gate\n";
    cout<<"3) NAND gate\n";
    cout<<"4) NOR gate\n";
    cout<<"5) XOR gate\n";
    cout<<"6) XNOR gate\n";
    cout<<"7) NEGATIVE AND gate\n";
    cout<<"8) NEGATIVE OR gate\n";
    cin>>c;
    return ©;
}
//===============================================================================
void NEGAND(int **pi,int e,int **po,int f,int k)
{
    int x[num];
    for(int m=k;m<num;m++)
    {
        if(pi[e][m]==0&&pi[e+1][m]==0)
        {
            x[m]=1;
        }
        else
        {
            x[m]=0;
        }
    }
    for(int p=k;p<num;p++)
    {
        po[f][p]=x[p];
    }
    cout<<"K= ";
    for(int n=k;n<num;n++)
    {
        cout<<po[f][n];
    }
    cout<<endl;
}
//===============================================================================
void XNOR(int **pi,int e,int **po,int f,int k)
{
    int x[num];
    for(int m=k;m<num;m++)
    {
        if((pi[e][m]==0&&pi[e+1][m]==0)||(pi[e][m]==1&&pi[e+1][m]==1))
        {
            x[m]=1;
        }
        else
        {
            x[m]=0;
        }
    }
    for(int p=k;p<num;p++)
    {
        po[f][p]=x[p];
    }
    cout<<"J= ";
    for(int n=k;n<num;n++)
    {
        cout<<po[f][n];
    }
    cout<<endl;
}
//===============================================================================
void NEGOR(int **pi,int e,int **po,int f,int k)
{
    int x[num];
    for(int m=k;m<num;m++)
    {
        if(pi[e][m]==1&&pi[e+1][m]==1)
        {
            x[m]=0;
        }
        else
        {
            x[m]=1;
        }
    }
    for(int p=k;p<num;p++)
    {
        po[f][p]=x[p];
    }
    cout<<"L= ";
    for(int n=k;n<num;n++)
    {
        cout<<po[f][n];
    }
    cout<<endl;
}
//===============================================================================
void  XOR(int **pi,int e,int **po,int f,int k)
{
    int x[num];
    for(int m=k;m<num;m++)
    {
        if((pi[e][m]==0&&pi[e+1][m]==1)||(pi[e][m]==1&&pi[e+1][m]==0))
        {
            x[m]=1;
        }
        else
        {
            x[m]=0;
        }
    }
    for(int p=k;p<num;p++)
    {
        po[f][p]=x[p];
    }
    cout<<"I= ";
    for(int n=k;n<num;n++)
    {
        cout<<po[f][n];
    }
    cout<<endl;
}//==============================================================================
void main()
{

    int **pi,**po;
    int a,b,k,nl,ni,no,ng;
    int e=0,f=0;
    int z=Menu();
    cout<<"Enter the number of levels\n";
    cin>>nl;
    cout<<"Enter the number of inputs\n";
    cin>>ni;
    pi=new int *[ni];
    for(int i=0;i<ni;i++)
        pi[i]=new int [num];
    cout<<"Enter the number of outputs\n";
    cin>>no;
    po=new int *[no];
    for(int j=0;j<no;j++)
        po[j]=new int [num];
    z;
    for(int X=0;X<nl;X++)
    {
        cout<<"Enter the inputs in Decimal\n";
        cin>>a>>b;
        DecToBin(a,b,pi,e,k);
        e+=2;
    switch(z)
    {
    case 1:
        {
            AND(pi,e,po,f,k);
            f++;
            break;
        }
    case 2:
        {
            OR(pi,e,po,f,k);
            f++;
            break;
        }
    case 3:
        {
            NAND(pi,e,po,f,k);
            f++;
            break;
        }
    case 4:
        {
            NOR(pi,e,po,f,k);
            f++;
            break;
        }
    case 5:
         {
             XOR(pi,e,po,f,k);
             f++;
             break;
         }
    case 6:
        {
            XNOR(pi,e,po,f,k);
            f++;
            break;
        }
    case 7:
        {
            NEGAND(pi,e,po,f,k);
            f++;
            break;
        }
    case 8:
        {
            NEGOR(pi,e,po,f,k);
            f++;
            break;
        }
    default:
        cout<<"Invalid gate please try again \n";
    }
    }
    for(int z=0;z<num;z++)
    {
        delete pi[z];
        delete po[z];
    }
    delete pi;
    delete po;
}
    


This post has been edited by Hector6008: 15 Dec, 2007 - 03:07 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Program Solves Digital Logic Gates Problems
15 Dec, 2007 - 10:46 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You have a lot of problems in this code. You should take a close look at how you are using pi and po because for instance you initialize one dimension, but you don't initialize the other dimension before you attempt to use them (you have an array of arrays full of nulls).

Because of this your AND is breaking to hell. Your way of reading in user input numbers using cin is also known to be problematic. Try using getline which won't go into a fail state like cin will if it encounters data it doesn't like.

Clean up the code a bit as well and make it more readable and then we can probably help further.

Good luck! smile.gif
User is offlineProfile CardPM
+Quote Post

mattman059
RE: Program Solves Digital Logic Gates Problems
16 Dec, 2007 - 05:17 AM
Post #3

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 340


Dream Kudos: 175
My Contributions
QUOTE
Because of this your AND is breaking to hell


maybe even to hell and back....

One major problem i notice is something that is easy to fix even for the beginner programmer...Use variable names that are significant instead of having a variable name using all of the letters of the alphabet use words that let others know what youre doing (it makes debugging a LOT easier) like for example:

AND(pi,e,po,f,k)

Just looking at that I cant tell if pi is PI (ie 3.14159) or something else, also, i dont know what any of those other variables are supposed to stand for, sure if i read through your code maybe a dozen times I would understand, but it is your responsibility to make sure that I dont have to, by using things like "count" instead of just x and things like that.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Program Solves Digital Logic Gates Problems
16 Dec, 2007 - 10:46 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,199



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
QUOTE(mattman059 @ 16 Dec, 2007 - 05:17 AM) *

QUOTE
Because of this your AND is breaking to hell


maybe even to hell and back....

One major problem i notice is something that is easy to fix even for the beginner programmer...Use variable names that are significant instead of having a variable name using all of the letters of the alphabet use words that let others know what youre doing (it makes debugging a LOT easier) like for example:

AND(pi,e,po,f,k)

Just looking at that I cant tell if pi is PI (ie 3.14159) or something else, also, i dont know what any of those other variables are supposed to stand for, sure if i read through your code maybe a dozen times I would understand, but it is your responsibility to make sure that I dont have to, by using things like "count" instead of just x and things like that.


That is an excellent note mattman. I always recommend that programmers make very readable and understandable variable names. It makes it easier for other programmers to see what is going on (it is self documenting). Good call. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 12:23AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month