Fandom

Scratchpad

Basic programming

215,761pages on
this wiki
Add New Page
Discuss this page0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Theme: Programming

In the end, programming is just giving orders. A computer is a device that will always obey. Some people, known as programmers, regard this as a very convenient feature. Of course, this faces us with a problem which is slightly different to that found in social interaction: to give the correct orders. Because the worse risk in programming is to have your orders done, not your wishes...

In the following we will assume that you have a running unix box installed in a computer to which you have access, and that you know the basics of the shell. I.e.: you know how to edit a text, how to behave within your directories and so on.

So let's assume all this and... let's dive into programming!!!

Your first program

Let's have the follwing typed and saved as hello.cc.

include<stdlib.h>
int main()
{
   printf("Hello, world!\n");
}

Hey, let's see it running before the explanantions...

Compilation

Type the following in the shell:

g++ -o hello hello.cc

Now you should have an executable file in your directory. Try

./hello

Does it work? If it does, now let's return to the code and explain it. The main line is that with the printf:

printf("Hello, world!\n");

It says "print with some format", and the "\n" stays for a return of the carriage. But the printf command, which seems so useful, is not available for you unless you say it explicitly to the compiler, this is the reason for the #include<stdio.h> line, which means include the standard input/output commands. If you think all this is too much information, don't worry. The important point is to grasp the idea.

Numbers

The next element in our program will be the use of numbers and variables. Let us try this program:

#include<stdio.h>

int main()
{
    int a,b;
    a=2;
    b=3;
    printf("Result of the multiplication: %d\n",a*b);
}

Compile and run this program as you did with the previous one. You should get a line such as:

 Result of the multiplication: 6

So the line int a,b; declares that the names a and b will stand for integer numbers, i.e.: whole numbers (possibly with a sign). The printf line comes with a new feature: it has a %d in it. This is a place where an integer number will be written in the screen. Which integer number? OK, it's given after the comma: a*b.

If you need to print more numbers in the same line, you can do like this:

   printf("Result of multiplying %d and %d: %d\n",a,b,a*b);

Exercises

1. Try to substitute that line into the previous program, run it and understand how it works.

2. Write a program which adds three numbers. Hey, don't keep on reading! Do it!! :)

A loop

But the main power of computers is in the fact that they can repeat a computation many many times, without getting tired! For example, let's consider that we want to compute the first square numbers. Try the next program:

 #include<stdio.h>
 int main()
 {
     int i;
     for (i=1;i<=10;i++)
         printf("square of %d is %d\n",i,i*i);
 }

This program contains many new features. First of all, run it!

The loop is the for line: for (i=1;i<=10;i++) says: the next line should be carried out for all values of i, from 1 to 10.

Exercises

1. Change the previous program so as it prints all square numbers from 10 to 20.

2. Change it again, so as it gives the multiplication table for number 9.



OK, OK, still not very exciting things... before going on, read these texts in order to know a little bit more about compiling and all that...

Basic programming: Compiling

Basic programming: Introduction to programming languages


Now read: Structured programming

Also on Fandom

Random wikia