Program in C and C++ for RS flip-flop

Program in C and C++ for RS flip-flop

RS Flip-Flop


RS flip-flop is capable of storing 1 bit. It has three inputs, labeled S(for set), R(for reset) and C(for clock). 
image source-google | image by-Wikimedia Commons

Circuit Diagram:

image source-google | image by-Wikimedia Common

Q(t+1)=S + R'.Q(t)

Program in C:




 #include <stdio.h>
  typedef char bit;

 bit rs(bit S,bit R,bit Q)
 {
  return (S)|(Q&(~(R)));
  }
 
int main()
{
 int i,j,k,a,res=0;
 int x,y,z;
 char b;
 printf("1.Truth Table of RS flip flop");
 printf("\n2.I/O operation\n");
 printf("Enter your choice: ");
 scanf("%d",&a);
 switch(a) 
 {
  case 1:
  printf("S  R  Q(t) | Q(t+1)\n");
  for(i=0;i<2;i++)
  {
   for(j=0;j<2;j++)
   {
    for(k=0;k<2;k++)
    {
    if(i==1&&j==1)
    {
     char r='x';
     printf("%d  %d  %d  ",i,j,k);
     printf("  | %c\n",r);
     }
    else
    {
     int res=rs(i,j,k);
     printf("%d  %d  %d  ",i,j,k);
     printf("  | %d\n",res);
     }
    }
   }
  }
 break;
  
case 2:
 printf("Enter the value of S,R,Q(t):");
 scanf("%d%d%d",&x,&y,&z);
 
  res=rs(x,y,z);
  printf("Q(t+1)= %d\n\n",res);

  break;

 default:
  printf("invalid input");   
         
  }
 printf("do you want to continue(Y/N):");
 scanf(" %c",&b);
  if(b=='Y'||b=='y')
    {
     main();
 }
 else{
  return 0;
    }
}  
      

Output:



 1. Truth Table of RS flip flop
 2.I/O operation
Enter your choice: 1
 S  R  Q(t) | Q(t+1)
 0  0  0    | 0
 0  0  1    | 1
 0  1  0    | 0
 0  1  1    | 0
 1  0  0    | 1
 1  0  1    | 1
 1  1  0    | x
 1  1  1    | x
do you want to continue(Y/N): y
1. Truth Table of RS flip flop
2.I/O operation
Enter your choice: 2
Enter the value of S, R & Q(t): 1 0 1
Q(t+1)= 1

do you want to continue(Y/N): N

--------------------------------
Process exited after 50.78 seconds with return value 0
Press any key to continue . . .


Program in C++:




 #include <iostream>
 using namespace std;
  typedef char bit;

  bit rs(bit S,bit R,bit Q)
  {
 
 return (S)|(Q&(~(R)));
  
   }

  
int main()
{
 int i,j,k,a,res=0;
 char b;
 cout<<"1.Truth Table of RS flip flop\n";
 cout<<"2.I/O operation\n";
 cout<<"Enter your choice: ";
 cin>>a;
switch(a) 
{
 case 1:
 cout<<"S  R  Q(t) | Q(t+1)"<<endl;
 for(i=0;i<2;i++)
  {
   for(j=0;j<2;j++)
   {
    for(k=0;k<2;k++)
    {
    if(i==1&&j==1)
    {
     char r='x';
     cout<<i<<"  "<<j<<"  "<<k<<"  ";
     cout<<"  | "<<r<<"\n";
     }
     else
     {
     int res=rs(i,j,k);
     cout<<i<<"  "<<j<<"  "<<k<<"  ";
     cout<<"  | "<<res<<"\n";
     } 
    }
   }
  }
   break;
  case 2:
  int s,r,t;
  cout<<"Enter the value of S,R, Q(t):";
  cin>>s>>r>>t;
  if(s==1&&r==1&&(t==0||t==1))
  {
  cout<<"Q(t+1)= x\n\n";
  }
  else
  {
   res=rs(s,r,t);
   cout<<"Q(t+1)= "<<res<<"\n\n";
   }
  break;
  }
  cout<<"do you want to continue(Y/N):";
  cin>>b;
  if(b=='Y'||b=='y')
    {
     main();
 }
 else{
  return 0;
    }
} 
      

Output:



 1. Truth Table of RS flip flop
 2.I/O operation
Enter your choice: 1
 S  R  Q(t) | Q(t+1)
 0  0  0    | 0
 0  0  1    | 1
 0  1  0    | 0
 0  1  1    | 0
 1  0  0    | 1
 1  0  1    | 1
 1  1  0    | x
 1  1  1    | x
do you want to continue(Y/N): y
1. Truth Table of RS flip flop
2.I/O operation
Enter your choice: 2
Enter the value of S, R & Q(t): 1 0 1
Q(t+1)= 1

do you want to continue(Y/N): N

--------------------------------
Process exited after 50.78 seconds with return value 0
Press any key to continue . . .



Algorithm:

STEP 1: Start

STEP 2: Define 'rs' function

STEP 3: Perform the operation (Q(t+1)=S + R'.Q(t)) where R, Q, J are one bit binary number

STEP 4: Invoke main function

STEP 5: Initialize variables

STEP 6: Print "Enter your choice"

STEP 7: Input choice 1 or 2

STEP 8: If the user enters 1 or 2 then follow the below steps else flow goes to default case & exit the program Switch(operator)

STEP 8.a case 1:
STEP 8.a.1: Check for condition (i<2), if Condition is true, go to step 7 otherwise go to step 8.b

STEP 8.a.2: Check for condition (j<2), if Condition is true, go to further step otherwise go to step 8.a.1

STEP 8.a.3: Check for condition (k<2), if Condition is true, go to further steps otherwise go to step 8.a.2

STEP 8.a.4: Call 'rs' function for value of Q(t+1)

STEP 8.a.5: Print i,j,k and Q(t+1) for truth table

STEP 8.b case 2:
STEP 8.b.1: Input three one bit binary number

STEP 8.b.2: Call 'rs' function for value of Q(t+1)

STEP 8.a.3: Print Q(t+1)

STEP 9: Stop

Comments :

Post a Comment