Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
need codes for k-map solver c language. k-map is basically used to minimize the number of gates to used

What I have tried:

#include<stdio.h>
#include<conio.h>
void main()
{
 int i,j,k[8]={0,0,0,0,0,0,0,0},a,f=0,y[13]={0,0,0,0,0,0,0,0,0,0,0,0,0},l[8]={0,0,0,0,0,0,0,0};
 int s,t;
 printf("\t\t\t\tK-Map solver in 3 variable\n\t\t\t\t===========================\n");
printf("enter total minters you want to input:\t");
scanf("%d",&a);

if(a>8)
{
   printf("\nInvalid because maximum  number of minterms in 3 variables is 8");
   goto *1;
}

t:
for(i=0;i<a;i++)
{
       printf("%d""th minterm:",i);
       scanf("%d",&k[i]);
}

printf("\n entered minterms are:-\t");
for(i=0;i<=7;i++)
{
 printf("%d",k[i]);
}

printf("\n if entered minterms are correct press 1 or press 0 to go back:");
scanf("%d",&s);
if(s==0)
{
goto t;
}
if(s==1)
{
printf("\n Final matrix:-");
for(i=0;i<=7;i++)
{
 for(j=0;j<=7;j++)
 {
       if(k[i]-j==0)
       {
    l[j]=1;

       }
 }
}
printf("\t");
for(i=0;i<=7;i++)
{
 printf("%d",l[i]);
}
printf("\n");
printf("the ans is:");
for(i=0;i<=7;i++)
{              if(l[i]==1)
      {
      f++;
      }
}
if(f==8)
{
 printf("x=1");
}
else
{
 printf("x=");

       if(l[0]==1&&l[1]==1&&l[4]==1&&l[5]==1)
       {
  y[0]=1;
  printf("B'+");
       }
       if(l[2]==1&&l[3]==1&&l[6]==1&&l[7]==1)
       {
  y[1]=2;

  printf("B+");
       }

       if(l[0]==1&&l[1]==1&&l[2]==1&&l[3]==1)
       {
  y[2]=3;

  printf("A'+");
       }
       if(l[4]==1&&l[5]==1&&l[6]==1&&l[7]==1)
        {
  y[3]=4;

  printf("A+");
       }
       if(l[0]==1&&l[2]==1&&l[4]==1&&l[6]==1)
       {
  y[4]=5;
  printf("C'+");
       }
       if(l[1]==1&&l[3]==1&&l[7]==1&&l[5]==1)
       {
  y[5]=6;

  printf("C+");
       }
       if(y[4]!=5&&y[0]!=1&&l[0]==1&&l[4]==1)
       {
  y[6]=7;
  printf("B'C'+");
       }
       if(y[0]!=1&&l[1]==1&&l[5]==1&&y[5]!=6)
       {
  y[7]=8;
  printf("B'C+");
       }
       if(y[5]!=6&&y[1]!=2&&l[3]==1&&l[7]==1)
       {
  y[8]=9;
  printf("BC+");
       }

       if(y[4]!=5&&y[1]!=2&&l[2]==1&&l[6]==1)
       {
  y[9]=10;
  printf("B'C+");
       }
       if(y[0]!=1&&l[0]==1&&l[1]==1&&y[2]!=3)
       {
  y[10]=11;
  printf("B'A'+");
       }
       if(y[1]!=2&&l[3]==1&&l[2]==1&&y[2]!=3)
       {
  y[11]=12;
  printf("A'B+");
       }
       if(y[1]!=2&&l[7]==1&&l[6]==1&&y[3]!=4)
       {
  y[12]=13;
  printf("AB+");
       }
       if(y[0]!=1&&l[4]==1&&l[5]==1&&y[3]!=4)
       {
  y[13]=14;
  printf("B'A+");
       }
       if(y[5]!=6&&l[3]==1&&l[1]==1&&y[2]!=3)
       {

  printf("A'C+");
       }
       if(y[5]!=6&&l[5]==1&&l[7]==1&&y[3]!=4)
       {

  printf("AC+");
       }
       if(y[2]!=3&&y[4]!=5&&l[0]==1&&l[2]==1)
       {
  printf("A'C'+");
       }
       if(y[4]!=5&&l[4]==1&&l[6]==1&&y[3]!=4)
       {

  printf("AC'+");
       }
       if(l[1]==0&&l[4]==0&&l[0]==1&&l[2]==0)
       {

  printf("A'B'C'+");
       }
       if(l[0]==0&&l[5]==0&&l[1]==1&&l[3]==0)
       {
  printf("A'B'C+");
       }
       if(l[1]==0&&l[2]==0&&l[3]==1&&l[7]==0)
       {
  printf("A'BC+");
       }
       if(l[3]==0&&l[6]==0&&l[0]==0&&l[2]==1)
       {
  printf("A'B'C+");
       }
       if(l[0]==0&&l[4]==1&&l[6]==0&&l[5]==0)
       {
  printf("AB'C'+");
       }
       if(l[1]==0&&l[4]==0&&l[5]==1&&l[7]==0)
       {
  printf("AB'C+");
       }
       if(l[3]==0&&l[5]==0&&l[6]==0&&l[7]==1)
       {
  printf("ABC+");
       }
       if(l[2]==0&&l[4]==0&&l[6]==1&&l[7]==0)
       {
  printf("ABC'+");
       }
}
getch();
}
}
Posted
Updated 5-Sep-21 20:22pm

This is not a code to order service. While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

And frankly, I wouldn't even look at that code: poorly indented, full of goto's, magic numbers, single character variables, and a total lack of anything resembling documentation. Any code review would not go well at all ...

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
You need to understand k-map at first. Maybe this video tutorial helps you.
Your code is very messy. This big if clause is trap for bugs.
I would use better names, use structs and write some functions like
C
bool isCause(int *data, int first, ... )
Please avoid gotos and use returns. In this cases it is best to also print some info for the user.

Good luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900