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
0is 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
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:
- Take the distance from the point to the center of the circle
- 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 thesqrt()function. This function takes in adoubleand returns the square root of it- You are not permitted to use an array in this exercise
</math.h>
When you think your program is working,
you can use autotest
to run some simple automated tests:
1511 autotest circle_generator
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
nwill 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:
1511 autotest numbered_hourglass
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;
}