Assignment 1: Basic C programming

Purpose: To get started with C-programming.
Contents:

Variables, arithmetic, iterations, conditionals, simple IO.

Preparations:

Read net lesson 1 and do some of the exercises until you feel familiar with the material.

Examination:

Present your solution for the teacher at a computer lab.

Exercises

  1. The Fibonacci numbers are the numbers in the following sequence:

    0, 1, 1, 2, 3, 5, 8, 13, ...

    i. e. each number after the two first is the sum of the two immediately preceding numbers.

    Write a program that reads a number n and prints the n first Fibonacci numbers.

  2. Write a function double harmonic(int n) that calculates the harmonic sum h(n) = 1 + 1/2 + 1/3 + ... 1/n.

    Write a main-function that reads in integer value n and then tabulates the harmonic for values up to n.
    Sample run:

    $ ./harmonic Give an integer: 5 1 1.000000 2 1.500000 3 1.833333 4 2.083333 5 2.283333 $
  3. Write another program that repeatedly asks a for a floating point value x and computes how many terms you need in the harmonic sum to exceed the limit x.
    Sample run:
    $ ./harmonic2 Give limit (0 to end): 2 4 terms needed to reach 2.00 Give limit (0 to end): 3 11 terms needed to reach 3.00 Give limit (0 to end): 4 31 terms needed to reach 4.00 Give limit (0 to end): 5 83 terms needed to reach 5.00 Give limit (0 to end): 6 227 terms needed to reach 6.00 Give limit (0 to end): 0 $

Valid CSS!