350 likes | 550 Vues
COMP103: Computer and Programming Fundamentals II Prof. Helen Shen < helens @cs.ust.hk>. Course Web: URL: www.cs.ust.hk/course/comp103/. Why are you taking COMP103?. Are these your motivation?. Because many people take it Just a random choice, no particular reasons A required course
E N D
COMP103: Computer and Programming Fundamentals IIProf. Helen Shen <helens@cs.ust.hk> Course Web: URL: www.cs.ust.hk/course/comp103/
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
I believe your REAL motivation is ... I take it because I am interested COMP103 - Introduction
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
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 tobuild a common data structure used by programmers – the “linked list” COMP103 - Introduction
Course 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 Tentative Outline COMP103 - Introduction
Assessment • Labs 10% • Assignments 30% (two programming assignments) • Quiz 10% • Midterm 20% • Final 30% COMP103 - Introduction
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
Teaching Assistants • Teaching Assistants and Graders: • NG, Cherrie cherrie@cs.ust.hk • Wong, David csdavid@cs.ust.hk • Wu, Sally sallywu@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. COMP103 - Introduction
Work hard COMP103 - Introduction
Try more exercises and more practice COMP103 - Introduction
Do the labs and assignments by yourself COMP103 - Introduction
Be patient with the machine COMP103 - Introduction
If you really need that, do it quietly ... COMP103 - Introduction
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: (helens@cs.ust.hk) COMP103 - Introduction
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++In Basic for(i=0; i < 10; i++) for i=1 to 10, step 1 cout << i << endl; 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
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 (=, +, -, /, *, %, 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 COMP103 - Introduction
Basic 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 COMP103 - Introduction
Data Operations • Assignment operation • =, +=, -=, *=, /=, %=, ++, -- • Arithmetic operations • +, -, x, / • Relational & logical operations • ==, !=, >, >=, <, <=, &&, || • Input/Output operations • cout << y, cin >> x • File input/output operations • InFile >> ch, OutFile << ch COMP103 - Introduction
Flow Control • Simple Branching if (value < 0) { cout << “ Number is negative “; } else { cout << “ Number is positive “; } COMP103 - Introduction
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
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
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
Function: recursion int fac( int n ){ // iteration version int product=1; while( n>1 ) { product *= n; n--; } return product; } int fac( int n ){ // recursive version if( n<=1 ) // base case return 1; else return n * fac( n-1 ); } COMP103 - Introduction
Good Programming Methodology • Structured programming • Program Goal: • Print out the following diamond pattern * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * COMP103 - Introduction
Good 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; * *** ***** ******* ********* ******* ***** *** * COMP103 - Introduction
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
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--){ //bottom half for(space=1; space<=5-row; space++) cout << " "; for(star=1; star<=2*row-1; star++) cout << "*"; cout << endl ; } COMP103 - Introduction
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
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
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
Clean Coding Style Your code should be readable by everyone! COMP103 - Introduction
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