Thursday, September 17, 2009

Challenge #1

I was looking over some of the code for the first challenge under the To do List and I was inspired to do my own version of the code. Mine looks like:


void GetInt(char *strint, int val){
char string[80];
int num=0; int num2=0;

/* THis will convert each digit to a char, then cuts off a digit due to
narrowing involved with dividing an integer. Comes out in reverse order */
while( val >0){
string[num++]= (val%10) + '0';
val/=10;
}

/*reverses the order, so the string will be in the right order.*/
for(num-=1;num>=0;num--){
strint[num2++]=string[num];
}
strint[num2]='\0';

}/*GetInt */

I've tested it and it works perfectly. True, it's not the most memory efficient solution (there's 80 elements in the local char array 'string') but I wasn't sure if malloc, calloc or realloc were allowed to be used for this challenge. Then again, I might of saved myself some headache by avoiding them.

No comments:

Post a Comment