Finally started programming!

Hi there!

I just finished my two first courses last week and this week the first programming course started. Just this first week has been fun! Most of what we have went through this first week I already knew, but some thing are new.

The first day, on monday, the teacher shortly introduced himself and told us about what he has done and how he got to be a teacher in programming and game design. Afterwards, everyone in the class shortly introduced themself. When all the introduction was over, the first programming lesson started. By the time the lesson started a lot of time had already gone by, but there was still enough time for us to learn quite a bit. As I earlier mentioned, some of what we went through this first week, I already knew about, but it was a good reminder as I had forgotten some of the stuff. But he did also briefly talk about the history of computers and programming. Also he went through the different parts of the computer that allows us to make these programmes.

Two of the most important parts of programming, 0s and 1s. In other words, base-2 numeral system. When I first learned base-2 numeral system, it was really confusing and it took me a while until I understood it, but then I saw that it was really simple. But to understand base-2, we first have to understand the base-10 numeral system, this one is not as hard as you might think as it is the numeral system that we everyday.Untitled

As you can see above, it is the numeral system we all are used too. But lets dive in and understand how it really works. If we start from the first number from the right, excluding the two numbers after the point. The number 1 is the first number in order, the way that one is calculated is by taking 10 to the power of 0 and then multiply it by the number that stands there, in this example it is 1. This results in 1. The second number works the same, as it is the number on the second position, it is 10 to the power of 1 and then multiplied by 2 because in this example it is a 2 there, resulting in 20. But all this is obvious to us, but the point is that the base-2 numeral system works the same way…almost.Untitled2

In this way of counting, we only have two numbers to rely on, 0 and 1. If we were to count in the way we are used to, this would turn out to be a quite big number. But in reality this number is that big. This number, when converted to our numeral system that we are used to, is 255.

Now lets explain how this works. We need too think that each number has their own position, starting from right to left. The numbers are calculated by taking their position and subtract 1 form it, then taking 2 to the power of that number, and then multiplied by the number showing(which can only be either 0 or 1).

So lets take an example, imagine 1111 being in base-2 and we want to convert it to base-10. So I will skip the part where you are supposed to take 2 to the power of x but I will show you guys the rest. 1*8+1*4+1*2+1*1= 15. Simple enough, right?

The second day, Tuesday, we went through some simple programming. The lecture overview looked like this: Programming, data types, variables, operators and finally there was some example programmes.

So first of all, what is programming? It is basicly instructions and data. A lot of data and many instructions.

The different we went through were integers, characters, booleans, floating points, void and null. So far I have used integers, characters, booleans and floating points and the other two are still a mystery to me. The rest where stuff I had learned earlier and also had not forgotten it.

We also got some exercises we could do if we wanted to. These exercises were both questions about everything we had learned and also programs we could do so we could train on everything we had learned. There were 26 programs, most of them were easy, but as I drew closer to the end it got harder. There were two programs I could not do, but luckily my roommate knew the solution and helped me now and then.

The third day we had a small math test which was supposed to take a maximum of 30 minutes and was made really easy. After the math test we could either go home or stay and just work on the exercises. I stayed in class and just tried to do as many exercises I could and, whenever it was needed, the teacher would help anyone that was stuck.

The fourth day we went through some more advanced stuff. The first being conditional statements which are used to perform different actions depending on what boolean conditions were met. A boolean expression produces one of two things, either true or false.

Enough of what we learned in classes, lets go on with the last exercise that we had. On the last exercise we were supposed to create a guessing game, where the players it to guess a number between 1 and 100 and after each guess, the player would be told whether they guessed too high or too low. Here is how the code looked like:

#include
#include
#include

int random(int min, int max)
{
return min + (rand() % (max – min + 1));
}

int main(int argc, char* argv[])
{
srand((unsigned int)time(0));

bool running = true;
while (running)
{
int guess = 0;
int rand = random(1, 100);
int quant = 0;
do
{
std::cout << “Guess a number!” << std::endl;
std::cin >> guess;
quant++;

if (guess > rand)
{
std::cout << “Too high!” << std::endl;
}
else if (guess < rand)
{
std::cout << “Too low!” << std::endl;
}
else
{
std::cout << “You guessed correct!” << ” It took you ” << quant << ” tries.” << std::endl;
}
} while (guess != rand);

int userInput = 0;

std::cout << “If you want to play again, press ‘5’.” << std::endl;
std::cin >> userInput;

if (userInput == 5)
{
running = true;

}
else
{
running = false;
}

}

std::cin.ignore(1024, ‘n’);
std::cin.get();
return 0;
}

This exercise made us use everything we had learned so far. I found this really hard in the beginning, but in the end with some guidence and time I was able to solve it. Enough blogging for tonight, I deserve a break and some candy now!

I will be blogging every week from now on, see you next week!

 

About Ara Mohammad

2014  Programming