Thread: find and indicate all errors

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    21

    find and indicate all errors

    Find and indicate all of the errors in the following program.
    This is easy c not c++

    Code:
    void main(void) 
    
    { 
    int a; 
    int b; 
    int c;
    int d;
    a = 30; 
    b = 0; 
    c= 4; 
    
    if ( a > c) 
    { b = c-a; 
    c= 0; 
    }
    if ( b=0) 
    { d = a + b +c } 
    a =0; 
    while (a <20) 
    { b=b+1; 
    c= c+1; 
    
    } 
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1. If this is C not C++ why did you post in the C++ forum?

    2. I have found all the errors and given myself a 100%. Go me!

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by team23 View Post
    This is ugly c not c++
    Fixed.

    But seriously, why even bother using code tags if your code has zero indentation? If you were using an editor that had decent auto indenting, you would find that some of the trouble would stand out on its own.

    Anyway, here is some indented code for you. Like tabstop, I found all the errors too.

    Code:
    void main(void)
    {
        int a;
        int b;
        int c;
        int d;
        a = 30;
        b = 0;
        c = 4;
    
        if (a > c) {
            b = c - a;
            c = 0;
        }
    
        if (b = 0) {
        d = a + b + c
        }
    
        a = 0;
        while (a < 20) {
            b = b + 1;
            c = c + 1;
        }
    }

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    1. We can't learn c++ without learning c

    2. If you know how to do it, then post it. We've found errors, but we're not sure if the operations even make sense.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by team23 View Post
    1. We can't learn c++ without learning c
    That ... adds one to the error count.
    Quote Originally Posted by team23 View Post
    2. If you know how to do it, then post it. We've found errors, but we're not sure if the operations even make sense.
    How do you not know if the operations make sense? We're adding and subtracting numbers here.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Look, this board does not exist for kids who don't want to do their homework. You must have missed that part when you read the board rules. You DID read them, didn't you? You know, the ones that said:

    Announcement: Forum Guidelines. Read before posting
    Announcement: Homework

    Having read those, you would know that asking to have your homework done is strongly discouraged here. If you want help, that is great - but show some effort; you are more likely to get help that way.
    Last edited by kermit; 04-11-2010 at 07:36 PM.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    yeah, but look at what the equations are telling us to do. both of the IF functions are relevant to the given values, so why are they even separated? and is a=0 supposed to not be enclosed within any operation? stuff like that...

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    First of all, this is a toy program, or in other words, it is a contrived example for the purpose of teaching. Personally, I am not convinced that this method is entirely helpful, but anyway... Because of this fact (i.e, it is a toy program), don't get caught up on what the if statements are doing - at least not in the sense of trying to find some kind of logical connection. They are merely there to manipulate some values. It seems to me that what you need to do is follow the code through, line by line - like the computer would - and see what is going on. When you come to an error, you need to correct it.

    As far as the a=0; assignment statement is concerned, there is nothing wrong, nor anything unusual about that sort of thing. It is simply setting the variable a with a value before going into a while loop. It is an idiom of C to do it that way. As you gain more experience with C programming, you will learn that this is so.

    I will give you a hint: Generally speaking, every statement in C should end with a semi-colon. Also, check all of your comparison statements.
    Last edited by kermit; 04-11-2010 at 07:55 PM.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Oh, that makes a lot more sense. thanks for the help

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by team23 View Post
    1. We can't learn c++ without learning c
    You mean, in order to be a proper ass of a C++ programmer you need to be able to claim later on that you have programmed lots in C and understand why it is not a great modern language like C++. Yawn.

    Quote Originally Posted by team23 View Post
    We've found errors, but we're not sure if the operations even make sense.
    No, probably because they are not in the STL. You need a more structured environment -- stay away from C.

    Which those non-sensical operations could be in any language virtually unchanged. It's a couple of loops and extremely basic arithmetic.
    Last edited by MK27; 04-12-2010 at 05:21 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Why don't you post the problems you have found, and we'll tell if you got them all? I've counted 4. Even though I think the entire program is completely useless, as it is possible to make a program that does exactly the same in one line of code.

Popular pages Recent additions subscribe to a feed