Programming Fundamentals

Information

  • This page contains extra challenge exercises for week 03.
  • These exercises are not compulsory, nor do they provide any marks in the course.
  • These exercises are intended for students that want more challenge in the course.
  • You cannot submit any of these exercises, however autotests are available for them (Command included at bottom of each exercise).

Exercise
(●●◌)
:

Sawtooth

This activity was moved later in the exercise after some students found it more difficult than it was intended to be. It's now a 2-circle exericse, rather than a 1-circle exercise.

Write a program sawtooth.c that reads in two non-negative integers, a height and a length. The height represents how tall the sawtooth pattern will be and the length represents how long the sawtooth will be.

A sawtooth pattern looks like the below:

*     *     *     *     *     *
**    **    **    **    **    **
* *   * *   * *   * *   * *   * *
*  *  *  *  *  *  *  *  *  *  *  *
*   * *   * *   * *   * *   * *   *
*    **    **    **    **    **    *
In this example, the height is 6 and the length is 36

Here are some examples of how your program should run:

 ./sawtooth
Please enter the height of the sawtooth: 4
Please enter the length of the sawtooth: 16
*   *   *   *
**  **  **  **
* * * * * * * *
*  **  **  **  *
./sawtooth
Please enter the height of the sawtooth: 6
Please enter the length of the sawtooth: 36
*     *     *     *     *     *
**    **    **    **    **    **
* *   * *   * *   * *   * *   * *
*  *  *  *  *  *  *  *  *  *  *  *
*   * *   * *   * *   * *   * *   *
*    **    **    **    **    **    *

./sawtooth
Please enter the height of the sawtooth: 8
Please enter the length of the sawtooth: 45
*       *       *       *       *       *
**      **      **      **      **      **
* *     * *     * *     * *     * *     * *
*  *    *  *    *  *    *  *    *  *    *  *
*   *   *   *   *   *   *   *   *   *   *   *
*    *  *    *  *    *  *    *  *    *  *
*     * *     * *     * *     * *     * *
*      **      **      **      **      **

Note: When you are testing your program with larger inputs, you may need to change the size of your terminal to prevent output text from wrapping around the screen. If you do not know how to do this, ask your tutor during the lab time

Assumptions

  • All heights will be non-negative integers
  • All lengths will be positive integers. Although a length of 0 is possible, it can be interpreted differently and will not be tested.

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest sawtooth

Exercise
(●●◌)
:

Circle Generator

Write a program called circle_generator.c that prompts the user to enter an integer r that corresponds to the radius of the circle. They will also be prompted as to whether the circle is hollow or not.

The program will then print a coloured circle of radius r with an outline.

Please enter a radius: 4
Is the circle hollow? y
. . # # # # # . .
. # . . . . . # .
# . . . . . . . #
# . . . . . . . #
# . . . . . . . #
# . . . . . . . #
# . . . . . . . #
. # . . . . . # .
. . # # # # # . .

Please enter a radius: 4
Is the circle hollow? n
. . # # # # # . .
. # * * * * * # .
# * * * * * * * #
# * * * * * * * #
# * * * * * * * #
# * * * * * * * #
# * * * * * * * #
. # * * * * * # .
. . # # # # # . .

Please enter a radius: 15
Is the circle hollow? n
. . . . . . . . . . # # # # # # # # # # # . . . . . . . . . .
. . . . . . . . # # * * * * * * * * * * * # # . . . . . . . .
. . . . . . # # * * * * * * * * * * * * * * * # # . . . . . .
. . . . . # # * * * * * * * * * * * * * * * * * # # . . . . .
. . . . # * * * * * * * * * * * * * * * * * * * * * # . . . .
. . . # * * * * * * * * * * * * * * * * * * * * * * * # . . .
. . # # * * * * * * * * * * * * * * * * * * * * * * * # # . .
. . # * * * * * * * * * * * * * * * * * * * * * * * * * # . .
. # * * * * * * * * * * * * * * * * * * * * * * * * * * * # .
. # * * * * * * * * * * * * * * * * * * * * * * * * * * * # .
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
. # * * * * * * * * * * * * * * * * * * * * * * * * * * * # .
. # * * * * * * * * * * * * * * * * * * * * * * * * * * * # .
. . # * * * * * * * * * * * * * * * * * * * * * * * * * # . .
. . # # * * * * * * * * * * * * * * * * * * * * * * * # # . .
. . . # * * * * * * * * * * * * * * * * * * * * * * * # . . .
. . . . # * * * * * * * * * * * * * * * * * * * * * # . . . .
. . . . . # # * * * * * * * * * * * * * * * * * # # . . . . .
. . . . . . # # * * * * * * * * * * * * * * * # # . . . . . .
. . . . . . . . # # * * * * * * * * * * * # # . . . . . . . .
. . . . . . . . . . # # # # # # # # # # # . . . . . . . . . .

Hints, Notes and Assumptions

  • You can assume that the radius given is non-negative and valid
  • You can assume that hollowness of the circle will be either 'y' or 'n'
  • You can calculate the distance between two points by finding their Euclidean distance.
  • To determine if a point is in the border region of the circle, perform the following steps:
    1. Take the distance from the point to the center of the circle
    2. If the integer component of this distance is equal to the radius, the point is on the border
  • #include <math.h> at the top of your code to use the sqrt() function. This function takes in a double and returns the square root of it.
  • You are not permitted to use an array in this exercise.
For this exercise, try and break the problem down into small parts and solve them individually.

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest circle_generator

Exercise
(●●●)
:

Numbered Hourglass

Write a program called numbered_hourglass.c that prompts the user to enter an integer n and prints an nxn pattern containing an hourglass. This hourglass will contain positive integers that decrease from the middle, all surrounded by hyphens "-".

Please enter a size: 9
 - - - - - - - - -
 - 1 1 1 1 1 1 1 -
 - - 2 2 2 2 2 - -
 - - - 3 3 3 - - -
 - - - - 4 - - - -
 - - - 3 3 3 - - -
 - - 2 2 2 2 2 - -
 - 1 1 1 1 1 1 1 -
 - - - - - - - - -

You can assume n is odd and >= 3.

Make your program match the examples below exactly.

This exercise is designed to give you practice with while loops and if statements. Do not use arrays for this exercise!

Note: you are not permitted to use an array in this exercise.

./hourglass
Please enter a size: 3
 - - -
 - 1 -
 - - -

./hourglass
Please enter a size: 7
 - - - - - - -
 - 1 1 1 1 1 -
 - - 2 2 2 - -
 - - - 3 - - -
 - - 2 2 2 - -
 - 1 1 1 1 1 -
 - - - - - - -

./hourglass
Please enter a size: 15
 - - - - - - - - - - - - - - -
 - 1 1 1 1 1 1 1 1 1 1 1 1 1 -
 - - 2 2 2 2 2 2 2 2 2 2 2 - -
 - - - 3 3 3 3 3 3 3 3 3 - - -
 - - - - 4 4 4 4 4 4 4 - - - -
 - - - - - 5 5 5 5 5 - - - - -
 - - - - - - 6 6 6 - - - - - -
 - - - - - - - 7 - - - - - - -
 - - - - - - 6 6 6 - - - - - -
 - - - - - 5 5 5 5 5 - - - - -
 - - - - 4 4 4 4 4 4 4 - - - -
 - - - 3 3 3 3 3 3 3 3 3 - - -
 - - 2 2 2 2 2 2 2 2 2 2 2 - -
 - 1 1 1 1 1 1 1 1 1 1 1 1 1 -
 - - - - - - - - - - - - - - -

 ./hourglass
Please enter a size: 25
 - - - - - - - - - - - - - - - - - - - - - - - - -
 - 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -
 - - 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 - -
 - - - 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 - - -
 - - - - 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 - - - -
 - - - - - 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 - - - - -
 - - - - - - 6 6 6 6 6 6 6 6 6 6 6 6 6 - - - - - -
 - - - - - - - 7 7 7 7 7 7 7 7 7 7 7 - - - - - - -
 - - - - - - - - 8 8 8 8 8 8 8 8 8 - - - - - - - -
 - - - - - - - - - 9 9 9 9 9 9 9 - - - - - - - - -
 - - - - - - - - - -1010101010 - - - - - - - - - -
 - - - - - - - - - - -111111 - - - - - - - - - - -
 - - - - - - - - - - - -12 - - - - - - - - - - - -
 - - - - - - - - - - -111111 - - - - - - - - - - -
 - - - - - - - - - -1010101010 - - - - - - - - - -
 - - - - - - - - - 9 9 9 9 9 9 9 - - - - - - - - -
 - - - - - - - - 8 8 8 8 8 8 8 8 8 - - - - - - - -
 - - - - - - - 7 7 7 7 7 7 7 7 7 7 7 - - - - - - -
 - - - - - - 6 6 6 6 6 6 6 6 6 6 6 6 6 - - - - - -
 - - - - - 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 - - - - -
 - - - - 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 - - - -
 - - - 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 - - -
 - - 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 - -
 - 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 -
 - - - - - - - - - - - - - - - - - - - - - - - - -
For this exercise, try and break the problem down into small parts and solve them individually.
For this exercise, you will need to print out a number that is always 2 characters wide. To do this, you should use printf("%2d", my_number); rather than printf("%d", my_number);.

When you think your program is working, you can use autotest to run some simple automated tests:

1511 autotest numbered_hourglass