CS201
Lab 5 - Looping, looping, and even more of looping...
Objectives:
- Design and implement an algorithm using nested
loops.
- Learn to use both while and for loops.
Readings:
- Read Chapter 5 in the Hanly/Koffman text.
Deliverables: (DUE
BY THE END OF LAB!)
- Submittal of your algorithm and two files: lab5.c and Makefile.
- The algorithm needs to be provided in the form of the flowchart, which was introduced
during the lecture on Top-Down Design with
Functions. You can draw the symbols by hand, or use any type of software
with diagram-drawing capabilities (e.g. MS Visio,
or some free ones: OpenOffice.org
Draw, Dia).
Your program’s code needs to match the algorithm you
submitted.
TO DO (for today's
lab)
- “Palindrome” in our exercise is a string of
digits that can be read the same way in the either direction. Examples of
palindromes are: 01210,
or 0123210.
- Design a program to display palindrome.
- For instance, suppose that we have an input file with 5
on the first line. This is the output, we are expecting:
The Palindrome of 5 is:
0
0 1 0
0 1 2 1 0
0 1 2 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
- The version below (generated for an input file with 4
in it) indicates that you need to put one blank space between every two
numbers ( “b”
represents the blank space):
The Palindrome of 4 is:
0
0b1b0
0b1b2b1b0
0b1b2b3b2b1b0
0b1b2b3b4b3b2b1b0
- Write a C program solving that
problem.
- Use fscanf
to get your
palindrome size (an integer number) from your input file datain5.
- Use both the while and for statements, and
implement the solution using a nested loop.
- Note: you will lose 50% of your total if you use only one type of loop in your
program).
- You DO NOT need to provide your datain5 file since we can test
your program with our own files.
LAB 5 ENRICHMENT
- Rewrite your code using do-while loop (where condition
is checked at the end of the loop).
- Rewrite your code in such way that you do not use
neither “<=”
nor “>=”
relational operators (you are allowed to use “<” or some
other operators).
- Can you generate the same output, when using only
decremented counters (e.g. i
= i - 1)? How
about using only prefix decrements (e.g. --i)?
- Can you rewrite your program in such way that it would
print out palindromes for multiple numbers stored in the same input file?
Think about using endfile-controlled loop (with EOF) .