total1

advertisement
1. Information Resource Pages
Welcome to the Software Engineering module for MSc Information Technology Students. These pages are
intended to complement the course and contain details of the course as well as some additional material.
Aim of Course

Develop programming skills

Introduce principles of software development

Develop an understanding of good professional practice in software engineering
Structure of Course

Introduction to Computing and Information Technology

C++ Programming

Software Engineering Principles

More on Unix operating systems
Teaching Details
This module should consist of approximately 100 hours of student effort. This will consist of 3 taught lectures
per week and 2 hours for computing labs when help will be available.
Note that you will be given exercises for each aspect of the programming which you are advised to attempt
before coming to the help sessions. This will enable you to gain the optimum usage of the help sessions.
One of the aims of this module is to gain experience on the use of UNIX workstations. However, there is no lab
of workstations which is large enough to accommodate this class. Therefore the lab sessions will use PCCaledonia in room 2.50/2.52 and eXceed to access UNIX workstations. At other times, you are encouraged to
use the UNIX workstations elsewhere in the department - e.g. the Linux Boxes in rooms G.46 & G.47.
Information is given here on using eXceed.
Information is given here on using Unix Workstations directly.
Information is given here on installing GCC Compiler on your PC at HOME.
You will be examined on this work as part of the MSc exam in April and you will have to submit an assignment
at the end of the course. This assignment will be a larger programming exercise combining the individual
elements of programming you have already been taught into a larger project. This assignment will be issued mid
term, at the end of the taught programming part of the course and the beginning of the software engineering
principles.
ASSIGNMENT
Feedback on Assignment
Introduction to Computing and Information Technology

Computers and operating systems

Computer Structures

News/Email

WWW

Word Processing / Text Formatting

Spreadsheets

Unix
C++ Programming
The outline of the programming part of the course is detailed below, and from the links on each of the topics you
can access copies of all the source code listed in the notes, some additional example programs and some
additional suggested exercises.
The source code given can be copied and compiled as detailed here.
Programs and Programming
Variables, Data Types, Constants and Operators
Control Constructs
Iteration
Arrays and Structures
Functions
Pointers
File Input and Output
Introduction to Classes
Software Engineering Principles

Software Development Models

Requiremnets analysis and specification

Design

Implementation

Validation and Verification

Organising software projects
More on Unix operating systems

Shell programming and shell scripts

Makefiles
Recommended Text
There is no specific recommended text. People tend to have different ideas about what they want from a text
book, therefore I would recommend that you look in the bookshop and see what you think of the books. Any
book on C++ programming, which starts from the very basics, should cover the C++ programming required for
the course. Note that some books may aasume that you have programmed in C before.
If you have NEVER programmed before, I personally would choose books such as:
Steve Oualline, "Practical C++ Programming", O'Reilly Associates
John Hubbard, "Programming with C++", Schaums Outline Series
NOTE: Full detailed notes will be provided at the lectures.
Announcements
Any announcements regarding the course will be detailed here.
If you have any queries please send email to me at jmb@cee.hw.ac.uk
NOTE: Clipart from http://www.signgray.demon.co.uk/clipart/
Disclaimer: As ever, these pages are still under construction, and bugs in the programs are an added bonus to
the learning experience! However, please email reports of any bugs to me.
Programs and Programming
Learning Objectives
Understand the terms source code and executable
Be able to use an editor to enter a source program into a file
Be able to compile the source code to produce the executable
Be able to run the program
Understand the basic layout of a simple program - including the keyword main, the use of the
semi-colon and the cout statement
Combine the above to be able to write simple programs to output text to the screen
Source Code from Notes
Additional Examples
Quick Check Exercises
Additional Exercises to try
Basic Programming
2. Source Code from Notes
Example
// Program written October 1997
// by jmb
#include <iostream.h
main()
{
cout <<< "Hello World!\n";
// \n causes new line
}
Basic Programming
3. Case Study
PROBLEM:
Write a program to display the text, This is my first program, on the screen.
SOLUTION:
The program must start with #include as the the program will output text to the screen.
The main body of the program is contained within curly braces which follow the keyword main().
The text is displayed on the screen using the cout statement. Note that this statement requires a semi--colon at
the end to indicate the end of the statement.
Comments have also been included to make the programme easier to understand.
// My first program
// Written by jmb
// October 1998
#include <iostream.h
main()
{
cout << "This is my first program \n";
// \n is required to produce newline
}
Basic Programming
4. Additional Exercises
1. Write a program to display your name, address and telephone number on the screen.
2. Correct the mistakes in the following program, type it in and compile the program.
// Example Program
Week 1
April 1997
//
mAin(
/ display data to screen
cout << "Example Program\n";
cout << 'for Introduction to Programming Class\n
cout " Autumn Term 1999/n";
cout " Illustrates the basic components of C++\n;
}
Programs
1. What is the purpose of #include <iostream.h ?
2. What is a source program? An executable?
3. Is a semi-colon required after a comment?
4. What is wrong with the following statement?
cout << Hello World\n;
5. What is wrong with the following statement?
COUT << "Hello World\n";
6. What is wrong with the following statement?
cout
"Hello World\n";
7. What is the purpose of the \n ?
8. What is wrong with the following statement?
cout << "Hello World"\n;
1. It includes the header file iostream.h into the source code. This file is required for input and output to the
program. Therefore, if the cout statement is used to output text/data to the screen it is required.
2. Source code is the actual C++ program which is typed into the file. This is then compiled into machine code
which can be executed by the computer.
3. No - a comment is not a program statement, so does not require to be terminated by a semi-colon.
4. The text to be printed must be within double quotes.
cout << "Hello World\n";
5. Keywords in C++ must be lowercase COUT must be written as cout
6. The output operator is << and not
7. The \n causes the a new line. It is required at the end of Hello WOrld to ensure the cursor returns to the next
line.
8. The \n (newline) is text to be printed and must be within the double quotes.
Variables, Data Types, Constants and Operators
Learning Objectives
Be able to declare variables
Be able to write simple arithmetic expressions
Be able to input values typed at the keyboard into a program using cin
Be able to output values to the screen using cout
Be familiar with the concept of constants
Be able to combine above to write simple programs which require the use to input values, make
calculations and output the results
Source Code from Notes
Additional Examples
Quick Check Exercises
Additional Exercises to try
Test Cases for Tutorial
Variables, Data Types, Constants and Operators
5. Source Code from Notes
Example 1
//
//
program to add two integer values
and display result
#include <iostream.h
main()
{
// declare variables
int
value1, value2, sum;
// assign values and compute the result
value1 = 32;
value2 = 27;
sum = value1 + value2;
// display the result
cout << "The sum of " << value1 << " and
sum << "\n" ;
<< value2
<< " is " <<
}
Example 2
This is the example program to calculate the area of a rectangle
#include <iostream.h
main()
{
// type definitions
// 3 variables - length, breadth and area
float
length, breadth, area;
// input phase - read length and breadth from user
cout << "Enter the length and breadth\n";
cin length breadth;
// computation - calculate area
area = length * breadth;
// output phase
cout << "The area of the rectangle with length " << length << " and
breadth " << breadth << " is " << area << "\n";
}
Example 3
This example calculates the average value of three numbers entered by the user.
// Program to calculate the average of three numbers
#include <iostream.h
main()
{
float
float
number1,number2,number3;
average;
// Enter values
cout << "Enter three numbers: ";
cin number1 number2 number3;
// calculate average
average = (number1 + number2 + number3)/3.0;
// Output result
cout << "The average value of " << number1 << " , " << number2 <<
" and " << number3 << " is " << average << "\n";
}
Example 4
This example prompts the user to input a number and then calculates the reciprocal of this number.
// Program to calculate the reciprocal of a number
#include <iostream.h
main()
{
float
float
number;
reciprocal;
// Enter Value
cout <<"Enter a number: ";
cin number;
// Calculate reciprocal
reciprocal = 1.0 / number;
// output result
cout << "The reciprocal of " << number << " is " << reciprocal << "\n";
}
Example 5
This example illustrates the use of constants. It calculates the area and circumference of a circle, when given the
radius.
// program to calculate circumference and area of circle
#include <iostream.h
main()
{
const float PI = 3.14156;
// define variables
float
radius,circumference,area;
// Input radius
cout << "Enter the radius: ";
cin radius;
// calculate radius and circumference
circumference = PI * 2 * radius;
area = PI * radius * radius;
// display results
cout << "The circumference is " << circumference << " and the rea is " <<
area
<< "\n";
}
Variables, Data Types, Constants and Operators
6. Case Study
PROBLEM:
Write a program to calculate an employees wages. They are paid at a basic rate of £6.65 hour, but are
taxed at a rate of 23% and pay a pension contribution of 4% and pay a union subscription of 1.25 per week.
Write a program to read in the hours worked and calculate the take home weekly wages of the employee.
SOLUTION:
The structure of the program will take the following form:#include <iostream.h
// Declare constants
main()
{
// declare variables
// prompt user to enter number of hours worked and read in value
// Calculate wages
// output weekly wages
}
Constants
We are told that the employee is paid 6.65 per hour is taxed at a rate of 23%, pays 4% pension contributions and
a union fee of 1.25. We should make these values be constants - since they have been given to use. In addition,
this makes it easier to change these values at a later date and if we use meaningful names will make the program
easier to read. Therefore we will declare:const
const
const
const
float
float
float
float
hourly_rate = 6.65;
tax_rate
= 0.23;
pension_rate = 0.04;
union_fee
= 1.25;
I have converted the percentages to floating point values, since for example 23% = 23/100 * value.
Variables
We are obviously going to require a variable to store the number of hours worked, which we will assume can be
a fractional number of hours and therefore use a floating point value. We will also use variables for the
gross_pay, pension, tax, deductions and total_pay. All of which will be floating point values.
float
hours, gross_pay, tax, pension, deductions, total_pay;
Input
We require to prompt the user to enter the number of hours worked and then read this into the variable hours .
cout << "Enter the number of hours worked \n";
cin hours;
Calculation
There are a number of different ways this could be implemented. I have chosen here to do it in a number of
different stages. Calculating initially the gross wages and then the deductions.
gross_pay = hours * hourly_rate;
tax = gross_pay * tax_rate;
pension = gross_pay * pension_rate;
deductions = tax + pension + union_fee;
total_pay = gross_pay - deductions;
Output
Finally we have to output the result. This can be achieved by the statement
cout << "The total weekly pay is " << total_pay << "\n";
Total Program
#include <iostream.h
main()
{
// Declare constants
const float hourly_rate = 6.65;
const float tax_rate
= 0.23;
const float pension_rate = 0.04;
const float union_fee
= 1.25;
//declare variables
float
hours, gross_pay, tax, pension, deductions, total_pay;
//prompt user to enter number of hours worked and read in value
cout << "Enter the number of hours worked \n";
cin hours;
// Calculate wages
gross_pay = hours * hourly_rate;
tax = gross_pay * tax_rate;
pension = gross_pay * pension_rate;
deductions = tax + pension + union_fee;
total_pay = gross_pay - deductions;
// output weekly wages
cout << "The total weekly pay is " << total_pay << "\n";
}
Variables, Data Types, Constants and Operators
7. Additional Exercises
1. Write a program which prompts the user to enter 2 integer values. The program will then print their sum,
difference, product, quotient and remainder.
2. Write a program which will prompt the user to enter a positive floating point number. The program will then
display the integral part and the fractional part of the number.
For example:number = 2.33
integral = 2
fractional = 0.33
3. Write a program which prompts the user to enter a number of days, hours and minutes. The program will
then calculate and display this as a total number of minutes.
For example
days = 1
hours = 12
minutes = 15
Total number of minutes = 2175
Variables, Data Types, Constants and Operators
1. What is wrong with this program?
main()
{
p=47;
cout << p;
}
2. What is wrong with this declaration?
int x = y = 95;
3. Write four different C++ statements that subtract 1 from the integer variable x.
4. Which of the following are valid C++ statements? Evaluate the ones which are valid.

x = 65/5*2

65 / 4 = k;

p = 65(4*7);

x = 34 - 4 * 7;

x = 34 - ( 4 * 7);

54 % 5 = h ;

x = 54 % 7;
1. The #include <iostream.h is missing and the variable p has not been declared.
2. The equals sign can only be used in a declaration to initialise a variable. Therefore it should be
int
x=95, y =95;
x = x - 1;
x --;
--x;
x -= 1;

missing ;

not valid - should be k = 64/4;

not valid

valid x = 6

valid

not valid

valid x = 5
Control Constructs
Learning Objectives
Know the relational and logical operators
Be able to use the if statement
Be able to use the if....else statement
Be able to use the else ... if statement to implement multi-way branching
Know when it is suitable to use the switch statement for multi-way branching and be able to
implement it
Source Code from Notes
Additional Examples
Quick Check Exercises
Additional Exercises to try
Test Cases for Tutorials
Control Constructs
8. Source Code from Notes
Example 1
//Program to calculate absolute value of an integer
#include <iostream.h
main()
{
int
number;
//Enter number
cout << "Enter a number: ";
cin number;
//test if negative
if (number < 0)
number = -number;
//output results
cout << "The absolute value is " << number << "\n";
}
Example 2
//Program to test if number is even or odd
#include <iostream.h
main()
{
int
number,remainder;
cout << "Enter a number: ";
cin number;
remainder = number % 2;
if (remainder ==0)
cout << "The number is even\n";
else
cout << "The number is odd\n";
}
Example 3
//Program to illustrate the use of nested if statements
#include <iostream.h
main()
{
int number;
cout << "Enter a number between 1 and 99: ";
cin number;
if (number 0 && number < 100 )
{
if (number < 10)
cout << "One digit number\n";
else
cout << "Two digit number\n";
}
else
cout << "Number not in range\n";
}
Example 4
#include <iostream.h
main()
{
int number, n_digits = 0;
cout << "Enter a number: ";
cin number;
if (number = 100)
n_digits = 3;
else if (number =10)
n_digits = 2;
else if (number =0)
n_digits = 1;
else
cout << "Value out of range\n";
if (n_digits 0)
cout << "The number has " << n_digits << " digits\n";
}
Example 5
#include <iostream.h
main()
{
char
day;
cout << "Which day?";
cin day
switch(day){
case 's': cout <<
break;
case 'm': cout <<
break;
case 't': cout <<
break;
case 'w': cout <<
break;
case 'f': cout <<
break;
default: cout <<
}
"Weekend\n";
"Week day\n";
"Week day\n";
"Week day\n";
"Week day\n";
"Not a day\n";
}
Example 6
#include <iostream.h
main()
{
char
day;
cout << "Which day?";
cin day;
switch(day){
case 's': case 'S':
break;
case 'm': case 'M':
break;
case 't': case 'T':
break;
case 'w': case 'W':
break;
case 'f': case 'F':
break;
default:
cout << "Week day\n";
cout << "Week day\n";
cout << "Week day\n";
cout << "Week day\n";
cout << "Not a day\n";
}
}
Example 7
#include <iostream.h
main()
{
int
cout << "Weekend\n";
input_value;
cout << "Which day?";
cin input_value;
switch (input_value){
case 1: cout << "The day is Monday\n";
break;
case 2: cout << "The day is Tuesday\n";
break;
case 3: cout << "The day is Wednesday\n";
break;
case 4: cout << "The day is Thursday\n";
break;
case 5: cout << "The day is Friday\n";
break;
case 6: cout << "The day is Saturday\n";
break;
case 7: cout << "The day is Sunday\n";
break;
default : cout << "Invalid Input\n";
break;
}
}
Control Constructs
9. Case Study
PROBLEM:
Write a program to calculate the commission based wages of a computer salesman. His basic wage is £50
per week and he is expected to sell at least 10 computers. If he sells more than 10 computers, he receives an
extra £5.50 per computer he sells after the 10th computer.
SOLUTION:
The basic algorithm could be drawn as:-
The structure of the program would then be:DECLARE CONSTANTS
DECLARE VARIABLES
PROMPT USER TO ENTER NUMBER OF COMPUTERS SOLD
READ IN NUMBER
CALCULATE PAY
IF SELLS MORE THAN 10
CALCULATE COMMISSION
OUTPUT PAYMENT
adding the actual code to the above:#include &ltiostream.h
main()
{
//DECALRE CONSTANTS
const float BASIC_PAY = 50;
const int LEVEL = 10;
const float COMMISSION = 5.50;
//DECLARE VARIABLES
float pay, commission_pay;
int num_computers;
//PROMPT USER TO ENTER NUMBER OF COMPUTERS SOLD
cout << "Please enter number of computers sold: \en";
//READ IN NUMBER
cin num_computers;
//CALCULATE PAY
if (num_computers level)
{
commission_pay = (num_computers - LEVEL)* COMMISSION;
pay = BASIC_PAY + commission_pay;
}
else
pay = BASIC_PAY ;
//OUTPUT PAYMENT
cout << "Your wage for this week is " << pay << " pounds\en";
}
NOTE that this is only one possible solution to the problem.
Try to think of some alternative methods of solving this problem.
Control Constructs
10.Additional Exercises
1. An employee in a company is paid at a rate of £5.20 per hour. The standard rate is paid for up to
37 hours worked in a week, after which 1.5 times the standard rate is paid. 22% tax is deducted from the
gross pay as well as 5% for national insurance and 6% for the company pension scheme. If the employee
works less than 20 hours a week, the income tax rate is only 20%.
Write a program to read in the hours worked and calculate the employees pay, displaying the gross
salary and all the deductions as well as the net take home pay.
Remember to make proper use of constants.
2. A leap year is a year which is exactly divisible by 4, unless it is exactly divisible by 100 in which case it
is only a leap year if it is exactly divisible by 400. Write a program which prompts the user for a year
and then displays if it is a leap year or not.
Control Constructs
1. What is wrong with the following?
int x;
if (x=1)
x++;
2. What is wrong with the following?
int x, y, z;
if (x
y && < z)
x = y;
3. Write an extract of code to test if the variable x is a positive number?
4. Write an extract to test if the variable x is a positive and even number?
1. The test should be x== 1, with x=1 this will assign the value of 1 to x rather than checking if x is a 1.
2. Assuming that it is intended to compare z to z, the test should be x y && x < z
3. if (x >
0)
4. if (x > 0 && x%2 == 0)
Back to Questions
Back to Main Page for Lecture 3
Iteration
Learning Objectives
Be able to use a while loop
Be able to use a do..while loop
Be able to use a for loop
Know how to change from one type of loop to another
Know when one form of loop is more suitable than another i.e. would not use for loop when checking if
input to program is within desired range.
Source Code from Notes
Additional Examples
Quick Check Exercises
Additional Exercises to try
Previous Lecture
Home
Next Lecture
Iteration
Source Code from Notes
Example 1
This example reverses the digits of a
number, and prints out the number in the
reverse order. Each digit is displayed as it
is extracted by the program. The final call
to \fC\s8printf\fR\s0 returns the cursor to
the next line as the newline character has
not been included within the loop.
#include <iostream.h
main()
{
int
number,
right_digit;
cout << "Enter
number\n";
cin number;
your
while (number !=0)
{
right_digit
=
number % 10;
cout
right_digit;
number /= 10;
}
cout << "\n";
}
Example 2
This example checks that the input value is
in the desired range
#include <iostream.h
main()
{
int
number;
const int min = 1;
const int max = 100;
number = 0;
while ( number < min ||
max)
{
cout << "Enter a
number in the range 1 to 100\n";
cin number;
}
cout << "the number is
in range\n";
}
number
Example 3
#include <iostream.h
main()
{
char
cout
continue\n";
letter;
<<
"Press
Y
to
do
{
letter
=
getchar();
} while (letter != 'y'
&& letter != 'Y');
cout
<<
"program
execution will continue\n";
}
Example 4
The following program creates a
conversion table for converting inches to
centimetre. The values in the table are
calculated for inches in the range 1 to 10 in
half inch increments.
#include <iostream.h
main()
{
float
inches,centimetres;
// print table header
cout
<<
"inches\t
centimetres\n";
//calculate and print
table entries
for (inches = 1; inches
<=10 ; inches += 0.5)
{
centimetres
=
2.54 * inches;
cout << inches
<< centimetres << "\n";
}
}
Back to Main Page for Lecture 4
Iteration
Case Study
PROBLEM:
Write a program to calculate the average
marks for a class of students. The program
will initially prompt for the number of
students to be entered and then for each
student will ask for their marks for each of
the 4 exams the student sat. The program
will then calculate the average mark for
each student and the average mark for the
whole class.
SOLUTION:
The solution to this will take the form:-
where the program repeats for each student
the code to enter the student's marks and
calculate their average.
This could be implemented using a while
loop to produce
//Using While Loop
#include <iostream.h
main()
{
int
num_students,
count;
float
mark3, mark4;
mark1,
mark2,
float
student_average,
class_total = 0, class_average;
// Enter no. of students
cout << "Please enter
the number of students in the
class: ";
cin num_students;
// set count to zero
count = 0;
while
(count
<
num_students)
{
// enter 4 marks
for each student
cout << "Please
enter the marks for student "
<< count <<
"\n";
cin
mark1
mark2 mark3 mark4;
//
Calculate
average
= (mark1 +
mark4) / 4;
student_average
mark2 + mark3 +
//
Add
average
to total
class_total
+=
student_average;
//
Display
average
cout << "The
average mark for student " <<
count
<< " is "
<< student_average << "\n\n";
//
Increment
count for next student
count ++;
}
//
Calculate
class
average
class_average
class_total / num_students;
=
// Output Class average
cout
<<
"The
class
average is " << class_average <<
"\n";
}
When this program is run it produces the
following output ( with sample figure
inserted):Please
enter
the
number
of
students in the class: 4
Please
enter
the
marks
for
student 0
23 45 63 54
The average mark for student 0
is 46.25
Please
enter
the
marks
for
student 1
67 87 67 87
The average mark for student 1
is 77
Please
enter
the
marks
for
student 2
54 65 34 76
The average mark for student 2
is 57.25
Please
enter
the
marks
for
student 3
34 35 36 25
The average mark for student 3
is 32.5
The class average is 53.25
The program can be drawn schematically
to show how it fits in to the flow structure
given above. The code used for each part is
shown
alongside.
The same program could be converted to a
for and only the initialisation, loop
statement and increment need to be
changed. Here is the same program but
written with the for loop.
//Using For Loop
#include <iostream.h
main()
{
int
num_students,
count;
float
mark1,
mark2,
mark3, mark4;
float
student_average,
class_total = 0, class_average;
// Enter no. of students
cout << "Please enter
the number of students in the
class: ";
cin num_students;
//
for
statement
includes
initialisation,
test
and increment
for (count = 0 ; count <
num_students ; count ++)
{
// enter 4 marks
for each student
cout << "Please
enter the marks for student "
<< count <<
"\n";
cin
mark1
mark2 mark3 mark4;
//
Calculate
average
= (mark1 +
mark4) / 4;
student_average
mark2 + mark3 +
//
Add
average
to total
class_total
+=
student_average;
//
Display
average
cout << "The
average mark for student " <<
count
<< " is "
<< student_average << "\n\n";
}
//
Calculate
class
average
class_average
class_total / num_students;
=
// Output Class average
cout
<<
"The
class
average is " << class_average <<
"\n";
}
Back to Main Page for Lecture 4
Iteration
Additional Exercises
5. In the train time table program,
add loops to check that the
times entered are valid (ie
times entered are between 0
and 24 hours for the start and
stop times and that the
frequency and journey times
are between 1 and 60 minutes).
Prevent the program from
proceeding
until
suitable
values are entered.
6. Write a program which reads
in a set of positive value real
number values terminated by a
negative value and prints out
the sum of all these values.
7. Write a program to find the
largest, smallest and average
values in a collection of n
numbers entered by the user
where n is the first data item
entered.
Back to Main Page for Lecture 4
Iteration
8. Change to following code to a
while loop.
int
x, y;
for (x=1; x < 57; x+=
2)
y = x;
9. What is wrong
following?
with
the
int
n=0;
while (n < 100)
value = n*n;
10. Write a while loop that
calculates the sum of all
numbers between 0 and 20
inclusive?
11. Convert the above loop to a do
... while loop.
12. Convert the above loop to a
for loop.
13. What is wrong
following?
i = 1;
while
)
(i
with
<=
cout << i;
i++;
Answers
the
10
x = 1;
while (x < 57)
{
y = x;
x += 2;
}
14. The value of n within the loop
is never incremented. It will
therefore always have a value
of 0, which is less than 100,
and hence this will produce an
infinite loop which will never
terminate. The program will
therefore never end.
int
i, sum =0;
i = 0;
while (i <= 100)
{
sum += i;
i++;
}
int
i, sum =0;
i = 0;
do
{
sum += i;
i++;
}while (i <= 20);
int
i, sum =0;
for (i=0; i <= 20; i++)
{
sum += i;
}
15. There are no braces around the
cout
and
increment
i
statements, therefore it will
execute
only
the
cout
statement within the loop, and
an infinite loop will again
occur.
Back to Questions
Back to Main Page for Lecture 4
Arrays
and
Structu
res
Learning Objectives
Be able to declare an array
Be able to access elements of an array
Be able to sequentially access arrays using loops
Be able to initialise arrays
Understand the use of structures
Be able to declare a structure and access the members of a structure variable
Source Code from Notes
Additional Examples
Quick Check Exercises
Additional Exercises to try
Previous Lecture
Home
Next Lecture
Arrays
and
Structu
res
Source Code from
Notes
Example
Program to input
each
of
the
temperatures over
the 30 days to
calculate
the
average
temperature during
the period.
// Program to
input
30
temperature
samples
and
calculate
their average
#include
<iostream.h
main()
{
const
int DURATION =
5;
int
day;
float
temperature[DU
RATION];
float
average,
total;
//Enter
temperature
values
for
(day =0; day <
DURATION
;day++)
{
cout
<<
"Enter
temperature of
day " << day
<< "\n";
cin
temperature[da
y];
}
//
initialise
total
variable
total
= 0.0;
// sum
temperatures
for each day
for
(day=0; day <
DURATION;
day++)
{
total
+=
temperature[da
y];
}
//
calculate
average
average
=
total/DURATION
;
//output
result
cout
<<
"The
average
temperature
is"
<<
average
<<
"\n";
}
Example
The
following
example reads in a
sentence which is
terminated by a
full
stop
and
calculates
the
number of lower
case vowels in the
sentence. An array
of char elements
is defined. The
length of this array
is limited to 100
elements.
// Program to
count
for
number
of
vowels
in
a
sentence
//
the
sentence must
be terminated
by a full stop
(.)
#include
<iostream.h
main()
{
const
int max_length
= 100; //max
number
of
characters in
sentence
char
sentence[max_l
ength];
int
i,count,length
;
i = 0;
count
= 0;
enter
sentence
//
the
cout
<<
"Enter
a
sentence
terminated by
a
full
stop
\n";
do {
cin
sentence[i];
i++;
}
while(sentence
[i-1] != '.'
&&
i
<
max_length);
//
number
of
characters in
sentence
length
= i;
//
count
number
of lower case
vowels
for
(i=0;i
<
length;i++) {
if
(sentence[i]
==
'a'
||
sentence[i] ==
'e'
||
sentence[i]
== 'i' ||
sentence[i] ==
'o'
||
sentence[i]
=='u') {
count++;
}
}
//
output number
of vowels
cout
<< "The number
of lower case
vowels is " <<
count << "\n";
}
Example
Two dimensional
array
example
where the average
temperature each
month for the last
20
years
is
declared and the
user
is
then
prompted to enter
the values
#include
<iostream.h
main()
{
const
int NO_MONTHS
= 12;
const
int NO_YEARS =
20;
float
temperature[NO
_YEARS][NO_MON
THS];
int
years,month;
//
input
values
to array
for
(years
=
0;years
<
NO_YEARS;years
++)
{
for
(month=0;month
<
NO_MONTHS;mont
h++)
{
cout
<<
"Enter
temperature ";
cin
temperature[ye
ars][month];
}
}
}
Back to Main Page for Lecture 5
Arrays
and
Structu
res
Additional
Examples
PROBLEM:
Write a program
which will read in
a sequence of
positive integers
terminated by -1 to
indicate the end of
the input. These
values will be read
into an array and
the
program
should
then
determine if the
sequence is a
palindrome.
A palindrome is a
sequence
of
number/characters/
words etc which is
the same when
read from either
direction.
For
example:
2345432
is a palindrome,
since the sequence
is the same when
read from left to
right or from right
to left.
2345
Is
NOT
palindrome.
a
SOLUTION:
The program does
not know how big
a sequence will be
entered so an array
of
characters
larger
than
required
is
declared.
The
program
then
reads
in
the
sequence
of
integers one at a
time into the array.
After it has read in
each integer it
checks if it was a 1, if it was -1 it
stops reading in. It
also checks to
ensure that we
can't read in any
more integers than
the size of our
array.
Once
it
has
completed reading
in the integers, the
number of values
read in can be
determined, as i
- 1 since we are
not interested in
the -1.
We
can
then
search through the
array comparing
the 1st element
with the last, the
2nd element with
the 2nd last etc. As
soon as we find
that they don't
match we can
terminate
the
search as we know
that the sequence
is
not
a
palindrome.
Note that we only
have to loop for i
< length/2 as we
compare the first
half of the array to
the second half.
#include
<iostream.h
main()
{
int
100;
int
const
SIZE
=
array[SIZE];
int
i=0,
length,
palindrome
=
1;
cout
<<
"Enter
A
sequence
of
positive
integer
numbers\n";
cout
<< "terminated
by -1\n";
do
{
cin
array[i]);
i++;
}while(array[i
-1] != -1 && i
< SIZE);
length
=
i
-1;
//don't need 1
(i=0;
i
length/2
palindrome
1 ;i++)
{
for
<
&&
==
if (array[i]
!=
array[length i - 1])
palindrome
0;
}
=
if
(palindrome ==
1)
cout
<<
"array
is
a
palindrome\n";
else
cout
<<
"array is not
a
palindrome\n";
}
Back to Main Page for Lecture 5
Arrays
and
Structu
res
Additional
Exercises
1. Write
a
progra
m to
read N
integer
s into
each
of two
arrays
X and
Y of
size
20.
The
progra
m will
promp
t the
user to
enter
the
values
into
each
of the
arrays.
The
progra
m will
then
compa
re
each
of the
eleme
nts of
X to
the
corres
pondin
g
eleme
nt in
Y.
Then,
in the
corres
pondin
g
eleme
nt of a
third
array
Z,
store:1
if
X
is
lar
ge
r
th
an
Y
0
if
X
is
eq
ua
l
to
Y
-1
if
X
is
les
s
th
an
Y
Then
print
out a
three
colum
n table
displa
ying
the
conten
ts of
the
arrays
X, Y
and Z,
follow
ed by
a
count
of the
numbe
r
of
eleme
nts of
X that
exceed
Y, and
a
count
of the
numbe
r
of
eleme
nts of
X that
are
less
than
Y.
Make
up
your
own
test
data.
Declar
e your
arrays
to be
of
a
size
greater
than
you
will
requir
e,
(larger
than
N).
2. Define
a
structu
re to
store
the x
and y
coordi
nates
of
a
point.
The
user
should
then
be
promp
ted to
enter
the x
and y
coordi
nates
for
two
points,
and
the
progra
m
should
make
use of
the
structu
re.
The
progra
m will
then
calcul
ate the
distan
ce on
the x
axis
betwe
en the
two
points
and
the
distan
ce on
the y
axis
betwe
en the
two
points.
Back to Main Page for Lecture 5
Array
s and
Struct
ures
3. Write
a
declar
ation
for an
array
of 100
floats.
4. Initiali
se all
eleme
nts in
this
array
to
have a
value
of 0
5. Assign
a
value
of
10.5 to
the 5th
eleme
nt in
the
array
you
have
just
declar
ed.
6. What
is
wrong
with
the
follow
ing?
int
array
[10],
i;
for
(i=0;
i
<
10;i+
+)
array
[i] =
array
[i+1]
;
7. What
is
wrong
with
the
follow
ing?
float
index
;
float
array
[9];
for
(inde
x=0;
index
< 9;
index
++)
array
[inde
x] =
index
;
Answers
8. float
a[100
];
int
i;
for
(i=0;
i<100
;i++)
a[i]
=
0.0;
9. a[4]=
10.5;
10. The
array
has 10
eleme
nts,wit
h
indexe
s from
0 to 9,
but the
last
time
round
the
loop i
= 9,
and
hence
the
code
tries to
do
array
[9] =
array
[10],
which
would
result
in an
error
since
a[10]
does
not
exist.
11. The
array
index
must
always
be an
integer
and
hence
the
first
line
should
have
been
int
index
;
Back to Questions
Back to Main Page for Lecture 5
F
u
n
c
t
i
o
n
s
Learning Objectives
Understand the use of the function definition and declaration
Be able to write a function definition given a specification
Be able to call a function
Understand the use of void
Be able to use standard functions
Understand the scope of variables in functions
Understand the use of recursive functions
Understand the use of refernce variables and be able to use them to alter values sent to a function
Understand the concept of Default Arguments and be able to use them
Understand the concept of function overloading
Source Code from Notes
Additional Examples
Quick Check Exercises
Additional Exercises to try
Test Cases for Tutorial 6
Extra Examples on Recursion
Previous Lecture
Next Lecture
Home
F
u
n
c
t
i
o
n
s
So
ur
ce
C
od
e
fr
o
m
N
ot
es
E
x
a
m
p
l
e
1
Th
e
fol
lo
wi
ng
pr
og
ra
m
us
es
a
fu
nc
tio
n
to
ca
lc
ul
at
e
th
e
vo
lu
m
e
of
th
e
cu
be
wi
th
th
e
lar
ge
st
vo
lu
m
e
fr
o
m
th
e
di
m
en
si
on
s
en
ter
ed
by
th
e
us
er
fo
r
tw
o
cu
be
s.
#i
nc
lu
de
<i
os
tr
ea
m.
h
//
fu
nc
ti
on
de
cl
ar
at
io
n
fl
oa
t
cu
be
_v
ol
um
e(
fl
oa
t
le
ng
th
,
fl
oa
t
he
ig
ht
,
fl
oa
t
wi
dt
h)
;
//
ma
in
bo
dy
of
pr
og
ra
m
ma
in
()
{
fl
oa
t
le
ng
th
1,
wi
dt
h1
,h
ei
gh
t1
,v
ol
um
e1
;
fl
oa
t
le
ng
th
2,
wi
dt
h2
,h
ei
gh
t2
,v
ol
um
e2
;
//
in
pu
t
cu
be
di
me
ns
io
ns
co
ut
<<
"E
nt
er
di
me
ns
io
ns
of
cu
be
1\
n"
;
ci
n
le
ng
th
1
he
ig
ht
1
wi
dt
h1
;
co
ut
<<
"E
nt
er
di
me
ns
io
ns
of
cu
be
2\
n"
;
ci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ar
t_
mi
n,
st
op
_m
in
;
in
t
jo
ur
ne
y_
ti
me
,
fr
eq
_t
im
e;
in
t
de
p_
re
d,
de
p_
gr
ee
n,
de
p_
ye
ll
ow
,a
rr
_b
lu
e;
in
t
de
p_
re
d_
mi
n,
de
p_
gr
ee
n_
mi
n,
de
p_
ye
ll
ow
_m
in
,a
rr
_b
lu
e_
mi
n;
co
ut
<<
"E
nt
er
th
e
jo
ur
ne
y
ti
me
(m
in
ut
es
)\
n"
;
ci
n
jo
ur
ne
y_
ti
me
;
co
ut
<<
"E
nt
er
th
e
fi
rs
t
tr
ai
n
ti
me
\n
";
ci
n
st
ar
t;
co
ut
<<
"E
nt
er
th
e
la
st
tr
ai
n
ti
me
\n
";
ci
n
st
op
;
co
ut
<<
"E
nt
er
th
e
fr
eq
ue
nc
y
ti
me
\n
";
ci
n
fr
eq
_t
im
e;
//
co
nv
er
t
st
ar
t
an
d
st
op
ti
me
s
to
mi
nu
te
s
st
ar
t_
mi
n
=
to
_m
in
s(
st
ar
t)
;
st
op
_m
in
=
to
_m
in
s(
st
op
);
//
ta
bl
e
he
ad
er
co
ut
<<
"R
ED
\t
GR
EE
N
\t
YE
LL
OW
\t
BL
UE
\n
";
fo
r
(t
im
e=
st
ar
t_
mi
n;
ti
me
<=
st
op
_m
in
;
ti
me
+=
fr
eq
_t
im
e)
{
//
Ca
lc
ul
at
e
Ti
me
s
de
p_
re
d_
mi
n
=
ti
me
;
de
p_
gr
ee
n_
mi
n
=
(t
im
e
+
jo
ur
ne
y_
ti
me
+
wa
it
);
de
p_
ye
ll
ow
_m
in
=
(t
im
e
+
2*
(j
ou
rn
ey
_t
im
e
+
wa
it
))
;
ar
r_
bl
ue
_m
in
=
ti
me
+
3*
jo
ur
ne
y_
ti
me
+2
*w
ai
t;
//
Co
nv
er
t
ti
me
s
to
24
ho
ur
cl
oc
k
to
di
sp
la
y
de
p_
re
d
=
to
_2
4h
r(
de
p_
re
d_
mi
n)
;
de
p_
gr
ee
n
=
to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
n1)
;
re
tu
rn
(r
es
ul
t)
;
}
E
x
a
m
p
l
e
Th
e
fol
lo
wi
ng
pr
og
ra
m
pr
o
m
pt
s
th
e
us
er
fo
r
th
e
co
or
di
na
tes
of
2
po
int
s.
It
th
en
ca
lc
ul
at
es
th
e
le
ng
th
of
th
e
lin
e
joi
ni
ng
th
e
tw
o
po
int
s.
#i
nc
lu
de
<m
at
h.
h
#i
nc
lu
de
<i
os
tr
ea
m.
h
st
ru
ct
co
or
d
{
fl
oa
t
x,
y,
z;
};
//
fu
nc
ti
on
de
cl
ar
at
io
ns
fl
oa
t
le
ng
th
(c
oo
rd
po
in
t1
,
co
or
d
po
in
t2
);
fl
oa
t
sq
ua
re
(f
lo
at
x)
;
vo
id
ma
in
(v
oi
d)
{
co
or
d
p1
,
p2
;
fl
oa
t
li
ne
_l
en
gt
h;
co
ut
<<
"E
nt
er
co
or
ds
of
po
in
t
1
:
";
ci
n
p1
.x
p1
.y
p1
.z
;
co
ut
<<
"E
nt
er
co
or
ds
of
po
in
t
2
:
";
ci
n
p2
.x
p2
.y
p2
.z
;
li
ne
_l
en
gt
h
=
le
ng
th
(p
1,
p2
);
co
ut
<<
"L
en
gt
h
=
"
<<
li
ne
_l
en
gt
h
<<
"\
n"
;
}
fl
oa
t
le
ng
th
(c
oo
rd
po
in
t1
,
co
or
d
po
in
t2
)
{
fl
oa
t
xl
en
,y
le
n,
zl
en
;
fl
oa
t
le
n_
sq
r;
xl
en
=
po
in
t1
.x
po
in
t2
.x
;
yl
en
=
po
in
t1
.y
po
in
t2
.y
;
zl
en
=
po
in
t1
.z
po
in
t2
.z
;
le
n_
sq
r
=
sq
ua
re
(x
le
n)
+
sq
ua
re
(y
le
n)
+
sq
ua
re
(z
le
n)
;
re
tu
rn
(s
qr
t(
le
n_
sq
r)
);
}
fl
oa
t
sq
ua
re
(f
lo
at
x)
{
re
tu
rn
((
x)
*(
x)
);
}
E
x
a
m
p
l
e
R
e
f
e
r
e
n
c
e
V
a
r
i
a
b
l
e
s
Th
is
ex
a
m
pl
e
ill
us
tra
tes
ho
w
ref
er
en
ce
va
ria
bl
es
ca
n
be
us
ed
to
pe
rm
it
a
fu
nc
tio
n
to
ch
an
ge
va
lu
es
in
th
e
ca
lli
ng
pr
og
ra
m.
#i
nc
lu
de
<i
os
tr
ea
m.
h
vo
id
sw
ap
(i
nt
&r
a,
in
t
&r
b)
;
ma
in
()
{
in
t
a,
b;
a
=
10
;
b
=
12
;
sw
ap
(a
,b
);
}
vo
id
sw
ap
(i
nt
&r
a,
in
t
&r
b)
{
in
t
te
mp
;
te
mp
=
ra
;
ra
=
rb
;
rb
=
te
mp
;
}
E
x
a
m
p
l
e
Th
is
ex
a
m
pl
e
se
nd
s
an
arr
ay
to
th
e
fu
nc
tio
n.
Th
e
fu
nc
tio
n
th
en
ca
lc
ul
at
es
bo
th
th
e
lar
ge
st
an
d
s
m
all
est
va
lu
e
st
or
ed
in
th
e
arr
ay
.
Th
es
e
m
ax
im
u
m
an
d
mi
ni
m
u
m
va
lu
es
st
or
ed
ar
e
th
en
ret
ur
ne
d
to
th
e
m
ai
n
ca
lli
ng
pr
og
ra
m
as
fu
nc
tio
n
ar
gu
m
en
ts.
Th
er
e
is
a
m
sit
ak
e
in
th
e
no
tes
,
th
e
fol
lo
wi
ng
pr
og
ra
m
is
co
rre
ct
an
d
ha
s
m
ad
e
M
A
X
be
gl
ob
al
so
th
e
fu
nc
tio
n
als
o
kn
o
ws
its
va
lu
e.
A
n
alt
er
na
tiv
e
so
lut
io
n
is
gi
ve
n
be
lo
w
w
he
re
M
A
X
is
lo
ca
l
an
d
is
se
nt
to
th
e
fu
nc
tio
n.
#i
nc
lu
de
<i
os
tr
ea
m.
h
#i
nc
lu
de
<m
at
h.
h
vo
id
ma
x_
mi
n(
fl
oa
t
&m
ax
,
fl
oa
t
&m
in
,
fl
oa
t
ar
ra
y[
])
;
co
ns
t
in
t
MA
X
=
5;
ma
in
()
{
fl
oa
t
ar
ra
y1
[M
AX
];
f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
fr
o
m
a
m
ai
n
ca
lli
ng
pr
og
ra
m
w
he
re
th
e
us
er
is
pr
o
m
pt
ed
to
en
ter
th
e
el
ap
se
d
ti
m
e
in
se
co
nd
s.
Re
fer
en
ce
va
ria
bl
e
m
us
t
be
us
ed
in
th
e
fu
nc
tio
n
ar
gu
m
en
ts
fo
r
ho
ur
s,
mi
nu
tes
an
d
se
co
nd
s.
#i
nc
lu
de
<i
os
tr
ea
m.
h
#i
nc
lu
de
<m
at
h.
h
//
Fu
nc
ti
on
De
cl
ar
at
io
n
vo
id
ch
an
ge
_t
im
e(
in
t
el
ap
se
d_
se
co
nd
s,
in
t
&h
ou
rs
,
in
t
&m
in
s,
in
t
&s
ec
on
ds
);
ma
in
()
{
in
t
el
ap
se
d_
ti
me
;
in
t
ti
me
_h
ou
rs
,
ti
me
_m
in
ut
es
,
ti
me
_s
ec
on
ds
;
co
ut
<<
"E
nt
er
th
e
ti
me
fo
r
th
e
ev
en
t\
n"
;
ci
n
el
ap
se
d_
ti
me
;
//
Fu
nc
ti
on
ca
ll
se
nd
ad
dr
es
s
of
va
ri
ab
le
s
to
ch
an
ge
ch
an
ge
_t
im
e(
el
ap
se
d_
ti
me
,
ti
me
_h
ou
rs
,
ti
me
_m
in
ut
es
,
ti
me
_s
ec
on
ds
);
//
Us
e
va
ri
ab
le
s
as
no
rm
al
co
ut
<<
"T
he
nu
mb
er
of
ho
ur
s
is
"
<<
ti
me
_h
ou
rs
<<
"
\n
";
co
ut
<<
"T
he
nu
mb
er
of
mi
nu
te
s
is
"<
<
ti
me
_m
in
ut
es
<<
"
\n
";
co
ut
<<
"T
he
nu
mb
er
of
se
co
nd
s
is
"
<<
ti
me
_s
ec
on
ds
<<
"
\n
";
co
ut
<<
"T
he
nu
mb
er
of
se
co
nd
s
is
"
<<
ti
me
_s
ec
on
ds
<<
"
\n
";
}
//
Fu
nc
ti
on
to
co
nv
er
t
to
ta
l
in
se
co
nd
s
to
ho
ur
s,
mi
ns
an
d
se
co
nd
s
vo
id
ch
an
ge
_t
im
e(
in
t
el
ap
se
d_
se
co
nd
s,
in
t
&h
ou
rs
,
in
t
&m
in
s,
in
t
&s
ec
on
ds
)
{
in
t
tm
p_
mi
nu
te
s;
se
co
nd
s
=
el
ap
se
d_
se
co
nd
s%
60
;
tm
p_
mi
nu
te
s
=
el
ap
se
d_
se
co
nd
s/
60
;
mi
ns
=
tm
p_
mi
nu
te
s%
60
;
ho
ur
s
=
tm
p_
mi
nu
te
s
/6
0;
}
Back to Main Page for Lecture 6
F
u
n
c
t
i
o
n
s
A
dd
iti
on
al
Ex
a
m
pl
es
Th
er
e
ar
e
thr
ee
ad
dit
io
na
l
w
or
ke
d
ex
a
m
pl
es:
Example 1
Example 2 - using reference variables
Example 3 - Functions and arrays
E
x
a
m
p
l
e
1
P
R
O
B
L
E
M
:
W
rit
ea
fu
nc
tio
n
th
at
wi
ll
di
sp
la
y
a
lin
e
of
n
ast
eri
sk
s
on
th
e
sc
re
en
.n
wi
ll
be
pa
ss
ed
as
a
ar
gu
m
en
t
to
th
e
fu
nc
tio
n.
Af
ter
wr
iti
ng
th
e
lin
e
of
n
ast
eri
sk
sa
ne
w
lin
e
sh
ou
ld
be
pri
nt
ed
.
W
rit
ea
pr
og
ra
m
to
ca
ll
thi
s
fu
nc
tio
n,
us
in
g
it
to
di
sp
la
y
a
gri
d
of
m
ti
m
es
n
ast
eri
sk
s.
Th
e
us
er
sh
ou
ld
be
pr
o
m
pt
ed
to
en
ter
s
th
e
va
lu
es
fo
r
m
an
d
n.
S
O
L
U
TI
O
N:
C
on
si
de
rin
g
ini
tai
lly
th
e
fu
nc
tio
n,
th
e
fir
st
thi
ng
to
co
ns
id
er
is
W
ha
t
ar
gu
m
en
ts
ar
e
to
be
se
nt
to
th
e
fu
nc
tio
n?
O
bv
io
us
ly,
it
ne
ed
to
be
tol
d
ho
w
m
an
y
ast
eri
sk
s
to
wr
ite
,
in
thi
s
ca
se
n
w
hi
ch
wi
ll
be
int
eg
er
?
Th
e
ne
xt
thi
ng
to
co
ns
id
er
is
w
ha
t
wi
ll
be
ret
ur
ne
d
by
th
e
fu
nc
tio
n?
O
bv
io
us
ly,
th
e
fu
nc
tio
n
on
ly
pri
nt
s
to
sc
re
en
an
d
th
er
ef
or
e
do
es
no
t
ne
ed
to
ret
ur
n
an
yt
hi
ng
to
th
e
m
ai
n
ca
lli
ng
pr
og
ra
m.
Th
e
ret
ur
n
ty
pe
is
th
er
ef
or
e
vo
id
W
e
ca
n
no
w
co
nti
nu
e
an
d
wr
ite
th
e
fu
nc
tio
n.
vo
id
li
ne
(i
nt
n)
{
in
t
i;
fo
r
(i
=0
;
i
<
n;
i+
+)
co
ut
<<
"*
";
co
ut
<<
"\
n"
;
}
Th
e
co
m
pl
et
e
pr
og
ra
m,
ha
s
to
ca
ll
thi
s
fu
nc
tio
n
m
ti
m
es,
an
d
so
a
lo
op
wi
ll
be
us
ed
to
ca
ll
th
e
pr
og
ra
m.
Th
e
co
m
pl
et
e
so
lut
io
n
is
th
en
.
#i
nc
lu
de
<i
os
tr
ea
m.
h
//
fu
nc
ti
on
de
cl
ar
at
io
n
*/
vo
id
li
ne
(
in
t
n)
;
ma
in
()
{
in
t
i;
in
t
m,
n;
co
ut
<<
"E
nt
er
va
lu
es
fo
r
m
an
d
n
\n
";
ci
n
m
n
;
//
Lo
op
to
ca
ll
fu
nc
ti
on
m
ti
me
s
fo
r
(i
=0
;
i
<
m;
i+
+)
li
ne
(n
);
}
//
fu
nc
ti
on
de
fi
ni
ti
on
vo
id
li
ne
(i
nt
n)
{
in
t
i;
fo
r
(i
=0
;
i
<
n;
i+
+)
co
ut
<<
"*
";
co
ut
<<
"\
n"
;
}
A
D
DI
TI
O
N
A
L
E
X
E
R
CI
S
E
A
m
en
d
th
e
m
ai
n
pr
og
ra
m
wr
itt
en
ab
ov
e,
so
th
at
th
e
fol
lo
wi
ng
ty
pe
of
pa
tte
rn
is
di
sp
la
ye
d.
Th
is
is
sh
o
w
n
fo
r
th
e
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
i
v
e
n
.
Back to Main Page for Lecture 6
Hints from Nick about the minimax function
Example 1
Multiplication by repeated addition
Example 2
Euclids Algorithm for Greatest Common Divisor
Example 3
Factorial Example
Example 4
Fibonacci Sequence
Back to Main Page for Lecture 6
Back to Main Page for Lecture 6
Back to Questions
Back to Main Page for Lecture 6
Learning Objectives
Understand the concept of pointers which point to memory locations
Know how * and & are used with pointers and what they do
Be able to use command line arguments to input data to programs
Understand the concept of linked lists
Source Code from Notes
Additional Examples
Quick Check Exercises
Additional Exercises to try
Test Cases for Tutorial 7
Previous Lecture
Home
Next Lecture
Back to Main Page for Lecture 7
Back to Main Page for Lecture 7
Back to Main Page for Lecture 7
Back to Questions
Back to Main Page for Lecture 7
Learning Objectives
Be able to open a file to read from or write to
Understand the different modes in which files can be opened
Know how to check if the file has been opened correctly
Be able to read from or write to the file
Be able to close a file after use
Understand how to use functions such as eof()
Source Code from Notes
Additional Examples
Quick Check Exercises
Additional Exercises to try
Test Cases for Tutorial 8
Previous Lecture
Home
Next Lecture
Back to Main Page for Lecture 8
Back to Main Page for Lecture 8
3
bob
10 10 10 10
fred
2
12 14 15
john
3
4
5
6
bob
10
fred
10.75
john
4.5
Back to Main Page for Lecture 8
Back to Questions
Back to Main Page for Lecture 8
Learning Objectives
Understand the basic concepts of classes and object orientated programming
Understand the concepts of member functions and member data
Be able to declare a class
Be able to access the members of class
Understand the terms private and public
Be able to create a constructor for a class
Source Code from Notes
Additional Examples
Quick Check Exercises
Additional Exercises to try
Test Cases for Tutorial 9
Previous Lecture
Home
Back to Main Page for Lecture 9
Back to Main Page for Lecture 9
Back to Main Page for Lecture 9
Back to Questions
Back to Main Page for Lecture 9
Download