Thread: C/C++ code

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    1

    C/C++ code

    I have the following code below, i am trying to add xnorr,norr,nandd function to it, how would i do this




    Code:
    char nott(char operand);
    char andd(char operand1, char operand2);
    char orr(char operand1, char operand2);
    char xorr(char operand1,char operand2);
    
    #define PREFERRED 'p'
    #define OTHER 'o'
    #define INVALID 'x'
    
    char nott( char operand )
    {
    char result=INVALID;
         if (operand == PREFERRED )
             result=OTHER;
    else
         result=PREFERRED;
         return (result);
    }
    char andd(char operand1, char operand2)
    {
    char result=INVALID;
         if (operand1==PREFERRED)
    {
         if (operand2==PREFERRED)
    {
         result=PREFERRED;
    }
    else if (operand2==OTHER)
    {
         result=OTHER;
    }
    }
    else if(operand1==OTHER)
    {
    
    if(operand2==OTHER)
    
    {
    
    result=OTHER;
    
    }
    
    else if (operand2==PREFERRED)
    
    {
    
    result=OTHER;
    
    }
    
    }
    
    return (result);
    
    }
    
    char orr(char operand1, char operand2)
    
    {
    
    char result=0;
    
    result=INVALID;
    
    if (operand1 == PREFERRED)
    
    result = PREFERRED;
    
    else if (operand2==PREFERRED)
    
    result = PREFERRED;
    
    else
    
    result = OTHER;
    
    return (result);
    
    }
    
    char xorr(char operand1,char operand2)
    
    {
    
    char result=orr(andd(nott(operand1),operand2),andd(nott(operand2),operand1));
    
    return(result);
    
    }
    
    char nandd(char operand1,char operand2)
    
    {
    
    char result=INVALID;
    
    result=nott(andd(operand1,operand2));
    
    return(result);
    
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    So wait, where are you having trouble exactly? Compiler errors? Runtime errors?

    Typical responses usually include : compile with warning flags (-Wall, -Wextra, -pedantic) and to analyze runtime errors, there's valgrind and gdb. I don't use gdb but that's because I like to write everything down on a piece of paper instead when I get stuck. Actually, I still debug with printf() statements lol.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Answer: Spend more time learning how to program and less time cross-posting to several forums.

    C/C++ Help - C And C++ | Dream.In.Code
    C/C++ code - C++ Forum

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by tarnold79
    C/C++ code
    There is a programming language named C. There is a programming language named C++. There is no programming language named C/C++. So, you need to be clear as to whether your code is C or C++. You can write code that is intended to be compilable on both C and C++ compilers, but that is not "C/C++" either.

    Quote Originally Posted by tarnold79
    I have the following code below
    You need to format your code properly, especially where indentation is concerned. For example:
    Code:
    char nott(char operand);
    char andd(char operand1, char operand2);
    char orr(char operand1, char operand2);
    char xorr(char operand1, char operand2);
    
    #define PREFERRED 'p'
    #define OTHER 'o'
    #define INVALID 'x'
    
    char nott(char operand)
    {
        char result = INVALID;
        if (operand == PREFERRED)
            result = OTHER;
        else
            result = PREFERRED;
        return (result);
    }
    
    char andd(char operand1, char operand2)
    {
        char result = INVALID;
        if (operand1 == PREFERRED)
        {
            if (operand2 == PREFERRED)
            {
                result = PREFERRED;
            }
            else if (operand2 == OTHER)
            {
                result = OTHER;
            }
        }
        else if (operand1 == OTHER)
        {
            if (operand2 == OTHER)
            {
                result = OTHER;
            }
            else if (operand2 == PREFERRED)
            {
                result = OTHER;
            }
        }
        return (result);
    }
    
    char orr(char operand1, char operand2)
    {
        char result = 0;
        result = INVALID;
        if (operand1 == PREFERRED)
            result = PREFERRED;
        else if (operand2 == PREFERRED)
            result = PREFERRED;
        else
            result = OTHER;
        return (result);
    }
    
    char xorr(char operand1, char operand2)
    {
        char result = orr(andd(nott(operand1), operand2), andd(nott(operand2), operand1));
        return (result);
    }
    
    char nandd(char operand1,char operand2)
    {
        char result = INVALID;
        result = nott(andd(operand1, operand2));
        return (result);
    }
    I suggest that you use braces for if statements even when it is unnecessary. You have done this inconsistently in your code.

    Quote Originally Posted by tarnold79
    i am trying to add xnorr,norr,nandd function to it
    You need to be clear as to what these functions do, and hence you need to tell us what they do, rather than assume that we know based on their names (and perhaps we do know, but what we know doesn't match what you think they should be). It would also be a good idea to document your existing functions (e.g., using comments) to describe what they do (but you don't need to explain how they do what they do, except where the implementation of the code needs some explanation).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    return (result);
    Does anyone remember which Language required or was better with the () in the return statement?

    I remember doing this a many years ago; because it was needed in one of my very earlier programming languages and I forgot what it was.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quote Originally Posted by stahta01 View Post
    Code:
    return (result);
    Does anyone remember which Language required or was better with the () in the return statement?

    I remember doing this a many years ago; because it was needed in one of my very earlier programming languages and I forgot what it was.

    Tim S.
    Question 20.19
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Does anyone remember which Language required or was better with the () in the return statement?
    I don't remember if it was required or not, but I think it is a hold over from K+R C. Many of the examples, that actually return a value, used the parentheses. For example from this link: Programming in C: A Tutorial Brian W. Kernighan Bell Laboratories, Murray Hill, N. J.

    14. Functions; Comments

    ...

    We have already seen many examples of calling a function, so let us concentrate on how to define one. Since count has two arguments, we need to declare them, as shown, giving their types, and in the case of buf, the fact that it is an array. The declarations of arguments go between the argument list and the opening `{'. There is no need to specify the size of the array buf, for it is defined outside of count.

    The return statement simply says to go back to the calling routine. In fact, we could have omitted it, since a return is implied at the end of a function.

    What if we wanted count to return a value, say the number of characters read? The return statement allows for this too:

    Code:
                   int i, c, nchar;
                   nchar = 0;
                   ...
                   while( (c=getchar( )) != '\0' ) {
                           if( c > size || c < 0 )
                                   c = size;
                           buf[c]++;
                           nchar++;
                   }
                   return(nchar);
    Any expression can appear within the parentheses. Here is a function to compute the minimum of two integers:

    Code:
           min(a, b)
              int a, b; {
                   return( a < b ? a : b );
           }

    Jim
    Last edited by jimblumberg; 12-11-2014 at 11:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM