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

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

Examples

dcc sawtooth.c -o sawtooth
./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
*       *       *       *       *       *
**      **      **      **      **      **
* *     * *     * *     * *     * *     * *
*  *    *  *    *  *    *  *    *  *    *  *
*   *   *   *   *   *   *   *   *   *   *   *
*    *  *    *  *    *  *    *  *    *  *
*     * *     * *     * *     * *     * *
*      **      **      **      **      **

Assumptions/Restrictions/Clarifications

  • 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.

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.

Examples

dcc circle_generator.c -o circle_generator
./circle_generator
Please enter a radius: 4
Is the circle hollow? y
. . # # # # # . .
. # . . . . . # .
# . . . . . . . #
# . . . . . . . #
# . . . . . . . #
# . . . . . . . #
# . . . . . . . #
. # . . . . . # .
. . # # # # # . .
./circle_generator
Please enter a radius: 4
Is the circle hollow? n
. . # # # # # . .
. # * * * * * # .
# * * * * * * * #
# * * * * * * * #
# * * * * * * * #
# * * * * * * * #
# * * * * * * * #
. # * * * * * # .
. . # # # # # . .
./circle_generator
Please enter a radius: 15
Is the circle hollow? n
. . . . . . . . . . # # # # # # # # # # # . . . . . . . . . .
. . . . . . . . # # * * * * * * * * * * * # # . . . . . . . .
. . . . . . # # * * * * * * * * * * * * * * * # # . . . . . .
. . . . . # # * * * * * * * * * * * * * * * * * # # . . . . .
. . . . # * * * * * * * * * * * * * * * * * * * * * # . . . .
. . . # * * * * * * * * * * * * * * * * * * * * * * * # . . .
. . # # * * * * * * * * * * * * * * * * * * * * * * * # # . .
. . # * * * * * * * * * * * * * * * * * * * * * * * * * # . .
. # * * * * * * * * * * * * * * * * * * * * * * * * * * * # .
. # * * * * * * * * * * * * * * * * * * * * * * * * * * * # .
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * #
. # * * * * * * * * * * * * * * * * * * * * * * * * * * * # .
. # * * * * * * * * * * * * * * * * * * * * * * * * * * * # .
. . # * * * * * * * * * * * * * * * * * * * * * * * * * # . .
. . # # * * * * * * * * * * * * * * * * * * * * * * * # # . .
. . . # * * * * * * * * * * * * * * * * * * * * * * * # . . .
. . . . # * * * * * * * * * * * * * * * * * * * * * # . . . .
. . . . . # # * * * * * * * * * * * * * * * * * # # . . . . .
. . . . . . # # * * * * * * * * * * * * * * * # # . . . . . .
. . . . . . . . # # * * * * * * * * * * * # # . . . . . . . .
. . . . . . . . . . # # # # # # # # # # # . . . . . . . . . .

Assumptions/Restrictions/Clarifications

  • 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

</math.h>

Exercise
(●●●)
:

Numbered Hourglass

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

For examples:

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 -
 - - - - - - - - -

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

Examples

dcc numbered_hourglass.c -o numbered_hourglass
./numbered_hourglass
Please enter a size: 3
 - - -
 - 1 -
 - - -
./numbered_hourglass
Please enter a size: 7
 - - - - - - -
 - 1 1 1 1 1 -
 - - 2 2 2 - -
 - - - 3 - - -
 - - 2 2 2 - -
 - 1 1 1 1 1 -
 - - - - - - -
./numbered_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 -
 - - - - - - - - - - - - - - -
./numbered_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 -
 - - - - - - - - - - - - - - - - - - - - - - - - -

Assumptions/Restrictions/Clarifications

  • You can assume that n will always be odd
  • You can assume that n &gt;= 3