Need Help In Decoder Programming

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2007
Posts: 5
Reputation: flamecly is an unknown quantity at this point 
Solved Threads: 0
flamecly flamecly is offline Offline
Newbie Poster

Need Help In Decoder Programming

 
0
  #1
Jan 9th, 2007
Hi, i'm new to this website. I have recently came across a qns given by a friend and i'm thinking how to do it. Hope u all can help me...

Here is the qns:

The circuit below shows a decoder circuit using 3-to-8 Decoder and 2-to-4 Decoder with logic gates.

*The Pic is in the attachment file.

Your task is to simulate the output logic level (ie: “0” or “1”) of each device in the above circuit. You will need to derive the following classes :

i) 3-to-8 Decoder
ii) 2-to-4 Decoder
iii) AND gate

Thank you for viewing and your help...
Attached Thumbnails
untitled.JPG  
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 486
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 48
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Need Help In Decoder Programming

 
0
  #2
Jan 10th, 2007
Well, a bitwise AND operation is performed using the '&' operator between two numbers
eg,
  1. int n = 6 & 12;
  2. // 0000 0110
  3. // &
  4. // 0000 1100
'n' will be 4

You'll need to know about Bitwise operations - (Binary arithmetic)
http://en.wikipedia.org/wiki/Bitwise_operation

In C++, the <bitset> library may come in useful.

have a look here for a 2-to-4 decoder
http://en.wikipedia.org/wiki/Image:Decoder_Example.svg
Last edited by Bench; Jan 10th, 2007 at 12:26 am. Reason: Had to disable smileys
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 327
Reputation: Colin Mac is on a distinguished road 
Solved Threads: 22
Colin Mac Colin Mac is offline Offline
Posting Whiz

Re: Need Help In Decoder Programming

 
0
  #3
Jan 10th, 2007
For a 2 input AND gate, you have 4 possible combination of inputs(0 to 3 in binary) . The output will only be a 1 if the two inputs are a 1, otherwise the output is a 0.

For a 2-4 bit decoder, you have an output for each combination of input. eg, If a=0 and b = 0 then D0= 0, or if a=0 and b=1 then D1=1 etc., just google the Truth Table for each chip. The 3-8 is the same with 3 inputs and 8 possible outputs...
Last edited by Colin Mac; Jan 10th, 2007 at 10:32 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 5
Reputation: flamecly is an unknown quantity at this point 
Solved Threads: 0
flamecly flamecly is offline Offline
Newbie Poster

Re: Need Help In Decoder Programming

 
0
  #4
Jan 16th, 2007
This is a small program i have try to do but with errors... can anyone help me to solve up the error?
  1.  
  2. //Miniproject
  3. #include <iostream>
  4. using namespace std;
  5. class decoder
  6. {
  7. protected:
  8. int decoderType;
  9. public:
  10. void setdecoderType(int);
  11. void showdecoder(void);
  12. };
  13. void decoder:: setdecoderType(int c)
  14. {
  15. decoderType = c;
  16. }
  17. void decoder::showdecoder()
  18. {
  19. switch (decoderType)
  20. {
  21. case '000': cout << "0" << endl;
  22. break;
  23. case '001': cout << "1" << endl;
  24. break;
  25. case '010': cout << "2" << endl;
  26. break;
  27. case '011': cout << "3" << endl;
  28. break;
  29. case '100': cout << "4" << endl;
  30. break;
  31. case '101': cout << "5" << endl;
  32. break;
  33. case '110': cout << "6" << endl;
  34. break;
  35. case '111': cout << "7" << endl;
  36. break;
  37. default:
  38. cout << "Wrong entry! Pls re-enter the 3 binary code!" << endl;
  39. break;
  40. }
  41. }
  42.  
  43.  
  44.  
  45. void main()
  46. {
  47. Decode code;
  48. int choice=0;
  49. cout << "This is a 3 to 8 decoder. ";
  50. cout << "Please enter the 3 binary code!(E.g. 001) "; cin >> choice;
  51. code.setdecodertype(choice);
  52. cout << "After decoding… The number is\n ";
  53. code.showdecoder();
  54. }
Thanks for helping in advance...
Last edited by WaltP; Jan 16th, 2007 at 4:24 am. Reason: Code Tags! When you enter your question, thos are instructions, not smudges, in the input box. Please read... And format your code so it can be read.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Need Help In Decoder Programming

 
0
  #5
Jan 16th, 2007
First of all:
use int main() instead of void main()
please use code tags, it makes your code easier to read as does indention

With that said:

you have a few typo's in your code:
  1.  
  2. void main()
  3. {
  4. Decode code;
Your class is called ' decoder ' not ' Decode', the line above to 'decoder code'
  1. code.setdecodertype(choice);
There is no function 'setdecodertype'. Change this to 'setdecoderType'.

Please keep in mind that C++ is casesensitive and therefore: a!=A

Regards Niek
Last edited by niek_e; Jan 16th, 2007 at 4:05 am.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Need Help In Decoder Programming

 
0
  #6
Jan 16th, 2007
Originally Posted by flamecly View Post
This is a small program i have try to do but with errors... can anyone help me to solve up the error?
What error? We can't help it you don't tell us.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 84
Reputation: rinoa04 is on a distinguished road 
Solved Threads: 4
rinoa04's Avatar
rinoa04 rinoa04 is offline Offline
Junior Poster in Training

Re: Need Help In Decoder Programming

 
1
  #7
Jan 16th, 2007
One of your coding mistake is as what niek e stated. But there is also another mistake. Your coding is prompting the user for integer but in your switch case it is comparing integer with character. You should eliminate the single quote since it represent character. Then I think your coding should be working after you correct all the small mistakes. Hope it helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 5
Reputation: flamecly is an unknown quantity at this point 
Solved Threads: 0
flamecly flamecly is offline Offline
Newbie Poster

Re: Need Help In Decoder Programming

 
0
  #8
Jan 17th, 2007
Thanks for your help for the previous errors...
Now i have rewrite the program but i still got some error and editing which need yours help.
Here is the code

//Miniproject

#include <iostream>
usingnamespace std;

class decoder 
{
protected:
int decoderType;
public:
void setdecoderType(int);    
void showdecoder(void);
};

void decoder:: setdecoderType(int c)
{
     decoderType = c;
}

void decoder::showdecoder()
{
switch (decoderType) 
      {
case 000: cout << "0" << endl;
break;
case 001: cout << "1" << endl;
break;
case 010: cout << "2" << endl;
break;
case 011: cout << "3" << endl;
break;
case 100: cout << "4" << endl;
break;
case 101: cout << "5" << endl;
break;
case 110: cout << "6" << endl;
break;
case 111: cout << "7" << endl;
break;      
default:  
                  cout << "\nWrong entry! Pls re-enter the 3 binary code!" << endl;
break;
      }
}

class twofour 
{
protected:
int twofourType;
public:
void settwofourType(int);    
void showtwofour(void);
};

void twofour:: settwofourType(int c)
{
     twofourType = c;
}

void twofour::showtwofour()
{
switch (twofourType) 
      {
case 00: cout << "0" << endl;
break;
case 01: cout << "1" << endl;
break;
case 10: cout << "2" << endl;
break;
case 11: cout << "3" << endl;
break;
default:  
                  cout << "\nWrong entry! Pls re-enter the 2 binary code!" << endl;
break;
      }
}

class and 
{
protected:
int andType;
public:
void setandType(int);        
void showand(void);
};

void and:: setandType(int c)
{
     andType = c;
}

void and::showand()
{
switch (andType) 
      {
case 00: cout << "0" << endl;
break;
case 01: cout << "0" << endl;
break;
case 10: cout << "0" << endl;
break;
case 11: cout << "1" << endl;
break;
default:  
                  cout << "\nWrong entry! Pls re-enter the 2 binary code!" << endl;
break;
      }
}

void main()
{
   decoder code;
twofour two;
and gate;
int  choice=0;

   cout << "This is a 3 to 8 decoder.\n";      
   cout << "Please enter the 3 binary code!(E.g. 001 to 111) "; cin >> choice; 
   code.setdecoderType(choice);
   cout << "After decoding, the number is: ";   
   code.showdecoder();

cout << "\nThis is a 2 to 4 decoder.\n";      
   cout << "Please enter the 2 binary code!(E.g. 00,01,10,11) "; cin >> choice; 
   two.settwofourType(choice);
   cout << "After decoding, the number is: ";   
   two.showtwofour();

cout << "\nThis is a And Gate.\n";      
   cout << "Please enter the 2 binary combination!(E.g. 00,01,10,11) "; cin >> choice; 
   gate.setandType(choice);
   cout << "The result is: ";   
   gate.showand();
}

First problem - For the 3 to 8 decoder, when 011 and 001 was keyed, it shows error instead of 3 and 1.

Second problem - how can i edit the program so the users can choose to use either one of the 3 decoder? I have try alot of times but all got errors.

Third problem - How can i do loop back if i key in a wrong value and i want to key in the value again after the error line has been shown?

Thanks...
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,841
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 298
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Need Help In Decoder Programming

 
0
  #9
Jan 17th, 2007
  1.  
  2. usingnamespace std;
There should be a space between using and namespace
First problem - For the 3 to 8 decoder, when 011 and 001 was keyed, it shows error instead of 3 and 1.
That's a problem with int's. 011 == 11 and is an invalid input. 010 will probably have the same problem. Try making the input to string and switch on that.
Or if you want to do it the ugly way: replace 011 with 11 and 010 with 10 in your switchcase.
Second problem - how can i edit the program so the users can choose to use either one of the 3 decoder? I have try alot of times but all got errors.
That's not that hard is it? Ask for a user input (1 for 3-8, 2 for etc.) Make a switch case on that and execute the right function.
Third problem - How can i do loop back if i key in a wrong value and i want to key in the value again after the error line has been shown?
you should work with returnvalues in your function. So if the input was valid return '0' and if it was invalid return '1'. Then call the function in a loop.

Regards Niek
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Need Help In Decoder Programming

 
0
  #10
Jan 17th, 2007
Originally Posted by niek_e View Post
That's a problem with int's. 011 == 11 and is an invalid input. 010 will probably have the same problem. Try making the input to string and switch on that.
Actually, 011 = 9. When you specify a numeric value with a leading 0, you are using an [search]octal[/search] value. You can't specify binary the way you are trying to do it. Enter it as a string and convert the string into it's binary equivalent. And in the switch, just use it's decimal value.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC