CS201
Lab 5 - Loops
Objectives:
- Use loops to read data from files and to perform incremental calculations.
- Learn how to declare and use 1D arrays.
Readings:
- Read Chapter 5 in the Hanly/Koffman text.
- Read Section 8.1-8.3 in the Hanly/Koffman text (you can finish on p. 376).
Deliverables: (DUE BY END OF LAB!)
- Submittal of your your Algorithm for this lab, lab5.c and Makefile
files.
TO DO (for today's lab)
- Design an algorithm to solve Programming Project #11
on page 277 of the Hanly/Koffman text.
- Write a program to implement your algorithm.
- Use fscanf to enter your base_size
height_size values. Do not prompt for these values, just read them in from
two data
files using
fscanf.
You need to declare base_size and height_size
as arrays of integers, where base_size should
be capable of storing 5 elements (of int
type), and height_size needs to have room for
6 elements (of int type). You will read the
values from two files stored in your current directory (the input files need
to be named datain5base and datain5height,
and stored in your current directory).
- Use a for-type loop, that counts forward, to
read data from datain5base file to base_size
array.
- Use a for-type loop, that counts backwards
(i.e. uses decrement steps), to read data from datain5height
file to height_size array. Make sure that
despite using decreasing counter (i--), your
still read and assign values to array in increasing order (i.e. first iteration of the
loop assigns the first value from the datain5height
file to the first element, i.e.: height_size[0]).
This one might be slightly tricky, so make sure you start and finish the loop in
the right moment.
- Use only while-type or do-while-type
loops to print out your output on the screen. Make sure to format output
properly so that they looks like as follows

- For your first input data file build a file with 5 lines. Store only one number
per line (assume that all numbers are integers). E.g.:
2
4
6
8
10
-
Call this file datain5base and save it to your
current (i.e. lab5) directory.
- For your second input data file, that provides values for height_size
array, you need to do almost the same. This time however the file needs
to store 6 numbers in 6 lines, and it needs to be named datain5height.
- You do not need to submit your sample data files. I will provide
a set of test data files for the automatic script to use to
test your program.
- Properly document your code and follow all code conventions discussed in the class.
- If you finish your assignment during the lab session, call your TA, hand in the printed algorithm and get checked off instantly.
Grading Scheme
Algorithm (Either in a separate text file or at the beginning of your source code as documentation) 10%
Program compile and execute as specified 30%
Program meets all specifications 50%
Program follow code conventions and contain proper documentation. 10%
No Enrichment this week