|
|
DT249-1 Programming and Algorithms
Week 03
Learning Outcomes:
Upon completion of this class, you should be able to:
- Write programs which contain if statements.
- Write programs which contain switch statements.
- Distinguish between the appropriate uses for if and switch
statements.
- Predict the behaviour of programs with if and switch statements.
- Write programs with iterative components.
- Create iteration in a program using a while loop.
- Create iteration in a program using a do..while loop.
- Create iteration in a program using a for loop.
- Distinguish between the appropriate uses for while, do..while and
for loops.
- Predict the behaviour of programs with while, do..while and for
loops.
Tasks
|
1. |
Complete the tasks from
last week. |
|
2. |
Download the code for
this week's notes. Go through all the code, compiling and running
each program separately. If you have any problems, ask the lab
supervisor for help, or take a note of the problem and we can
discuss it in next week's class. The code is
bundled into a ZIP file. This is a compressed file which contains
all the separate source code files. To open this file and extract
the contents, you can use the WinZip tool which is available in the
lab, and is available for free download
here.
Make sure to put all your code into a folder named
week_3 in your programming folder on your u: drive. |
|
3. |
Write a program which reads in two integers and
checks whether the first is evenly divisible by the second.
Call this program paa_ex_03_01.c and put it in your
week_3 folder.
Please see sample solution
here. |
|
4. |
Write a program to input a number 1 - 7 from the
keyboard. Your program should display the day of the week
corresponding to the number (where 1 is Sunday, 2 is Monday etc.).
If an incorrect number is entered, an error message must be
displayed. Call this program paa_ex_03_02.c
and put it in your week_3 folder.
Please see sample solutions here,
here, here
(incorrect), here and
here. |
|
5. |
Write a program to sum (add up) all the odd numbers
between 0 and 100 using a while loop. Call
this program paa_ex_03_03.c and put it in your week_3 folder.
Please see sample solutions here and
here. |
|
6. |
Write a program to sum (add up) all the odd numbers
between 0 and 100 using a for loop. Call
this program paa_ex_03_04.c and put it in your week_3 folder.
Please see sample solutions here,
here, here,
here and here. |
Further Reading
Chapters 5 & 6 of Paul Kelly book.
|