First day at Hacker School:
Jan. 5th, 2015 05:38 pmI 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
Here was my first attempt:
Which gives this output:
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:
It gives the output:
So that's good. But it doesn't seem like the "right" way to get the right output? I will continue working on it. :)
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. :)