Least Significant Bit

All in all we’re just another bit in the word

How to teach C to a violinist (Part IV)

Posted by jayzeegp en julio 16, 2008

Tired of just using integer numbers?

On this entry we will learn how to use floating point numbers (such as 3.45321) in an easy way.

If you didn’t see Parts I, II and III you may like to read them.

To declare a floating point number you must do the same that you did with integers but just using the type float instead of int.

For instance:

float num=3.5;

float num2;

float num3=5.231;

But there are things about declarations that I didn’t tell you on Part I (because I wanted it to be simple).

Can I declare several variables on the same line?

I’m happy that you make this question because I was just going to explain it to you.

The answer is yes, but just of the same type.

I will show you an example (as always):

int a,b=3,c,d;

float ferkiwi=6.9, skate, fable=7.3, junior=0.01;

This will create 4 int variables (a,b,c and d) and just one would have a value on declaration (b which values 3) and also 4 float variables (ferkiwi, skate, fable and junior), having values 3 of them.

I’m on top of the world!

Let my show you my skills!

Ok! Two exercises more:

1.- Make a program which asks for 2 floating point numbers and then shows the result of the division of them.

Note: on scanf and printf when you use floating point numbers, you must put %f instead of %d.

2.- Make a program which asks for 2 floating point numbers and then shows the result of the addition, multiplication, substraction and division of them USING JUST 2 VARIABLES.

Note: to use just 2 variables you can read the comments on Part III.


4 comentarios to “How to teach C to a violinist (Part IV)”

  1. rafinhaviolino said

    See! I’m learning… at least now you dont need to say me tu put ; in the end of every line… x]~
    But you need to tell me that %f is not the same of f%
    (how can some1 be soooo stupid?!)

    ok ok… the right one!

    main () {
    float a, b, c;
    printf (“Enter a number: “);
    scanf (“%f”,&a);
    printf (“Enter another one: “);
    scanf (“%f”,&b);
    printf (“%f/%f=%f”,a,b,c);

    Yeahhh! I’m a lil genious!

  2. jayzeegp said

    I’m sure you are lil, but I’m not sure you are a genious 😛
    Well done! 😛

  3. rafinhaviolino said

    Violin Hero em ação!!! x]~
    Thank you for the exercises! I wont forget the printf thing anymore (I hope ^^’)

    main () {
    float a, b;
    printf (“Enter a: “);
    scanf (“%f”,&a);
    printf (“Enter b: “);
    scanf (“%f”, &b);
    printf (“%f+%f=%f\n”,a,b,a+b);
    printf (“%f-%f=%f\n”,a,b,a-b);
    printf (“%f*%f=%f\n”,a,b,a*b);
    printf (“%f/%f=%f”,a,b,a/b);
    getchar ();
    getchar ();

  4. jayzeegp said

    Well done! Look the main page 😛


Por favor, inicia sesión con uno de estos métodos para publicar tu comentario:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )


Conectando a %s

A %d blogueros les gusta esto: