Course Web: URL: www.cs.ust.hk/course/comp103/ COMP103: Computer and Programming

Download Report

Transcript Course Web: URL: www.cs.ust.hk/course/comp103/ COMP103: Computer and Programming

COMP103: Computer and Programming
Fundamentals II
Prof. Helen Shen <[email protected]>
Course Web:
URL: www.cs.ust.hk/course/comp103/
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F.
Gilberg, Thomson Learning, 2000.
Why are you
taking
COMP103?
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F.
Gilberg, Thomson Learning, 2000.
Are these your motivation?









Because many people take it
Just a random choice, no particular reasons
A required course
I failed COMP103 before; I failed COMP104
I got a good grade in COMP102
Short lecture hours
Want to be taught by a tough instructor and TAs
Want to be taught by a pretty/handsome TA
More …
COMP103 - Introduction
3
I believe your REAL motivation is ...
I take it because I
am interested
COMP103 - Introduction
4
Course Objectives




Covers important, fundamental topics in structured
programming
Introduces object-oriented programming concepts.
After this class, if you do a decent job in exams, and
produce reasonable work which represents your own effort,
you should be able to analyze a small problem and translate
it into working computer code in terms of classes and
objects.
Through this class, we hope you can


(1) develop the right mentality to solve a problem
(2) excel at the right programming tool, such as C++.
COMP103 - Introduction
5
Concepts to be covered
The course is divided roughly into 4 parts
 1st Review of basic programming




And a Quiz on the topic 
2nd Memory manipulation

Pointers and memory address space

Dynamic memory allocation
3rd Introduction to object-oriented programming

Classes and Objects in C++

Data encapsulation and information hiding

Abstract data types
4th Linked List Data Structure

We will use memory management + Class/Object to
build a common data structure used by programmers – the “linked list”
COMP103 - Introduction
6
Course Outline
Tentative Outline
 Programming Review (3 weeks) - Ch 2 to 8

Pointers and Dynamic Memory (3 weeks) - Ch 9

Class & Objects (2 weeks) - Ch 10,11

ADT & OO Design (2 weeks) - Ch 12

Linked-List, Searching & Sorting (3 weeks) - Ch 17
COMP103 - Introduction
7
Assessment

Labs
10%

Assignments
30% (two programming assignments)

Quiz
10%

Midterm
20%

Final
30%
COMP103 - Introduction
8
Resources

Text book:

Computer Science - A Structured Approach using
C++, Forouzan & Gilberg, second edition, Thomson
Learning, 2004.


This book has lots of C++ examples, it is highly recommended that you
purchase it.
Other resources:

Please refer to the course web page
COMP103 - Introduction
9
Teaching Assistants

Teaching Assistants and Graders:



NG, Cherrie
Wong, David
Wu, Sally
[email protected]
[email protected]
[email protected]
-The TAs will provide you tutorial in labs and help
you on assignments! They are your friends 
-The TAs have offices on campus (see webpage)
which you can visit them. You can also send them
e-mail to arrange meetings.
COMP103 - Introduction
10
Five Tips to Success
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F.
Gilberg, Thomson Learning, 2000.
Work hard
COMP103 - Introduction
12
Try more exercises and more practice
COMP103 - Introduction
13
Do the labs and assignments by yourself
COMP103 - Introduction
14
Be patient with the machine
COMP103 - Introduction
15
If you really need that, do it quietly ...
COMP103 - Introduction
16
Remember . . .




This course is for you!
Without the students the university wouldn’t
exist
If you have questions, concerns (like the TA is
not answering your email), or any suggestions,
please let me (the instructor) know
You can also always ask me questions via e-mail
at: ([email protected])
COMP103 - Introduction
17
Your background for COMP103




In this course, we assume that you have some programming
background (e.g. COMP102)
The following slides are a quick programming review
Programming languages are all relatively similar, but their
“syntax” can be different
Example: for loops in C++ and in Basic:
In C++
for(i=0; i < 10; i++)
cout << i << endl;


In Basic
for i=1 to 10, step 1
print i
If you don’t know the exact syntax for C++ that is OK, there
is always a C++ book you can refer to
If none of the following slides seems familiar then you may
consider taking a more basic programming course like
COMP102
COMP103 - Introduction
18
Quick Programming Review

Most programming languages provide the following:
 Basic data types for variables


Data operations (=, +, -, /, *, %, cin, cout, etc.)

Flow control (sequential, branching, iteration)

Function support (sometimes called “sub routines”)



integer, real, boolean, 1-D array, 2-D array
parameter passing by value, by reference
recursive function
As a programmer, you should exhibit
 Good programming methodology & style
COMP103 - Introduction
19
Basic Data Structure (cont.)

SIMPLE DATA TYPE in C++
Category
Character
Signed integer
Unsigned integer
Floating point

Data types by size
char, signed char, unsigned char
short, int, long
unsigned short, unsigned,
unsigned long
float, double, long double
COLLECTION OF DATA

1-D Arrays, multi-D Arrays, Strings
COMP103 - Introduction
20
Data Operations

Assignment operation


Arithmetic operations


==, !=, >, >=, <, <=, &&, ||
Input/Output operations


+, -, x, /
Relational & logical operations


=, +=, -=, *=, /=, %=, ++, --
cout << y, cin >> x
File input/output operations

InFile >> ch, OutFile << ch
COMP103 - Introduction
21
Flow Control

Simple Branching
if (value < 0)
{
cout << “ Number is negative “;
}
else
{
cout << “ Number is positive “;
}
COMP103 - Introduction
22
Flow Control

More complex Branching
switch (month)
{ case 9: case 4: case 6: case 11:
days_in_month = 30;
break;
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days_in_month = 31;
break;
case 2: if (leap_year)
days_in_month = 29;
else
days_in_month = 28;
break;
default:
cout << “Incorrect value for Month.\n”;
}
COMP103 - Introduction
23
Flow Control (cont.)

Iteration (while, for, do-while)



cin >> next_value;
while (next_value > 0)
{
sum += next_value;
cin >> next_value;
}
for (int counter = 1; counter <= N; ++counter)
cout << counter << ‘ ‘;
do
{
cout << ‘Do it again?’;
cin >> response;
} while ((response == ‘Y’) || (response == ‘y’));
COMP103 - Introduction
24
Function: parameter passing
int main ( ) {
double x, y, sum, mean;
cout << "Enter two numbers: ";
cin >> x >> y;
sum_ave (x, y, sum, mean);
cout << "The sum is " << sum << endl;
cout << "The average is " << mean << endl;
return 0;
}
void sum_ave(double no1, double no2, double& sum,
double& average) {
sum = no1 + no2;
average = sum / 2;
}
COMP103 - Introduction
25
Function: recursion
int fac( int n ){
int product=1;
while ( n>1 ) {
product *= n;
n--;
}
return product;
}
// iteration version
int fac( int n ){
if ( n<=1 )
return 1;
else
return n * fac( n-1 );
}
// recursive version
// base case
COMP103 - Introduction
26
Good Programming Methodology

Structured programming

Program Goal:

Print out the following diamond pattern
*
* *
* * *
* *
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* *
* * *
* *
*
COMP103 - Introduction
27
Good Programming Methodology (cont)

Break the problem into sub-problems:




Think about how to solve the sub-problems *
***
Print out upper half:
 row 1: print 4 spaces, 1 star;
*****
 row 2: print 3 spaces, 3 stars;
*******
 row 3: print 2 spaces, 5 stars;
*********
 row 4: print 1 space,
7 stars;


print out the upper half
print out the lower half
row 5: print 0 spaces, 9 stars;
Print out lower half:




row 4: print 1 space, 7 stars;
row 3: print 2 spaces, 5 stars;
row 2: print 3 spaces, 3 stars;
row 1: print 4 spaces, 1 star;
COMP103 - Introduction
*******
*****
***
*
28
Good Programming Methodology (cont)


Think of the “logic” and “algorithms” needed to
realize the solutions to the problems
Algorithm for upper half:






row 1: print (5-row)spaces, (2*row - 1) stars;
row 2: print (5-row)spaces, (2*row - 1) stars;
row 3: print (5-row)spaces, (2*row - 1) stars;
row 4: print (5-row)spaces, (2*row - 1) stars;
row 5: print (5-row)spaces, (2*row - 1) stars;
Algorithm for lower half:




row 4: print (5-row)spaces, (2*row - 1) stars;
row 3: print (5-row)spaces, (2*row - 1) stars;
row 2: print (5-row)spaces, (2*row - 1) stars;
row 1: print (5-row)spaces, (2*row - 1) stars;
COMP103 - Introduction
*
***
*****
*******
*********
*******
*****
***
*
29
Good Programming Methodology (cont)
// Translate your logic and algorithm to working code!!
int row, space, star;
for(row=1; row<=5; row++){
//top half
for(space=1; space<=5-row; space++)
cout << " ";
for(star=1; star<=2*row-1; star++)
cout << "*";
cout << endl ;
}
for(row=4; row>=1; row--){
for(space=1; space<=5-row; space++)
cout << " ";
for(star=1; star<=2*row-1; star++)
cout << "*";
cout << endl ;
}
//bottom half
COMP103 - Introduction
30
Good Programming Style







use functions extensively
avoid global variables
use reference arguments properly
use functions properly
handle errors properly
avoid goto
provide good documentation
COMP103 - Introduction
31
Good Documentation Style



As a student, you often want to ignore documentation
In a real company, where hundreds of programmers have
to work together, documentation is very very important!
Good documentation style include

An initial comment for the program that includes:








statement of purpose
author and date
description of the program’s input and output
description of how to use the program
assumptions such as the type of data expected
statement of exceptions, that is, what could go wrong
description of the key variables
Initial comments in each function that state its purpose,
preconditions, postconditions, and functions called
COMP103 - Introduction
32
Good Documentation Style


Comments in the body of each function to explain
important features or subtle logic
Consistent Naming convention






Function names begin with a lowercase letter
Variables begin with a lowercase letter
Words in multiple-words identifiers each separated by an
underscore
Named constants and enumerators are entirely uppercase
and use underscores to separate words
user-defined data types and names of structures, classes
begin with an uppercase letter
consistent indentation style
COMP103 - Introduction
33
Clean Coding Style
Your code should be readable by everyone!
Single Statement Blocks
Multi-Statement Blocks
if (condition)
statement;
if (condition)
{
statements;
}
for(int i=0; i < 10; i++)
statement;
for(int i=0; i < 10; i++)
{
statements;
}
while (condition)
statement;
while (condition)
{
statements;
}
if (condition)
statement;
elseif (condition)
statement;
else
statement;
if (condition)
{
statement;
}
elseif (condition)
{
. . .
COMP103 - Introduction
34
This is it

If you feel comfortable with basic
programming and want to learn more
about C++ and object-oriented
programming
Welcome to COMP103 
COMP103 - Introduction
35