[personal profile] horrorcheck
I don't want to think too much about my third day in NYC, much less write about it. Exhausting. I'm living in a nice house in Staten Island now, though.

SO on to my first day at Hacker School.

Hacker School is awesome! Everybody is super nice and friendly!

Today was mostly spent learning C. A bunch of us signed up to be randomly paired with others for pair programming. I misread my email and thought my partner was Marin, so I talked with him a bit and found out he is very good at C. He offered to teach me some. The first thing we worked on was learning how the standard output, standard input, and standard error work. Then, he helped me write an attempt a implementing my own atoi. atoi turns strings into integers.

Here was my first attempt:

#include <stdlib.h=
#include <stdio.h=

int my_atoi(const char *nptr)
{
    int answer = 0;
    int counter = 0;

    while (nptr[counter] != '\0')
    { 
        answer = answer * 10 + (nptr[counter] - '0');
        counter = counter + 1;
    }

    return answer;
}

int main (int ac, char **av)
{
    int number, longNumber, negNumber, string;

    number = my_atoi("42");
    longNumber = my_atoi("523219"); 
    negNumber = my_atoi("-42");
    string = my_atoi("hello");

    printf("42: %d\n", number); 
    printf("A longer number: %d\n", longNumber);
    printf("A negative number (-42): %d\n", negNumber);
    printf("A string (hello): %d\n", string);
    
    return (0);
}


Which gives this output:

42: 42
A longer number: 523219
A negative number (-42): -258
A string (hello): 619663


So, some of these are wrong. Maybe it's okay for "hello" to not make any sense, but "-42" ought to be -42.

So I'm still trying to figure out what I want to do to make negative numbers work. This seems to work:

#include <stdlib.h=
#include <stdio.h=

int my_atoi(const char *nptr)
{
    int answer = 0;
    int counter = 0;

    while (nptr[counter] != '\0')
    { 
        if ((nptr[counter] - '0') < 10  &&
            (nptr[counter] - '0') = 0)
        {
            answer = answer * 10 + (nptr[counter] - '0');
        }
        else if (nptr[counter] == '-' && counter == 0)
        { 
            // this is a-okay, it's a negative
        }
        else 
        {
            // BAD WRONG
        }
        counter = counter + 1;
    }

    if (nptr[0] == '-')
    { 
      answer = answer * (-1);
    }

    return answer;
}

int main (int ac, char **av)
{
    int number, longNumber, negNumber, string;

    number = my_atoi("42");
    longNumber = my_atoi("523219"); 
    negNumber = my_atoi("-42");
    string = my_atoi("hello");

    printf("42: %d\n", number); 
    printf("A longer number: %d\n", longNumber);
    printf("A negative number (-42): %d\n", negNumber);
    printf("A string (hello): %d\n", string);
    
    return (0);
} 


It gives the output:

 42: 42
A longer number: 523219
A negative number (-42): -42
A string (hello): 0


So that's good. But it doesn't seem like the "right" way to get the right output? I will continue working on it. :)

From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

Profile

horrorcheck

August 2015

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829
3031     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 26th, 2017 08:54 am
Powered by Dreamwidth Studios