You are here:

C#/C# programming logic.

Advertisement


Question
QUESTION: Hi Daniel,

i`m learning c# 3.0 and i developed a simple program of printing a truth table on the console :-

using System;

class Example28
{
   static void Main()
   {
       bool p, q;

       Console.WriteLine("P\tQ\tP & Q \tP | Q \tP ^ Q \t!P\n");
       p = true;
       q = true;

       Console.Write(p + "\t" + q + "\t");
       Console.Write((p&q) + "\t" + (p|q) + "\t");
       Console.WriteLine((p ^ q) + "\t" + (!p) + "\n");

       p = true; q = false;
       Console.Write("{0}\t{1}\t",p,q);
       Console.Write("{0,4}\t{1,4}\t",p&q,p|q);
       Console.WriteLine("{0,4}\t{1,4}\t\n",p^q,!p);

       p = false; q = true;
       Console.WriteLine(p + "\t" + q + "\t" + (p & q) + "\t" + (p | q) + "\t"
         + (p ^ q) + "\t" + !p + "\n");

       p = false; q = false;
       Console.WriteLine(p + "\t" + q + "\t" + (p & q) + "\t" + (p | q) + "\t"
         + (p ^ q) + "\t" + !p + "\n");
       Console.WriteLine();

   }
}

Now, i want this program to ouptut 0`s and 1`s instead of true and false... but somehow cant get the logic....

could you help me out ...

Thanks,

Neville.

STAC IT
STAC IT  
ANSWER: Hi Neville,

the answer you seek is in the Convert object. If you enclose the variables p and q in the .ToInt32() method that is a member of the Convert object, it will output the desired result.

For example:

int ip = Convert.ToInt3(p);


The value of ip will become 1 or 0 depending on the value of p.

You can then replace all occurances of the variable p in the Console.WriteLine() method with ip. And the same can be done with q.

If you want to save ram (trade off is space for speed) then you can simply use Convert.ToInt32(p) within the Console.WriteLine function itself, such as:


Console.WriteLine(Convert.ToInt32(p) + ...

Hope this helps!

---------- FOLLOW-UP ----------

QUESTION: Thanks Daniel,

but let me tell you something .... i`m studying c# from Herbert Schildts book "C# 3.0 : A Beginners Guide" and it is in this book that i found out this question and that too in just the second chapter ... which does not introduce you to any convert.toint32 object ....

you can check out this link of the online edition of the book for yourself :


http://books.google.co.in/books?id=wH4CnNtWAb4C&pg=PA67&lpg=PA67&dq=c%23+3.0+tru

The thing is that whatever i have studied in the first 2 chapters , only using that i have to modify the program so that it uses and displays 0 and 1.

(just check out the link i gave .. it will open up to that particular page itself...scroll down on the next page to see the question.)

Thanks once again.

Neville.

S-TAC IT
S-TAC IT  
ANSWER: I hope you enjoyed your holiday Neville.

I checked out the link, but Google must use a session based cookie: I was unable to see the question you were referring to.

However, I think I have the solution you are looking for. You can also shape the output using the ? : operators. For example:

Console.Write(p + "\t" + q + "\t");

could be written as

Console.Write(p ? "1\t" : "0\t");
Console.Write(q ? "1\t\n" : "0\t\n");

The Syntax is:
variable ? true statement : false statement

This method would have been covered in a section on boolean values, so you likely encountered that in your reading.

---------- FOLLOW-UP ----------

QUESTION: Hey Daniel ....

Thanks, once again ... and i hope i`m not irritating you .... !!,
but let me tell you ... these 2 chapters that i encountered in c# so far from
Herb Schildts book dont teach anything about ? operator.
so once again ... if you could write this program in a much more simpler way .... Daniel ...!!

Thanks once again ...!!

Neville.

Answer
S-TAC Information Technology
S-TAC Information Tech  
Sorry about the delay in response Neville, I had just recently completed a move and was waiting for the cable guy to install my internet access.

Im surprised that the book did not mention the ? : syntax, as this is very common in programming languages.

Referencing the book, I think I see a way that could be useful to you. You can use conditional logic (if statement) to output the desired value. For instance you can compare the results side by side using this code:

using System;

class Example28
{
   static void Main()
   {
       bool p, q;

       Console.WriteLine("P\tQ\tP & Q \tP | Q \tP ^ Q \t!P\n");
       p = true;
       q = true;

       Console.Write(p + "\t" + q + "\t");
       Console.Write((p & q) + "\t" + (p | q) + "\t");
       Console.WriteLine((p ^ q) + "\t" + (!p) + "\n");

       p = true; q = false;
       Console.Write("{0}\t{1}\t", p, q);
       Console.Write("{0,4}\t{1,4}\t", p & q, p | q);
       Console.WriteLine("{0,4}\t{1,4}\t\n", p ^ q, !p);

       p = false; q = true;
       Console.WriteLine(p + "\t" + q + "\t" + (p & q) + "\t" + (p | q) + "\t"
         + (p ^ q) + "\t" + !p + "\n");

       p = false; q = false;
       Console.WriteLine(p + "\t" + q + "\t" + (p & q) + "\t" + (p | q) + "\t"
         + (p ^ q) + "\t" + !p + "\n");
       Console.WriteLine();

       Console.WriteLine("P\tQ\tP & Q \tP | Q \tP ^ Q \t!P\n");
       p = true;
       q = true;

       if(p && q)
         Console.Write("1\t1\t");
       else if(p && !q)
         Console.Write("1\t0\t");
       else if(!p && q)
         Console.Write("0\t1\t");
       else
         Console.Write("0\t0\t");


       if (p & q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if (p | q)
         Console.Write("1\t");
       else
         Console.Write("0\t");
       
       if(p ^ q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if(!p)
         Console.Write(" \n");
       else
         Console.Write("0\n");

       p = true; q = false;

       if(p && q)
         Console.Write("1\t1\t");
       else if(p && !q)
         Console.Write("1\t0\t");
       else if(!p && q)
         Console.Write("0\t1\t");
       else
         Console.Write("0\t0\t");


       if (p & q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if (p | q)
         Console.Write("1\t");
       else
         Console.Write("0\t");
       
       if(p ^ q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if(!p)
         Console.Write("1\n");
       else
         Console.Write("0\n");

       p = false; q = true;

       if (p && q)
         Console.Write("1\t1\t");
       else if (p && !q)
         Console.Write("1\t0\t");
       else if (!p && q)
         Console.Write("0\t1\t");
       else
         Console.Write("0\t0\t");


       if (p & q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if (p | q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if (p ^ q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if (!p)
         Console.Write("1\n");
       else
         Console.Write("0\n");


       p = false; q = false;
       if (p && q)
         Console.Write("1\t1\t");
       else if (p && !q)
         Console.Write("1\t0\t");
       else if (!p && q)
         Console.Write("0\t1\t");
       else
         Console.Write("0\t0\t");


       if (p & q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if (p | q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if (p ^ q)
         Console.Write("1\t");
       else
         Console.Write("0\t");

       if (!p)
         Console.Write("1\n");
       else
         Console.Write("0\n");

       Console.ReadKey();

   }
}


as you can see this way is much more tedious :)

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Daniel Fredriksen

Expertise

Questions about programming in C#, Syntax, Constructs, Semantics, SQL, Common Language Infrastructure (CLI), Managed Code, Intermediate Language (IL), Metadata, Object Oriented Programming, Software Engineering, Systems Analysis & Design, SDLC, Methods, Variables, References, Scope, Resources, Classes, Libraries, Interfaces, Components, Libraries, .NET, COM, Garbage Collection, etc.

Experience

Thirteen years experience as a computer programmer with six years experience consulting on various information technology matters to include cybersecurity. Six years experience consultanting for the U.S. Military and business clients, to include physical and information security consulting. Veteran of Operation Iraqi Freedom III, and Operation Enduring Freedom VII

Organizations
American Society for Industrial Security (ASIS) International Foundation for Protection Officers (IFPO) National Association of Investigative Specialists (NAIS) NY Army National Guard

Education/Credentials
Pursuing Doctorate in Information Technology (University of Phoenix) Pursuing Doctorate in Intelligence Management (Henley-Putnam University) C# Certification (Brainbench) Certified Protection Officer (IFPO) Associate in Science LA, AOF in Computer Science/Protective Services (Excelsior College)

©2012 About.com, a part of The New York Times Company. All rights reserved.