Title: Imperative Programming 2 CS4512
1Imperative Programming 2CS4512
http//www.ul.ie/nikolovn/cs4512
- Lecturer Nikóla S. Nikólov (Nik)
- Room CS1-031
- E-mail nikola.nikolov_at_ul.ie
- Phone 061-213579
2Lectures
Day Time Location
Thursday 1100 CSG01 Friday 1200 CSG01
Lecturenotes on their own are not enough, they
are just a guide please bring them to your labs
and tutorials!
3Textbooks (keywords C, Data Structures,
Problem solving)
- Nell Dale and David Teague (2001),C plus Data
Structures,2nd Edition, Jones and Bartlett,
ISBN 0763714704 - Walter Savitch (2001),Problem Solving with
C,3rd Edition, Addison Wesley, ISBN
0201703904 - Frank Carrano, Janet Prichard (2002), Data
Abstraction and Problem Solving with C,3rd
Edition, ISBN 0201741199 - Glenn Rowe (1996),Data Structures Algorithms
C,ISBN 0135-79178-2 - Mark Weiss (2000),Data Structures and Problem
Solving Using C,2nd Edition, ISBN 020161250X
4Tutorials (Mondays)TAs John Miller, Miguel
Nicolau and Nik Nikolov
Time Location Group 9-11 SG21 3A 9-11 SG20
3B 9-11 SG18 3C 11-13 SG21 3D 11-13 SG20
3E 11-13 SG18 3F 14-16 SG21 3G 14-16 SG20
3H
5Labs (Tuesdays)LAs Marion McKenna, Eoin Kennedy
and Paul Gallagher
Time Location Group 9-11 CS304 2BC 9-11 CS245
2H 11-13 CS304 2AE 11-13 CS245 2G 14-16 CS3
04 2DF
http//www.timetable.ul.ie
6Attendance
- It is of your benefit to attend all your
lectures. Attendance at lectures will be taken on
a random basis. - Labs and tutorials are compulsory.
- You are expected to attend a 2 hr. lab session
and a 2 hr. tutorial session on a weekly basis
(weeks 2 to 11 inclusive). - Should your attendance at labs and/or tutorials
be less than 90, you will incur a 5 penalty for
each session missed. - Any penalties are a gross deduction from your
final percentage.
7Assessment
- Pen Paper Test (week 4 lectures) 7
- Pen Paper Test (week 10 lectures) 7
- Project (due Thursday week 12) 11
- End of Semester Exam 75
- Section A multiple choice and short questions
36 - Section B questions related to the project
9 - Section C long questions (3 attempt 2)
30 -
- Repeat Exam 100
Consequences of Plagiarism - zero , possible
reporting to the disciplinary committee)
8Grading Scheme
Mark Grade QCA 80 A1 4.00
72 A2 3.60 64 B1 3.20 60 B2 3.00
56 B3 2.80 52 C1 2.60 48 C2 2.40
40 C3 2.00 38 D1 1.60 35 D2 1.20 0
F 0.00 NG 0.00 I incomplete
100
40
9Lecture 1C Input/Output
11110101110000
00010101111011
Two points of view to a program
- Output Data Program(Input Data)
- Program Model of a problem domain Execution
Simulation of the problem domain
10C program
11Using cin and cout
(( ) )
- cin, cout and cerr are variables, defined in
iostream - cin refers to the standard input stream cout
refers to the standard output stream and cerr
refers to the standard error (output) stream - The default action of the input operator gtgt is to
skip white space before reading the next input
item (even in the case of char) - The operators ltlt and gtgt associate from the left
- The value of the expression cin gtgt id refers to
the same input stream, after the vallue assigned
to id is removed from the stream.
12include ltiostreamgtusing namespace std int
main() int val, sum 0 cout ltlt "Enter
next number " while ( cin gtgt val ) sum
val cout ltlt "Enter next number "
cout ltlt "Sum of all values "ltltsumltlt'\n'
return 0
Example 2
Where a true or false value is expected, an
expression such as cin gtgt val is converted to
true, if a value is read into val, and false
otherwise
13Manipulators
- Used to manipulate the output, include
ltiomanipgt required. - Except for setw, a manipulator permanently
changes the state of the stream to which it is
applied. - setw sets the width of the output field.
- setfill is used to specify a particular fill
character, used to fill the extra space when the
field width is greater than the item to be
output. - setprecision is used to specify the number of
digits of precision of floating-point numbers.
cout ltlt setprecision(5) ltlt 12.48381344
12.484
14include ltiostreamgtinclude ltiomanipgt using
namespace std int main() float a 1.05, b
10.15, c 200.87 cout ltlt setfill( '' ) cout
ltlt setw( 10 ) ltlt a ltlt '\n' cout ltlt setw( 10 )
ltlt b ltlt '\n' cout ltlt setw( 10 ) ltlt c ltlt
'\n' return 0
Example 3
Output 1.05 10.15 200.87
15Exercise 1. Write a statement to set the fill
character on cout to the digit zero.
Exercise 2. Write a C program that takes as an
iput the first day of a month and the total
number of days in a month and outputs the
calendar
Mon Tue Wed Thu Fri Sat Sun 1
2 3 4 5 6 7 8 9 10 11
12 13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30 31
16Input from and Output to (Disk) Files
include ltfstreamgt
outfile
infile
ifstream infile infile.open(Asd.dat) infile
gtgt myvariable infile.close()
ofstream outfile outfile.open(Qwe.xyz) outfile
ltlt hello ltlt 5 outfile.close()
17Input from and Output to (Disk) Files (more)
Reading from a file in a while loop
ifstream infile ofstream outfile infile.open(in
come.dat) outfile.open(tax.out) while (
infile gtgt income ) if ( income lt cutoff )
tax rate1 income else tax rate2
income outfile ltlt Income ltlt income
ltlt euro\n ltlt Tax ltlt tax
ltlt euro\n infile.close() outfile.close()
2000.00 3500.00 6500.00 10000.00 2000.50 3500.00 8
500.00
income.dat
18Input from and Output to (Disk) Files (more)
Testing whether a file is open properly
ifstream infile infile.open ( scores.dat ) if
( !infile ) cerr ltlt Unable to open
scores.dat\n exit ( 0 )
What is cerr ?
The function exit requires include ltcstdlibgt
19Exercise 3. Modify the program from Excersise 3
to print out the calendar into a file called
calendar.dat.
Exercise 4. Let punts.dat be a file containing a
list of prices in Irish punts. Write a C
program that reads the prices from punts.dat,
converts them to euro and writes the euro prices
into file euro.dat with precision 2 digits after
the decimal point. (Assume 1 punt 1.27p).
19.99 2.00 4.95 1.50
25.39 2.54 6.29 1.90
punts.dat
euro.dat