Programming Lesson 3

Blog post by darkwyrm on Sat, 2010-01-30 01:32

Continued (mis)adventures in programming for all of the curious into the insights of being a codemonkey. In this lesson, we examine the different types of data we can use, a more in-depth look at how to print to the screen, and more! Learning to Program With Haiku, Lesson 3.pdf All previous lessons have received some minor revisions and code is now colored for better readability.

Comments

Re: Programming Lesson 3

Nice one, DarkWyrm!

Was the printf line in bughunt#2 intentionally the same as in the hypotenuse example or is that a copy&paste mishap?
You may want to explain the difference and use of declarations and definitions a bit.

Maybe you should keep one page with an index of all lessons that you can link to in every episode.

Keep it up!
Regards,
Humdinger

Re: Programming Lesson 3

That would be a copy and paste error, which I just fixed. Thanks for pointing it out! :-)

Re: Programming Lesson 3

Hi,

Some wording with regards to explaining operators is incorrect and misleading. When using "a++", a is not incremented after the whole expression, but after taking the value of a and inserting it into the expression. This can be illustrated:

int a = 1;
int b = a++ + a++;

What you say makes it sound as though b will be evaluated to 2, but it will be 3.

Some more things to point out:

* Don't teach beginners bad style like "int a, b;". Always declare variables on separate lines for easier reading later on.

* In C++, don't put "void" into functions that don't take arguments, it's superfluous.

* Why not mention some Haiku specific types, like "uint8"?

Otherwise, thanks for doing these lessions!

Re: Programming Lesson 3

Count me as confused, then. Could you then explain this to me?

#include <stdio.h> 
 
int main(void) 
{ 
	int a = 1;
 
	int b = a++ * a++;
 
	printf("a is %d, b is %d\n",a,b);
 
	return 0; 
} 

Output is as follows:

$ ./foo
a is 3, b is 1

When I flip it around and do ++a * ++a, I get what would be expected, based on your explanation -- a is printed as 3, but b is 9.

Re: Programming Lesson 3

AFAIK evaluating things like f(a++,a++) is ambiguous. Evaluation order and results are undefined and completely compiler dependent.

Nice explanation on it: http://c-faq.com/expr/evalorder2.html

P.S.

Lessons are fine. I am waiting for something Haiku-specific, it should be interesting.

Re: Programming Lesson 3

Haiku-specific is coming. I'm starting with the basics that you would see in C. Once I get into the object-oriented concepts in C++, expect to see stuff that is geared toward the BeOS API in general. Rest assured, though, that I won't leave out Haiku-specific features like the layout management code.

PS Thanks for the note about evaluation. Much appreciated. :)

Re: Programming Lesson 3

Wow, maybe it's a good thing that you are doing these lessons and not me! :-)

Re: Programming Lesson 3

Good work, I will recommend to my colleagues beginners in programming. As someone already pointed out, the syntax is easy to learn in web; practical examples with your pleasant way of writing and explain would be incredible.

It's good for Haiku community too as well, since many people can born here.

Re: Programming Lesson 3

The last lesson in Haiku Lesson 3: Using the equation Interest = Principal * rate * time, calculate a principal of $20000 at a rate of 5% per month for 24 months. I am having a bit of trouble with it. Here is what I coded:

#include <stdio.h>
#include <math.h>
int SimpleInterest(int p,long i,int m)
{
	return p*i*m;
}
int main(void)
{
	int p;
	long i;
	int m;
	p=20000;
	i=5/100;
	m=24;
	printf("principal %d, interest %f, months %d, Simple Interest %d\n" , p,i,m, SimpleInterest(p,i,m));
	return 0;
}

This is the result I got in the Terminal:

~/Documents/develop_lessons/SimpleInterest> SimpleInterest
principal 20000, interest 0.000000, months 0, Simple Interest 0.000000

Could you please show me what is wrong? Thanks for the lessons. It is great learning in Haiku r35947. It is very stable for an alpha.

ps. In the preview to my post I notice the two #includes at the top leave out parts but they are there in reality. It is the posting process that ignores them. They are: stdio.h and math.h

Re: Programming Lesson 3

Hi cebif!

There are two mistakes in your program:
"i" should be a "float" or "double"; "long" is for big whole numbers.
The line "i=5/100;" should be "i = 5.0 / 100.0;" By adding the ".", you force the compiler to use and return a floating point number, otherwise it rounds to a whole number, resulting in an interest of 0. I'm sure I've read that in one of DarkWyrm's lessons, maybe it's in one of the later ones.

Regards,
Humdinger.

Re: Programming Lesson 3

Thanks Humdinger, that fixed it.