Thread: Should i be happy with this code?

  1. #1
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694

    Should i be happy with this code?

    Ok so today we had our midterm exam in C.(my first in the uni i have my scholarship).

    The way of examining was very new to me, in comparison with the one in my country so it took me time to understand how things flow.

    As a result time for the "On computer task" was 13 minutes for me exactly.I clearly remember it.

    The problem description : The program reads characters from input, stores it in an array with adjustable size and then does the xor operation (something like that was the description.)
    The code was given to us, full of bugs so that when first compiled with Wall flag of gcc i got the 3/4 of my 15" screen filled with errors and warnings.The xor operation they said to us that it was ok.

    This is the code i finally(after 13 minutes passed) handed out
    Code:
    /* System Programming USI/SUPSI/IDSIA
     * Mid Term Exam, Fall 2012
     *
     * Question 7: Eleminate compiler warnings,
     *             compiler errors and run time errors
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int checkSum(char *array , int length);
    
    int main(int argc, char *argv[])
    {
        /* Commnets with double slash are used by me in C++ */
        /* Check for one parameter (max length for string reading in) */
        if(argc==1) {
            printf("Usage : %s <length of input>\n\nExiting...\n\n",argv[0]);
            return -1;
        }
        int i = 0;
        int c; /* EOF handling, so declare as int, not char */
        int length = atoi(argv[1]);
        if(length <= 0 )
        {
            printf("Argument from cmd non positive or not a number.\n\nExiting...\n");
            return -1;
        }
        char array[length];
        while ( (c = getc(stdin)) !=EOF ){
            if( c == '\n')
                continue;
            array[i] = c;
            i++;
            if(i >= length)
                break;
        }
        
        int check = checkSum(array,length);
        
        /* print out the ckeck sum */
        printf("sum is : %d\n", check);
        return 0;
    }
    
    int checkSum(char *array,int length)
    {
        int check = 0;
        int i;
        for (i = 0; i < length; i++)
            check ^= array[i];       /* bit operation */
        return check;
    }
    Do you think i am going to lose points?Is something wrong with it?

    PS1 - Maybe i am overreacting , but the fact that this is my first midterm here makes me feel unsure. :/

    PS2 - I also miss my parents,friends,unofficial girlfriend and my home.... (but this has nothing to do with the code )

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You're using C99. We're still stuck with C89, and even this semester's C++ theory professor uses C++98 ...
    Devoted my life to programming...

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Πατριωτη σε ποιο τμημα εισαι αν επιτρεπεται ?

    So what about my code? Comment it please

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The use of char and int with a bitwise operator is not the best; I would have used unsigned int where bit wise operators are being used.

    But, that is my experience of being bitten on systems where you have options or do not know whether char is unsigned of signed.

    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

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by std10093 View Post
    Πατριωτη σε ποιο τμημα εισαι αν επιτρεπεται ?
    ΤΕΠΛΗΤ Καλαμάτας, Σπάρτη. Είμαι στο τρίτο έτος.

    Oh, and please, this is an english forum!
    Devoted my life to programming...

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by stahta01 View Post
    The use of char and int with a bitwise operator is not the best; I would have used unsigned int where bit wise operators are being used.

    But, that is my experience of being bitten on systems where you have options or do not know whether char is unsigned of signed.

    Tim S.
    Tim the xor operation was not allowed to be changed.Also the requirements was to have a char array.

    Quote Originally Posted by GReaper View Post
    ΤΕΠΛΗΤ Καλαμάτας, Σπάρτη. Είμαι στο τρίτο έτος.

    Oh, and please, this is an english forum!
    Just a phrase my friend

  7. #7
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Your code looks good. I think you will get an A+
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am very happy....
    By NANO in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-04-2002, 03:06 PM
  2. I'm So Happy
    By mike_k in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-19-2002, 06:38 AM
  3. o happy day...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-26-2002, 03:46 PM
  4. Happy now?
    By quzah in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 04:01 PM