Week 03 Extra Sample Solutions

Information

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

Exercise
(●◌◌)
:

Loop Sum

Write a program called loop_sum.c that reads an integer n from standard input, and then scans in n integers from standard input, adds them together, then prints the sum.

You can assume that n is non-negative (>= 0).

You can assume that you are given exactly n + 1 integers.

You can assume that scanf succeeds (you do not need to check for errors).

Make your program match the examples below exactly.

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

Examples

dcc loop_sum.c -o loop_sum
./loop_sum 
How many numbers: 2
1
2
The sum is: 3
How many numbers: 3
-3
4
13
The sum is: 14
How many numbers: 5
-2
-1
0
1
2
The sum is: 0

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

1091 autotest loop_sum
Sample solution for loop_sum.c
// Read numbers in a loop, and calculate their sum.
// A sample solution.

#include <stdio.h>
#include <stdlib.h>

int read_num(void);

int main(void) {
    printf("How many numbers: ");
    int amount = read_num();

    int sum = 0;
    int i = 0;
    while (i < amount) {
        sum += read_num();
        i++;
    }

    printf("The sum is: %d\n", sum);

    return 0;
}

// Note: you did not need to check that scanf succeeds for this exercise.
int read_num(void) {
    int num;
    if (scanf("%d", &num) != 1) {
        printf("Could not read a number\n");
        exit(EXIT_FAILURE);
    }
    return num;
}

Exercise
(●◌◌)
:

What Zodiac Animal are you?

Download zodiac_animal.c here, or copy it to your CSE account using the following command:

cp -n /import/reed/A/dp1091/public_html/24T3/activities/zodiac_animal/zodiac_animal.c .

Complete the C program zodiac_animal.c, which should first ask the user to enter their birth year. It should then print out what (approximate) Chinese zodiac animal that year corresponds to.

(For simplicity, we'll assume that the each calendar year corresponds exactly to a zodiac animal).

The starter code contains an enum zodiac_animal, which defines the different Chinese zodiac animals.

In your program, you should aim to:

  1. Scan in the year (as an integer)
  2. Calculate which zodiac animal that year corresponds to, and convert it into an enum zodiac_animal variable
  3. Use that enum to decide which message to print out.

Examples

dcc zodiac_animal.c -o zodiac_animal
./zodiac_animal
Enter the year of your birth: 1999
You were born in the year of the Rabbit!
./zodiac_animal
Enter the year of your birth: 2000
You were born in the year of the Dragon!
./zodiac_animal
Enter the year of your birth: 2001
You were born in the year of the Snake!
./zodiac_animal
Enter the year of your birth: 2002
You were born in the year of the Horse!
./zodiac_animal
Enter the year of your birth: 2003
You were born in the year of the Goat!
./zodiac_animal
Enter the year of your birth: 2004
You were born in the year of the Monkey!
./zodiac_animal
Enter the year of your birth: 2005
You were born in the year of the Rooster!
./zodiac_animal
Enter the year of your birth: 1900
You were born in the year of the Rat!
./zodiac_animal
Enter the year of your birth: 1901
You were born in the year of the Ox!
./zodiac_animal
Enter the year of your birth: 2022
You were born in the year of the Tiger!
./zodiac_animal
Enter the year of your birth: 1970
You were born in the year of the Dog!
./zodiac_animal
Enter the year of your birth: 1971
You were born in the year of the Pig!

Assumptions/Restrictions/Clarifications

  • You can assume the year entered will be no earlier than 1900, (we will not test any years before 1900).
  • You may assume that your program will only scan in integers
  • Your program is not required handle any years before 1900 (You don't need to check this).

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

1091 autotest zodiac_animal
Sample solution for zodiac_animal.c
// Prints out the zodiac animal corresponding to a given year.
// Written by <YOUR-NAME-HERE> (<YOUR-ZID>) 


#include <stdio.h>

// This year corresponds to the Rat.
#define MIN_YEAR 1900

// Enum defining the different zodiac ANIMALS
enum zodiac_animal {
    RAT,
    OX,
    TIGER,
    RABBIT,
    DRAGON,
    SNAKE,
    HORSE,
    GOAT,
    MONKEY,
    ROOSTER,
    DOG,
    PIG
};


int main(void) {


    printf("Enter the year of your birth: ");

    int birth_year;
    scanf("%d", &birth_year);

    birth_year = (birth_year - MIN_YEAR) % (PIG + 1);

    if (birth_year < 0) {
        birth_year = birth_year + (PIG + 1);
    }

    enum zodiac_animal zodiac = birth_year;


    if (zodiac == RAT) {
        printf("You were born in the year of the Rat!\n");
    } else if (zodiac == OX) {
        printf("You were born in the year of the Ox!\n");
    } else if (zodiac == TIGER) {
        printf("You were born in the year of the Tiger!\n");
    } else if (zodiac == RABBIT) {
        printf("You were born in the year of the Rabbit!\n");
    } else if (zodiac == DRAGON) {
        printf("You were born in the year of the Dragon!\n");
    } else if (zodiac == SNAKE) {
        printf("You were born in the year of the Snake!\n");
    } else if (zodiac == HORSE) {
        printf("You were born in the year of the Horse!\n");
    } else if (zodiac == GOAT) {
        printf("You were born in the year of the Goat!\n");
    } else if (zodiac == MONKEY) {
        printf("You were born in the year of the Monkey!\n");
    } else if (zodiac == ROOSTER) {
        printf("You were born in the year of the Rooster!\n");
    } else if (zodiac == DOG) {
        printf("You were born in the year of the Dog!\n");
    } else if (zodiac == PIG) {
        printf("You were born in the year of the Pig!\n");
    } 

    return 0;
}

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.

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

1091 autotest sawtooth
Sample solution for sawtooth.c
// Program to print out a sawtooth pattern based on a given height and length.
// Written by Rory Golledge (September 2021)
#include <stdio.h>

int main(void) {
    int height;
    int length;

    printf("Please enter the height of the sawtooth: ");
    scanf("%d", &height);
    printf("Please enter the length of the sawtooth: ");
    scanf("%d", &length);

    int row = 0;
    while (row < height) {
        int col = 0;
        while (col < length) {
            if (row == col % height || col % height == 0) {
                printf("*");
            } else {
                printf(" ");
            }
            col++;
        }
        printf("\n");
        row++;
    }
    return 0;
}

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

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

1091 autotest circle_generator
Sample solution for circle_generator.c
// Program to print out an circle with various features
// input size.
// Written by Rory Golledge (March 2022)

#include <stdio.h>
#include <math.h>

int main(void) {
    int radius;
    char is_hollow;

    printf("Please enter a radius: ");
    scanf("%d", &radius);
    printf("Is the circle hollow? ");
    scanf(" %c", &is_hollow);

    int row = -radius;
    while (row <= radius) {
        int col = -radius;
        while (col <= radius) {
            double dist = sqrt(row * row + col * col);
            if (is_hollow == 'n' && dist < radius) {
                printf("* ");
            } else if ((int)dist == radius) {
                printf("# ");
            } else {
                printf(". ");
            }
            col++;
        }
        printf("\n");
        row++;
    }

    return 0;
}

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

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

1091 autotest numbered_hourglass
Sample solution for numbered_hourglass.c
// Program to print out an hourglass shape given an
// input size.
// Written by Rory Golledge (June 2021)

#include <stdio.h>

int main(void) {
    int input;
    printf("Please enter a size: ");
    scanf("%d", &input);

    int row = 0;
    while (row < input) {
        int col = 0;
        while (col < input) {
            // Take care of outlines
            if (row == 0 || row == input - 1) {
                printf(" -");
            }
            // Take care of Top-half triangle
            else if (row <= input / 2) {
                if (row <= col && row + col <= input - 1) {
                    printf("%2d", row);
                } else {
                    printf(" -");
                }
            }
            // Take care of Bottom-half triangle
            else {
                if (row >= col && row + col >= input - 1) {
                    printf("%2d", input - 1 - row);
                } else {
                    printf(" -");
                }
            }

            col++;
        }
        printf("\n");
        row++;
    }

    return 0;
}