COMP103 Programming Fundamentals II - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

COMP103 Programming Fundamentals II

Description:

Continues to teach basic C programming techniques ... No make-up examination. Unless under very unusual circumstances, with letters of proof ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 24
Provided by: fuhb
Category:

less

Transcript and Presenter's Notes

Title: COMP103 Programming Fundamentals II


1
COMP103 Programming Fundamentals II
  • http//course.cse.ust.hk/comp103

2
Whats about this course?
  • COMP103 Programming Fundamentals II
  • Continues to teach basic C programming
    techniques
  • Introduces object-oriented programming concepts
  • Covers fundamental ideas in structured
    programming

3
Major Topics
  • The course is roughly divided into 3 major parts
  • 1st Review of basic programming
  • 2nd Pointers and dynamic memory
  • Pointers and memory address space
  • Dynamic memory allocation
  • Linked list
  • 3rd Introduction to object-oriented programming
  • Classes and Objects in C
  • Data encapsulation and information hiding
  • Abstract Data Types (ADT)
  • Template and Standard Template Library (STL)

4
Course Outline (Tentative)
  • Fast Programming Review (2 weeks)
  • Pointers and Dynamic memory allocation (3 weeks)
  • Linked List (3 weeks)
  • Class Objects (2 weeks)
  • Dynamic Class (1 week)
  • Template and STL (1 week)
  • Final (Week 15/16)

5
Lecture Format
  • Feel free to interrupt to ask questions
  • Lectures
  • Slides are available one day before the lecture
  • It is important to attend the lectures and take
    notes (Not all materials are covered in slides)
  • If you miss any lectures, learn from your friends
  • Labs
  • Supplement the lectures
  • Some important exercises
  • Programming assignment
  • More rigorous problems to consolidate your
    knowledge

6
Grading Scheme
  • 10 Labs 15 (programming)
  • 1 Assignment 5 (programming)
  • Midterm 35
  • Final 45

7
Midterm and Final
  • Closed-book, closed-notes
  • No make-up examination
  • Unless under very unusual circumstances, with
    letters of proof
  • Instructor informed beforehand

8
Honesty and Integrity
  • Collaboration You are encouraged to collaborate
    in study groups
  • All work submitted for grading must be your own
  • TAs will catch cheaters
  • What if you are caught copying?
  • 1st time Both get 0
  • 2nd time One full grade lower
  • 3rd time FAIL the course
  • If it is midterm or final, an automatic FAIL

9
Email Policy
  • Email is not effective to explain things. Please
    visit TAs office
  • Please do not expect answers right away
  • Please do not send us codes for debugging
  • We will not debug codes for you

10
Quick Programming Review
  • Most programming languages provide the following
  • Basic data types for variables
  • integer, real, boolean, 1-D array, 2-D array
  • Data operations (, , -, /, , , etc.)
  • Flow control (sequential, branching, iteration)
  • Function support (sometimes called sub
    routines)
  • parameter passing by value, by reference
  • recursive function

11
Basic Data Structure
  • 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

12
Data 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

13
Flow Control Branching
  • Simple Branching
  • if (value lt 0)
  • cout ltlt Number is negative
  • else
  • cout ltlt Number is positive

14
  • 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

15
Flow Control Iteration
  • 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))

16
Function 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

17
Function 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 ) //iterative
    expression

18
Structured Programming Methodology
  • Program Goal Print out the following diamond
    pattern on the screen

19
Problem Analysis
  • 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



20
Algorithm Design
  • 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



21
Coding
  • // 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

22
Practice
  • There is a certain five digit number, x, which
    has the following property
  • The last three digits, squared, subtract the
    first two digits equals the original number.

23
Practice
  • Write a program that displays the calendar for a
    given month of the year.
  • Enter full year (e.g., 2001) 2008
  • Enter month in number between 1 and 12 2
  • February 2008
  • -----------------------------
  • Sun Mon Tue Wed Thu Fri Sat
  • 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
  • Press any key to continue
Write a Comment
User Comments (0)
About PowerShow.com