Programming Fundamentals

Information

  • This page contains additional revision 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 may be available for some them (Command included at bottom of each exercise if applicable).

Exercise — individual:
Simple Counting Up

Write a program simple_counting_up.c that scans in an integer n, then counts up to n from 1.

The program should:

  1. Prompt for the integer n with the message: Enter a number to count to: .
  2. Print the message: Counting to <number>: , where <number> is the integer n that is scanned in.
  3. Count up to the integer n from 1.

Examples

dcc simple_counting_up.c -o simple_counting_up
./simple_counting_up
Enter a number to count to: 4
Counting to 4: 1 2 3 4
./simple_counting_up
Enter a number to count to: 6
Counting to 6: 1 2 3 4 5 6 

Assumptions/Restrictions/Clarifications

  • You may assume that all scanned in values will be positive integers greater than 0.

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

1511 autotest simple_counting_up

Exercise — individual:
Simple Counting Down

Write a program simple_counting_down.c that scans in an integer n, then counts down to 1 from n.

The program should:

  1. Prompt for the integer n with the message: Enter a number to count from: .
  2. Print the message: Counting from <number>: , where <number> is the integer n that is scanned in.
  3. Count down from the integer n to 1.

Examples

dcc simple_counting_down.c -o simple_counting_down
./simple_counting_down
Enter a number to count from: 4
Counting from 4: 4 3 2 1
./simple_counting_down
Enter a number to count from: 6
Counting from 6: 6 5 4 3 2 1

Assumptions/Restrictions/Clarifications

  • You may assume that all scanned in values will be positive integers greater than 0.

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

1511 autotest simple_counting_down

Exercise — individual:
Print a Solid Square

Write a program solid_square.c that reads an integer n from standard input and prints a solid square of size n x n made up of asterisks * .

Examples

dcc solid_square.c -o solid_square
./solid_square
Enter a number: 3
***
***
***
./solid_square
Enter a number: 5
*****
*****
*****
*****
*****

Assumptions/Restrictions/Clarifications

  • You may assume that your program will only scan in positive integers.
  • Your program only has to handle sizes in the range 1 to 20 inclusive.

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

1511 autotest solid_square

Exercise — individual:
Print an Outlined Rectangle

Write a program outlined_rectangle.c that reads an integer n from standard input and prints a of a rectangle of size n x n.

The rectangle should have

  • Hyphens - on the top and bottom sides of the rectangle.
  • Pipe | on the left and right side of the rectangle, not including the top and bottom of each side.
  • Asterisks * everywhere else.

Examples

dcc outlined_rectangle.c -o outlined_rectangle
./outlined_rectangle
Enter a number: 3
---
|*|
---
./outlined_rectangle
Enter a number: 5
-----
|***|
|***|
|***|
-----
./outlined_rectangle
Enter a number: 8
--------
|******|
|******|
|******|
|******|
|******|
|******|
--------

Assumptions/Restrictions/Clarifications

  • You may assume that your program will only scan in positive integers.
  • You may assume that the scanned in value will be 3 or greater.

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

1511 autotest outlined_rectangle

Exercise — individual:
Print a Z

Write a program print_z.c that reads in an integer n from standard input and prints a Z of size n made of * .

The Z should have:

  • The top row as n asterisks *.
  • The bottom row as n asterisks *.
  • A diagonal line of asterisks * connecting the top and bottom row from top right to bottom left.

Examples

dcc print_z.c -o print_z
./print_z
Enter a number: 3
***
 * 
*** 
./print_z
Enter a number: 5
*****
   * 
  *  
 *   
*****
./print_z
Enter a number: 11
***********
         * 
        *  
       *   
      *    
     *     
    *      
   *       
  *        
 *         
***********

Assumptions/Restrictions/Clarifications

  • You may assume that your program will only scan in positive integers.
  • You may assume that the scanned in value will be 3 or greater.

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

1511 autotest print_z