Next: Arrays Up: Looping and Iteration Previous: break and continue


  1. Write a program to read in 10 numbers and compute the average, maximum and minimum values.

    " >

  2. Write a program to read in numbers until the number -999 is encountered. The sum of all number read until this point should be printed out.

    " >

  3. Write a program which will read an integer value for a base, then read a positive integer written to that base and print its value.

    Read the second integer a character at a time; skip over any leading non-valid (i.e. not a digit between zero and ``base-1'') characters, then read valid characters until an invalid one is encountered.

               Input       Output
            ==========     ======
            10    1234      1234
             8      77        63   (the value of 77 in base 8, octal)
             2    1111        15   (the value of 1111 in base 2, binary)
    The base will be less than or equal to 10.

    NOTE: The Ceilidh skeleton is a complete program which works for the fixed base 10.

    " >


  4. Read in three values representing respectively

    a capital sum (integer number of pence),

    a rate of interest in percent (float),

    and a number of years (integer).

    Compute the values of the capital sum with compound interest added over the given period of years. Each year's interest is calculated as

    interest = capital * interest\_rate / 100;
    and is added to the capital sum by
     capital += interest;
    Print out money values as pounds (pence / 100.0) accurate to two decimal places.

    Print out a floating value for the value with compound interest for each year up to the end of the period.

    Print output year by year in a form such as:

    Original sum 30000.00 at  12.5 percent for 20 years
    Year Interest  Sum
      1  3750.00 33750.00
      2  4218.75 37968.75
      3  4746.09 42714.84
      4  5339.35 48054.19
      5  6006.77 54060.96
      6  6757.62 60818.58
      7  7602.32 68420.90
      8  8552.61 76973.51
      9  9621.68 86595.19
     10 10824.39 97419.58

    " >

  5. Read a positive integer value, and compute the following sequence: If the number is even, halve it; if it's odd, multiply by 3 and add 1. Repeat this process until the value is 1, printing out each value. Finally print out how many of these operations you performed.

    Typical output might be:

     Inital value is 9
     Next value is  28
     Next value is  14
     Next value is   7
     Next value is  22
     Next value is  11
     Next value is  34
     Next value is  17
     Next value is  52
     Next value is  26
     Next value is  13
     Next value is  40
     Next value is  20
     Next value is  10
     Next value is   5
     Next value is  16
     Next value is   8
     Next value is   4
     Next value is   2
     Final value 1, number of steps 19
    If the input value is less than 1, print a message containing the word
    and perform an
            exit( 0 );
    " >

  6. Write a program to count the vowels and letters in free text given as standard input. Read text a character at a time until you encounter end-of-data.

    Then print out the number of occurrences of each of the vowels a, e, i, o and u in the text, the total number of letters, and each of the vowels as an integer percentage of the letter total.

    Suggested output format is:

            Numbers of characters:
            a   3 ; e   2 ; i   0 ; o   1 ; u   0 ; rest  17
            Percentages of total:
            a  13%; e   8%; i   0%; o   4%; u   0%; rest  73%
    Read characters to end of data using a construct such as
            char ch;
                ( ch = getchar() ) >= 0
            ) {
                /* ch is the next character */    ....
    to read characters one at a time using getchar() until a negative value is returned.

    " >


  7. Read a file of English text, and print it out one word per line, all punctuation and non-alpha characters being omitted.

    For end-of-data, the program loop should read until "getchar" delivers a value <= 0. When typing input, end the data by typing the end-of-file character, usually control-D. When reading from a file, "getchar" will deliver a negative value when it encounters the end of the file.

    Typical output might be


    " >

Next: Arrays Up: Looping and Iteration Previous: break and continue
Fri May 20 13:40:49 BST 1994