DPST1091 Revision If While Functions Variables IO Sample Solutions

Revision Exercise: Remove non-digits from input (●◌◌)

Write a C program digit_filter.c which reads characters from its input and writes character to its output.

Digits ('0' .. '9'), spaces and newline characters should be written unchanged.

All other characters should be replaced with a '.' character.

Your program should stop only at the end of input.

For example:

dcc -o digit_filter digit_filter.c
./digit_filter
This is week 9 of COMP1511.
.... .. .... 9 .. ....1511.
I paid $10.50 for 3 cups of coffee
. .... .10.50 ... 3 .... .. ......
i is the symbol for the square root of -1
. .. ... ...... ... ... ...... .... .. .1
0123456789abcdef
0123456789......

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

1091 autotest digit_filter
Sample solution for digit_filter.c
//  Written 3/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
//  read characters from stdin and write characters to stdout
//  digits (0-9) newline and space characters are written unchanged
//  other characters are replaced by the character '.'

#include <stdio.h>

int is_unchanged(int character);

int main(void) {
    // getchar returns an int which will contain either
    // the ASCII code of the character read or EOF

    int character = getchar();
    while (character != EOF) {

        if (is_unchanged(character)) {
            putchar(character);
        } else {
            putchar('.');
        }

        character = getchar();
    }

    return 0;
}

// return 1 if character is a lower case vowel
// 0 otherwise

int is_unchanged(int character) {
    return character == ' ' ||
           character == '\n' ||
           (character >= '0' && character <= '9') ;
}

Revision 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;
}

Revision Exercise: Print an Hourglass (●●◌)

Write a program called hourglass.c that prompts the user to enter an integer n and prints an nxn pattern containing an hourglass. This hourglass will be made up of *'s', all surrounded by hyphens "-".

Please enter a size: 9
*********
-*******-
--*****--
---***---
----*----
---***---
--*****--
-*******-
*********

You can assume n is >= 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.

Examples

dcc hourglass.c -o hourglass
./hourglass
Please enter a size: 3
***
-*-
***
./hourglass
Please enter a size: 6
******
-****-
--**--
--**--
-****-
******
./hourglass
Please enter a size: 7
*******
-*****-
--***--
---*---
--***--
-*****-
*******
Please enter a size: 10
**********
-********-
--******--
---****---
----**----
----**----
---****---
--******--
-********-
**********
./hourglass
Please enter a size: 15
***************
-*************-
--***********--
---*********---
----*******----
-----*****-----
------***------
-------*-------
------***------
-----*****-----
----*******----
---*********---
--***********--
-*************-
***************
 ./hourglass
Please enter a size: 25
*************************
-***********************-
--*********************--
---*******************---
----*****************----
-----***************-----
------*************------
-------***********-------
--------*********--------
---------*******---------
----------*****----------
-----------***-----------
------------*------------
-----------***-----------
----------*****----------
---------*******---------
--------*********--------
-------***********-------
------*************------
-----***************-----
----*****************----
---*******************---
--*********************--
-***********************-
*************************
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:

1091 autotest hourglass
Sample solution for 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 Top-half triangle
            if (row < input / 2) {
                if (row <= col && row + col <= input - 1) {
                    printf("*");
                } else {
                    printf("-");
                }
            }
            // Take care of Bottom-half triangle
            else {
                if (row >= col && row + col >= input - 1) {
                    printf("*");
                } else {
                    printf("-");
                }
            }

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

    return 0;
}

Revision Exercise: Ordering Three Integers (●◌◌)

Write a C program order3.c using if statements (no loops) that reads 3 integers and prints them from smallest to largest.

Examples

dcc order3.c -o order3
./order3
Enter integer: 23
Enter integer: 5
Enter integer: 27
The integers in order are: 5 23 27
./order3
Enter integer: 3
Enter integer: 6
Enter integer: 27
The integers in order are: 3 6 27
./order3
Enter integer: 9
Enter integer: 8
Enter integer: 7
The integers in order are: 7 8 9

You can assume the user supplies 3 integers. You do not have to check the return value from scanf.

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

1091 autotest order3
Sample solution for order3.c
// Modified 3/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Print 3 integers in non-decreasing order

#include <stdio.h>

int main(void) {
    int a, b, c;
    int tmp;

    printf("Enter integer: ");
    if (scanf("%d", &a) != 1) {
        return 1; // EXIT_FAILURE would be more portable
    }

    printf("Enter integer: ");
    if (scanf("%d", &b) != 1) {
        return 1;
    }

    printf("Enter integer: ");
    if (scanf("%d", &c) != 1) {
        return 1;
    }

    // a, b, c can be in any order

    // swap a & b if they are not in order
    if (a > b) {
        tmp = b;
        b = a;
        a = tmp;
    }

    // swap a & c if they are not in order
    if (a > c) {
        tmp = c;
        c = a;
        a = tmp;
    }

    // a must be the smallest now

    // swap b & c if they are not in order
    if (b > c) {
        tmp = c;
        c = b;
        b = tmp;
    }

    // a, b, c now in  order

    printf("The integers in order are: %d %d %d\n", a, b, c);

    return 0;
}
Alternative solution for order3.c
// Program to take in three numbers then print them
// in decreasing order.
// Written by Rory Golledge (12/06/2021)
//
// Note: This solution provides an alternative to the much
// "smarter" solution given. This is in the case that a student
// does not understand the other as it may seem less
// intuitive.

#include <stdio.h>

int main(void) {
    int a;
    int b;
    int c;

    // Scan in all numbers
    printf("Enter integer: ");
    scanf("%d", &a);

    printf("Enter integer: ");
    scanf("%d", &b);

    printf("Enter integer: ");
    scanf("%d", &c);

    printf("The integers in order are: ");

    // Determine order from 6 possible combinations
    // and print them out.
    if (a <= b && b <= c) {
        printf("%d %d %d\n", a, b, c);
    } else if (a <= c && c <= b) {
        printf("%d %d %d\n", a, c, b);
    } else if (b <= a && a <= c) {
        printf("%d %d %d\n", b, a, c);
    } else if (b <= c && c <= a) {
        printf("%d %d %d\n", b, c, a);
    } else if (c <= a && a <= b) {
        printf("%d %d %d\n", c, a, b);
    } else {
        printf("%d %d %d\n", c, b, a);
    }

    return 0;
}

Revision Exercise: Ordering Three Integers Without If Statements (●●●)

Write a C program order3_challenge1.c that reads 3 integers and prints them from smallest to largest.

You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. This means,

  • You are not permitted to use if statements
  • You are not permitted to use loops (e.g. while)
  • You are not permitted to use the ternary ? operator
  • You are not permitted to use arrays. You are not permitted to define functions

You are not permitted to call functions other than printf and scanf. For example, you are not permitted to use functions from the math library.

You are not permitted to use printf inside expressions - you can only use printf as a statement (the way it has been used in lectures).

You are not permitted to assign variables inside expressions - you can only assign variables as a statement (the way it has been done in lectures).

For example, both of these are invalid:

(a < b) && printf("a"); // invalid
(a < b) && (a = b); // invalid

You can use printf to print the value of an expression, in other words you can have an expression inside printf.

Examples

dcc order3_challenge1.c -o order3_challenge1
./order3_challenge1
Enter integer: 23
Enter integer: 5
Enter integer: 27
The integers in order are: 5 23 27
./order3_challenge1
Enter integer: 3
Enter integer: 6
Enter integer: 27
The integers in order are: 3 6 27
./order3_challenge1
Enter integer: 9
Enter integer: 8
Enter integer: 7
The integers in order are: 7 8 9

This is more puzzle than a programming exercise.

Try to invent your own solution - don't google or ask others how do it.

Autotest is available to help you test your program - but it doesn't check that your code meets the above restrictions.

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

1091 autotest order3_challenge1
Sample solution for order3_challenge1.c
// Modified 3/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Print 3 integers in non-decreasing order
// without using functions: if/while or other control statements, ?:
// using only  C covered in the first 2 weeks of COMP1511 lectures

// This is a puzzle not a programming exercises

#include <stdio.h>

int main(void) {
    int a, b, c;
    int tmp;

    printf("Enter integer: ");
    scanf("%d", &a);

    printf("Enter integer: ");
    scanf("%d", &b);

    printf("Enter integer: ");
    scanf("%d", &c);

    tmp = b;
    b = a - (1 - (a > b)) * (a - b);
    a = a - (1 - (a < tmp)) * (a - tmp);
    tmp = c;
    c = a - (1 - (a > c)) * (a - c);
    a = a - (1 - (a < tmp)) * (a - tmp);
    tmp = c;
    c = b - (1 - (b > c)) * (b - c);
    b = b - (1 - (b < tmp)) * (b - tmp);

    printf("The integers in order are: %d %d %d\n", a, b, c);

    return 0;
}
Alternative solution for order3_challenge1.c
// Modified 3/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Print 3 integers in non-decreasing order
// without using functions: if/while or other control statements, ?:
// using only  C covered in the first 2 weeks of COMP1511 lectures

// This is a puzzle not a programming exercises

#include <stdio.h>

int main(void) {
    int a, b, c;
    int tmp;

    printf("Enter integer: ");
    if (scanf("%d", &a) != 1) {
        return 1; // EXIT_FAILURE would be more portable
    }

    printf("Enter integer: ");
    if (scanf("%d", &b) != 1) {
        return 1;
    }

    printf("Enter integer: ");
    if (scanf("%d", &c) != 1) {
        return 1;
    }

    tmp = b;
    b = a - (1 - (a > b)) * (a - b);
    a = a - (1 - (a < tmp)) * (a - tmp);
    tmp = c;
    c = a - (1 - (a > c)) * (a - c);
    a = a - (1 - (a < tmp)) * (a - tmp);
    tmp = c;
    c = b - (1 - (b > c)) * (b - c);
    b = b - (1 - (b < tmp)) * (b - tmp);

    printf("The integers in order are: %d %d %d\n", a, b, c);

    return 0;
}

Revision Exercise: Ordering Three Integers Without If Statements and With Only Three Variables (●●●)

Write a C program order3_challenge1.c that reads 3 integers and prints them from smallest to largest.

You are only permitted to use parts of C covered in the weeks 1 and 2 lectures. This means,

  • You are not permitted to use if statements
  • You are not permitted to use loops (e.g. while)
  • You are not permitted to use the ternary ? operator
  • You are not permitted to use arrays. You are not permitted to define functions

For this exercise you also cannot use more than 3 variables

You are not permitted to call functions other than printf and scanf. For example, you are not permitted to use functions from the math library.

You are not permitted to use printf inside expressions - you can only use printf as a statement (the way it has been used in lectures).

You are not permitted to assign variables inside expressions - you can only assign variables as a statement (the way it has been done in lectures).

For example, both of these are invalid:

(a < b) && printf("a"); // invalid
(a < b) && (a = b); // invalid

You can use printf to print the value of an expression, in other words you can have an expression inside printf.

Examples

dcc order3_challenge1.c -o order3_challenge1
./order3_challenge1
Enter integer: 23
Enter integer: 5
Enter integer: 27
The integers in order are: 5 23 27
./order3_challenge1
Enter integer: 3
Enter integer: 6
Enter integer: 27
The integers in order are: 3 6 27
./order3_challenge1
Enter integer: 9
Enter integer: 8
Enter integer: 7
The integers in order are: 7 8 9

This is more puzzle than a programming exercise.

Try to invent your own solution - don't google or ask others how do it.

Autotest is available to help you test your program - but it doesn't check that your code meets the above restrictions.

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

1091 autotest order3_challenge2
Sample solution for order3_challenge2.c
// Modified 3/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Print 3 integers in non-decreasing order
// without using: functions, if/while or other control statements, ?:
// using only  C covered in the first 2 weeks of COMP1511 lectures
// and only using 3 int variables

// This is a puzzle not a programming exercises

#include <stdio.h>

int main(void) {
    int a, b, c;

    printf("Enter integer: ");
    scanf("%d", &a);

    printf("Enter integer: ");
    scanf("%d", &b);

    printf("Enter integer: ");
    scanf("%d", &c);

    printf("The integers in order are:");
    printf(" %d", a - (1 - (a < (b - (1 - (b < c)) * (b - c)))) * (a - (b - (1 - (b < c)) * (b - c))));
    printf(" %d", (a - (1 - (a < b)) * (a - b)) - (1 - ((a - (1 - (a < b)) * (a - b)) > (c - (1 - (c < (a - (1 - (a > b)) * (a - b)))) * (c - (a - (1 - (a > b)) * (a - b)))))) * ((a - (1 - (a < b)) * (a - b)) - (c - (1 - (c < (a - (1 - (a > b)) * (a - b)))) * (c - (a - (1 - (a > b)) * (a - b))))));
    printf(" %d", a - (1 - (a > (b - (1 - (b > c)) * (b - c)))) * (a - (b - (1 - (b > c)) * (b - c))));
    printf("\n");

    return 0;
}
Alternative solution for order3_challenge2.c
// Modified 3/3/2018 by Andrew Taylor (andrewt@unsw.edu.au)
// as a lab example for COMP1511

// Print 3 integers in non-decreasing order
// without using: functions, if/while or other control statements, ?:
// using only  C covered in the first 2 weeks of COMP1511 lectures
// and only using 3 int variables

// This is a puzzle not a programming exercises

#include <stdio.h>

int main(void) {
    int a, b, c;

    printf("Enter integer: ");
    if (scanf("%d", &a) != 1) {
        return 1; // EXIT_FAILURE would be more portable
    }

    printf("Enter integer: ");
    if (scanf("%d", &b) != 1) {
        return 1;
    }

    printf("Enter integer: ");
    if (scanf("%d", &c) != 1) {
        return 1;
    }

    printf("The integers in order are:");
    printf(" %d", a - (1 - (a < (b - (1 - (b < c)) * (b - c)))) * (a - (b - (1 - (b < c)) * (b - c))));
    printf(" %d", (a - (1 - (a < b)) * (a - b)) - (1 - ((a - (1 - (a < b)) * (a - b)) > (c - (1 - (c < (a - (1 - (a > b)) * (a - b)))) * (c - (a - (1 - (a > b)) * (a - b)))))) * ((a - (1 - (a < b)) * (a - b)) - (c - (1 - (c < (a - (1 - (a > b)) * (a - b)))) * (c - (a - (1 - (a > b)) * (a - b))))));
    printf(" %d", a - (1 - (a > (b - (1 - (b > c)) * (b - c)))) * (a - (b - (1 - (b > c)) * (b - c))));
    printf("\n");

    return 0;
}

Revision 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;
}

Revision 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;
}

Revision 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;
}