Title: COMP103: Computer and Programming Fundamentals II Prof' Helen Shen helenscs'ust'hk
1COMP103 Computer and Programming Fundamentals
IIProf. Helen Shen lthelens_at_cs.ust.hkgt
- Course Web
- URL www.cs.ust.hk/course/comp103/
2Why are you taking COMP103?
3Are 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
4 I believe your REAL motivation is ...
I take it because I am interested
5Course 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.
6Concepts 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
tobuild a common data structure used by
programmers the linked list
7Course 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
8Assessment
- Labs 10
- Assignments 30 (two programming assignments)
- Quiz 10
- Midterm 20
- Final 30
9Resources
- 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
10Teaching Assistants
- Teaching Assistants and Graders
- NG, Cherrie cherrie_at_cs.ust.hk
- Wong, David csdavid_at_cs.ust.hk
- Wu, Sally sallywu_at_cs.ust.hk
- -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.
11Five Tips to Success
12Work hard
13Try more exercises and more practice
14Do the labs and assignments by yourself
15Be patient with the machine
16If you really need that, do it quietly ...
17Remember . . .
- This course is for you!
- Without the students the university wouldnt
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 (helens_at_cs.ust.hk)
18Your 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 In Basic
- for(i0 i lt 10 i) for i1 to 10,
step 1 - cout ltlt i ltlt endl print i
- If you dont 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
19Quick Programming Review
- Most programming languages provide the following
- Basic data types for variables
- integer, real, boolean, 1-D array, 2-D array
- Data operations (, , -, /, , , cin, cout,
etc.) - Flow control (sequential, branching, iteration)
- Function support (sometimes called sub
routines) - parameter passing by value, by reference
- recursive function
- As a programmer, you should exhibit
- Good programming methodology style
20Basic Data Structure (cont.)
- SIMPLE DATA TYPE in C
- Category Data types by size
- Character char, signed char, unsigned char
- Signed integer short, int, long
- Unsigned integer unsigned short, unsigned,
- unsigned long
- Floating point float, double, long double
- COLLECTION OF DATA
- 1-D Arrays, multi-D Arrays, Strings
21Data Operations
- Assignment operation
- , , -, , /, , , --
- Arithmetic operations
- , -, x, /
- Relational logical operations
- , !, gt, gt, lt, lt, ,
- Input/Output operations
- cout ltlt y, cin gtgt x
- File input/output operations
- InFile gtgt ch, OutFile ltlt ch
22Flow Control
- Simple Branching
- if (value lt 0)
-
- cout ltlt Number is negative
-
- else
-
- cout ltlt Number is positive
-
23Flow 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 ltlt Incorrect value for
Month.\n -
24Flow Control (cont.)
- Iteration (while, for, do-while)
- cin gtgt next_value
- while (next_value gt 0)
-
- sum next_value
- cin gtgt next_value
-
- for (int counter 1 counter lt N counter)
- cout ltlt counter ltlt
- do
-
- cout ltlt Do it again?
- cin gtgt response
- while ((response Y) (response y))
25Function parameter passing
- int main ( )
- double x, y, sum, mean
- cout ltlt "Enter two numbers "
- cin gtgt x gtgt y
- sum_ave (x, y, sum, mean)
- cout ltlt "The sum is " ltlt sum ltlt endl
- cout ltlt "The average is " ltlt mean ltlt
endl - return 0
-
- void sum_ave(double no1, double no2, double sum,
double average) - sum no1 no2
- average sum / 2
-
26Function recursion
- int fac( int n ) // iteration version
- int product1
- while ( ngt1 )
- product n
- n--
-
- return product
-
- int fac( int n ) // recursive version
- if ( nlt1 ) // base case
- return 1
- else
- return n fac( n-1 )
-
27Good Programming Methodology
- Structured programming
- Program Goal
- Print out the following diamond pattern
-
-
-
-
-
-
-
-
-
28Good Programming Methodology (cont)
- Break the problem into sub-problems
- print out the upper half
- print out the lower half
- 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
- 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
29Good 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, (2row - 1) stars
- row 2 print (5-row)spaces, (2row - 1) stars
- row 3 print (5-row)spaces, (2row - 1) stars
- row 4 print (5-row)spaces, (2row - 1) stars
- row 5 print (5-row)spaces, (2row - 1) stars
-
- Algorithm for lower half
- row 4 print (5-row)spaces, (2row - 1) stars
- row 3 print (5-row)spaces, (2row - 1) stars
- row 2 print (5-row)spaces, (2row - 1) stars
- row 1 print (5-row)spaces, (2row - 1) stars
-
-
30Good Programming Methodology (cont)
- // Translate your logic and algorithm to working
code!! - int row, space, star
- for(row1 rowlt5 row) //top half
- for(space1 spacelt5-row space)
- cout ltlt " "
- for(star1 starlt2row-1 star)
- cout ltlt ""
- cout ltlt endl
-
- for(row4 rowgt1 row--) //bottom half
- for(space1 spacelt5-row space)
- cout ltlt " "
- for(star1 starlt2row-1 star)
- cout ltlt ""
- cout ltlt endl
-
31Good Programming Style
- use functions extensively
- avoid global variables
- use reference arguments properly
- use functions properly
- handle errors properly
- avoid goto
- provide good documentation
32Good 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 programs 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
33Good 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
34Clean Coding Style
Your code should be readable by everyone!
35This is it
- If you feel comfortable with basic programming
and want to learn more about C and
object-oriented programming - Welcome to COMP103 ?