| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

More Exercises with Counted Loops

Page history last edited by Steve Sweeney 9 years, 3 months ago

 

  1. [medium difficulty] The Fibonacci numbers are a sequence of numbers where the next number in the sequence is the sum of the previous two numbers.  The first number is 0 and the second number is 1.  Write a program that outputs the first N numbers in the sequence, starting with 0.  The user should be asked how many numbers of the sequence to output.

    For Example:  The first 7 numbers are 0 1 1 2 3 5 8

     

    Hint 1 Hint 2 Hint 3
    need two variables to keep track of previous two values, and one to calculate the current number after displaying the current number, update your "previous values"; in a more mathematical form:
    N <-- old_N + really_old_N
    output N
    really_old_N <-- old_N
    old_N <-- N

    we already know the first two numbers, so we just display them outside the loop; whichever number the user gives is reduced by two for the loop

     

  2. Output the numbers from -10 to 10.  Beside each number output the square of each number

    Extension:

    (a) In addition, output the square root of each number.  Since you cannot take the square root of a negative number, leave those ones blank or put a message about it.

     

    Hint 1 Hint 2 Hint 3 (ext_a)
    for i : -10 ... 10 inside the loop, output the number on one line and its square on another;now put them on the same line use an "if" statement inside the loop to decide if we're able to take the square root; choose output calculations and output accordingly

     

  3. Modify the program from #3 from (Exercises - Intro to Counted Loops) to allow the user to create a wave pattern.  The user should be asked to input the height of the wave (say from 1 to 10), and the number of waves.

    For example, for wave height = 3 and number of waves = 2, the output might be:

    *

    **

    ***

    ***

    **

    *

    *

    **

    ***

    **

    *

    Extension:

    (a) Modify your algorithm to produce a more interesting wave pattern.  You might want to explore some more complicated mathematical operations.  Remember that you can only output an integer number of symbols.
    (b) A much more interesting, but challenging, wave pattern is horizontal instead of vertical.  For example, the same wave as before, with height = 3 and waves = 2, would look like:

      **    **
     ****  ****
    ************

     

  4. [challenge] Suppose there is an island with two rabbits (male and female).  Every 30 days, the population of rabbits doubles.  Write a program that has the user input the number of days and then calculates the new population.  Use a counted loop.

    Extension:

    (a) Let the user specify the initial population of the island.

    (b) Let the user specify how many days it takes for the population to double.

     

  5. [challenge] Create a stopwatch program that displays minutes and seconds (to one decimal place, or one tenth of a second).  For example, 4:32.7

    Hint:  You will need to use the delay() function in Turing.  You might also want to look into the cls (clear screen) function to improve the look of your output.

     

 

Comments (0)

You don't have permission to comment on this page.