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 :p )