943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3248
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 9th, 2007
0

Need Help In Decoder Programming

Expand Post »
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
Click image for larger version

Name:	untitled.JPG
Views:	39
Size:	10.0 KB
ID:	2704  
Last edited by Nick Evan; May 11th, 2010 at 9:03 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flamecly is offline Offline
5 posts
since Jan 2007
Jan 10th, 2007
0

Re: Need Help In Decoder Programming

Well, a bitwise AND operation is performed using the '&' operator between two numbers
eg,
CPP Syntax (Toggle Plain Text)
  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
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006
Jan 10th, 2007
0

Re: Need Help In Decoder Programming

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.
Reputation Points: 78
Solved Threads: 22
Posting Whiz
Colin Mac is offline Offline
327 posts
since Sep 2006
Jan 16th, 2007
0

Re: Need Help In Decoder Programming

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flamecly is offline Offline
5 posts
since Jan 2007
Jan 16th, 2007
0

Re: Need Help In Decoder Programming

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:
C++ Syntax (Toggle Plain Text)
  1.  
  2. void main()
  3. {
  4. Decode code;
Your class is called ' decoder ' not ' Decode', the line above to 'decoder code'
C++ Syntax (Toggle Plain Text)
  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
Last edited by Nick Evan; May 11th, 2010 at 9:04 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jan 16th, 2007
0

Re: Need Help In Decoder Programming

Click to Expand / Collapse  Quote originally posted by flamecly ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Jan 16th, 2007
1

Re: Need Help In Decoder Programming

One of your coding mistake is as what nick 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.
Last edited by Nick Evan; May 11th, 2010 at 9:04 am.
Reputation Points: 52
Solved Threads: 4
Junior Poster in Training
rinoa04 is offline Offline
84 posts
since Sep 2006
Jan 17th, 2007
0

Re: Need Help In Decoder Programming

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
flamecly is offline Offline
5 posts
since Jan 2007
Jan 17th, 2007
0

Re: Need Help In Decoder Programming

C++ Syntax (Toggle Plain Text)
  1.  
  2. usingnamespace std;
There should be a space between using and namespace
Quote ...
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.
Quote ...
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.
Quote ...
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.
Last edited by Nick Evan; May 11th, 2010 at 9:05 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Jan 17th, 2007
0

Re: Need Help In Decoder Programming

Click to Expand / Collapse  Quote originally posted by niek_e ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: AnsiString to string conversion
Next Thread in C++ Forum Timeline: Creating with C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC