UWA Logo Computer Science & Software Engineering
C Programming (CITS1210) - Labsheet 2
   Faculty Home  |  CSSE Home  |  csentry  |  CITS1210  |  help1210

Labsheet 2: Repetition and Control Flow

For the week commencing Monday 11th August.

Some of this week's tasks will require you to write programs that accept a single argument from the command-line and convert it to a number. The following code will do this conversion for you:

#include <stdlib.h>
.....
//  Determine the numerical value of the first argument to the program  

int n = atoi( argv[1] );

//  n now holds the numerical value of the first argument

The sample program starter.c extends this concept by allowing for any arbitrary number of arguments, converting each to a number before calling a function called process to display the value. You may find this program useful for the later tasks on this labsheet.

Additionally, when displaying output to the screen, note that the last line of the output must end with a newline character, otherwise the line will be over-written by the shell's next command prompt. A newline character is denoted by the sequence \n and denotes "go to the next line" to the output device (e.g. your screen).


This week's tasks:

Remember that we're using a to indicate if a question is a bit more difficult.

1.
 
Write a program to display the following patterns of asterisks:

i)
One line containing 10 asterisks.

Remember to add a newline character (represented by the string "\n") to the end of your display to ensure your output will be observable on the screen. So, if you used some kind of loop (as you should) to do this question (a for loop is advisable), you will need to add the line:

printf("\n");

after the loop to ensure the output is observable on the screen.

ii)
20 lines, each line containing 1 asterisk.
iii)
20 lines, each line containing 10 asterisks (i.e. a filled rectangle of asterisks).
iv)
20 lines, each line containing <line-number> asterisks. For example:
*
**
***
****
        ...
v)
The same as iv., except that the asterisks are aligned at the right. For example:
                   *
                  **
                 ***
        ...
  ******************
 *******************
********************
vi)
20 lines, each line containing <line-number> asterisks provided the <line-number> is odd. For example:
*
***
*****
        ...
vii)
The same as vi., except the asterisks are centred aligned. For example:
         *
        ***
       *****
      *******
     *********
        ...
viii)
The same as vii., except the "pyramid" is additionally "reflected" to form a "diamond". For example:
         *
        ***
       *****
        ...
 ***************** 
*******************
 ***************** 
        ...
       *****
        ***
         *
ix)
The same as vii., except only the asterisks at the edge of the pyramid are printed. For example:
         *
        * *
       *   *
      *     *
     *       *
        ...
x)
The same as vii., except the bottom rows of the pyramid are "interlaced" in reverse order with the top rows. For example:
         *
*******************
        ***
 ***************** 
       ***** 
  ***************  
        ...
2.
A year is a leap year if it is divisible by 400, or if it is divisible by 4 and not divisible by 100. Write a program that determines if the year supplied as the single argument to the program is a leap year or not. For example 1896, 2000, and 2004 are leap years, while 1897, 1900, and 1902 are not.
3.
Consider the following statement: a child's parents promise to give the child $2 on her 12th birthday and double the gift on every subsequent birthday until the gift exceeds $1000, at which time the amount would be capped at $1000 and remain constant there-after (yes, the child's parents are generous!). Write a program to calculate the size of the gift at any given age and the total received by the child up to that age.
4.
Write a program to reverse the digits of a positive integer number supplied as the single argument to the program. For example, if the number is 1234, the reversed number should be 4321. To reverse the digits, "decompose" the number into two parts: the units digit (found by % 10), and the multiples of 10 (found by / 10) and repeat the process on the multiples of 10 part. Your function should return the reversed number, not simply print it out.
5.
Write a program that determines the minimum, maximum, and average of the (assumed to be numerical) arguments supplied to the program.

You may find the sample program starter.c useful in demonstrating how to loop through all the arguments to the program and convert them to numbers.

6.
Write a program that prints out the ordinal description of the (assumed to be numerical) arguments supplied to the program. For example:
csse2100% ./ordinal 1 6 11 12 21 22 23
1st
6th
11th
12th
21st
22nd
23rd
7.
Write a program that prints out the "byte size" description of the (assumed to be numerical) arguments supplied to the program. For example:
csse2100% ./bytesize 1 1200 2444555 5666777888
1Byte
1KByte
2MByte
5GByte
8.

Write a program that prints out all "Roman numeral" equivalents of the numbers between 1 and the single argument supplied to the program.
Use the rules for writing Roman numerals as stated on: http://mathforum.org/dr.math/faq/faq.roman.html.
Top of Page
CRICOS Provider Code: 00126G