Скачать презентацию 1 MT 258 Computer Programming and Problem Solving Скачать презентацию 1 MT 258 Computer Programming and Problem Solving

c501a0253c72b3b0f4c7d46122012eaa.ppt

  • Количество слайдов: 54

1 MT 258 Computer Programming and Problem Solving Unit 1 1 MT 258 Computer Programming and Problem Solving Unit 1

Important points w Method for calculating the final grade. w w The procedure for Important points w Method for calculating the final grade. w w The procedure for requesting of extensions. The procedure for handing in TMAs. w Programming style. 2

Contents w w w Course Outline Questions and Answers Revision of Unit 1 Questions Contents w w w Course Outline Questions and Answers Revision of Unit 1 Questions and Answers What to do next? 3

Course Overview • • • Basics of writing programs Loops and logic Data and Course Overview • • • Basics of writing programs Loops and logic Data and structures Break-down and build-up Abstract data and hidden code Search and order Divide and conquer Building lists and trees From algorithms to problem solving Revision 4 weeks 5 weeks 4 weeks 4 weeks 2 weeks 4

Study Material w Course Material (Unit 1 - 9) (Blue Books) w Text Book Study Material w Course Material (Unit 1 - 9) (Blue Books) w Text Book • C How to Program 5

TEXT BOOK w Deitel/Deitel C HOW TO PROGRAM Prentice Hall 6 TEXT BOOK w Deitel/Deitel C HOW TO PROGRAM Prentice Hall 6

Reference Book w Angela Shiflet Problem Solving in C Including Breadth and Laboratories West Reference Book w Angela Shiflet Problem Solving in C Including Breadth and Laboratories West w Kruse, R. L. , Leung, B. P. , and Tondo, C. L. Data Structures and Program Design in C (2 nd Edition). Prentice Hall 7

Reference Book w Herbert Schildt The Complete Reference (3 rd Edition) Osborne Mc. Graw-Hill Reference Book w Herbert Schildt The Complete Reference (3 rd Edition) Osborne Mc. Graw-Hill 8

Reference Book (recommended by Celia) w Brian W. Kernighan, Dennis M. Ritchie The C Reference Book (recommended by Celia) w Brian W. Kernighan, Dennis M. Ritchie The C Programming Language Prentice Hall Software Series 9

On line Reference w MT 258 SUPPLEMENT http: //learn. ouhk. edu. hk/~mt 258/MT 258 On line Reference w MT 258 SUPPLEMENT http: //learn. ouhk. edu. hk/~mt 258/MT 258 Main. Frame. htm • Self Study Centre • • Supplementary notes Errata to Study Units Perform Laboratory • Specimen Examination paper • Specimen TMA 10

Online Reference w OLE • Discussion board 11 Online Reference w OLE • Discussion board 11

How to Success w w Follow course schedule Fully understand • Blue books Submit How to Success w w Follow course schedule Fully understand • Blue books Submit TMA on time Study TMA solution 12

UNIT ONE 13 UNIT ONE 13

History of Programming Language and C 14 History of Programming Language and C 14

Writing Your First Prgram Body 15 Writing Your First Prgram Body 15

First Program Body #include <stdio. h> void main() { printf(“Hello. I am a C First Program Body #include void main() { printf(“Hello. I am a C program. “); printf(“My name is Celia. n”); printf(“Nice to meet you. I will be your friend for life. : ) n”); printf(“You may press Enter to byen”); fflush(stdin); getchar(); Semicolon } Hello. I am a C program. My name is Celia. Nice to meet you. I will be your friend for life. : ) You may press Enter to bye 16

Second Program declares a variable #include <stdio. h> void main() { int age; printf(“My Second Program declares a variable #include void main() { int age; printf(“My name is Celia. Tell me your age: “); scanf(“%d”, &age); input statement printf(“Oh! You are only %dn”, age); printf(“Very young! ; ) n”); printf(“You may press Enter to byen”); output statement fflush(stdin); getchar(); } My name is Cee. Tell me your age: 20 Oh! You are only 20 Very young! ; ) You may press Enter to bye 17

Overview of basic elements in C w Output • Output of a program may Overview of basic elements in C w Output • Output of a program may be directed to the user through the screen, an external device such as a printer, or another program. w Input • It allows users to give data to programs and receive results. Input may also come from an external device such as the network, or another program. w Variables • Variables allow data to be temporarily stored in memory. A variable lives as long as the program lives. 18

Variable w w w They are typed for storing different value types. a piece Variable w w w They are typed for storing different value types. a piece of memory for storing data within the program. The data will disappear when the program terminates. They should be declared before use. They should initialized; otherwise the value is undefined. They have precision and range limits. The precision and range limits of various types are dependent on the computer and operating system. 19

Variables w The declaration has two parts. • The type of the variable. • Variables w The declaration has two parts. • The type of the variable. • The name that identifies the variable – Identifier. 20

Data types w w w w w char short int unsigned long float double Data types w w w w w char short int unsigned long float double long double no string w no Boolean w 21

Activity 1 w Name any 4 fundamental data types of C. 22 Activity 1 w Name any 4 fundamental data types of C. 22

Activity 1 w Name any 4 fundamental data types of C. • • int Activity 1 w Name any 4 fundamental data types of C. • • int float double char 23

Identifier w w w the name that identifies the variable. arbitrary names given to Identifier w w w the name that identifies the variable. arbitrary names given to functions, variables, user-defined data types, and so on. unique in the program that no other programming component would share the same name. 24

Rules concerning the identifier w w w Identifiers can be up to 31 characters Rules concerning the identifier w w w Identifiers can be up to 31 characters in length. An identifier must begin with a letter or an underscore. After the initial letter, subsequent characters may be the letters a to z and A to Z, the underscore character "_", and the digits 0 to 9. Identifiers are case-sensitive. No keyword may be used as a variable identifier. 25

Keywords in C w w w auto break case char const continue default do Keywords in C w w w auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct volatile switch while typedef union unsigned void 26

Activity 2 w Which of the following variable identifiers are invalid ? • • Activity 2 w Which of the following variable identifiers are invalid ? • • • const a* xchar b_ cont 3 phases enum profit+tax Nu. Mb. Er 27

Activity 2 w Which of the following variable identifiers are invalid ? • • Activity 2 w Which of the following variable identifiers are invalid ? • • • const a* xchar b_ cont 3 phases enum profit+tax • Nu. Mb. Er (invalid) (valid) (invalid) (valid) 28

Using variables in your programs declaring a variable of a certain type w setting Using variables in your programs declaring a variable of a certain type w setting a value to a variable w getting the value from a variable w 29

Variables declaration & initialisation w Declaration examples : • int a. Integer; • float Variables declaration & initialisation w Declaration examples : • int a. Integer; • float a. Real. Number; • char a. Char; Before a variable is being initialized or assigned to a value, its value is undefined. w Initialisation examples : • int a. Integer; or int a. Integer = 3 ; • a. Integer = 3; w char a. Char = ‘c’; • (single quote is a character) • char a. Char = “c”; (Not correct) • (double quote is a string) • char x[10]="computer"; • 30

Variable Precision w caused by assignment from one type to another type • float Variable Precision w caused by assignment from one type to another type • float a. Float = 3. 6; • int a. Integer; • a. Interger = a. Float; • (value in a. Interger will be 3 instead of 3. 6) w caused by limited precision of the type • float a. Float = 3. 141592612345 • (value in a. Float will be 3. 141593) w caused by limited in storing values to most primitive data types, machine and operating system dependent. 31

Program Example #include <stdio. h> void main() { char a. Char = ‘B’; printf(“The Program Example #include void main() { char a. Char = ‘B’; printf(“The value of a. Char is %cn”, a. Char); printf(“The ASCII value of a. Char is %dn”, a. Char); printf(“The value of ‘B’ is %cn”, ‘B’); printf(“The ASCII value of ‘B’ is %dn”, ‘B’); fflush(stdin); getchar(); } The value of a. Char is B The ASCII value of a. Char is 66 The value of ‘B’ is B The ASCII value of ‘B’ is 66 32

Program Example #include <stdio. h> void main() { char a. Char = 66; printf(“The Program Example #include void main() { char a. Char = 66; printf(“The value of a. Char is %cn”, a. Char); printf(“The ASCII value of a. Char is %dn”, a. Char); fflush(stdin); getchar(); } The value of a. Char is B The ASCII value of a. Char is 66 33

Program Example #include <stdio. h> void main() { char first. Char = ‘ 1’; Program Example #include void main() { char first. Char = ‘ 1’; char second. Char; second. Char = first. Char; printf(“The value of second. Char is %cn”, second. Char); printf(“The ASCII value of second. Char is %dn”, second. Char); fflush(stdin); getchar(); } The value of second. Char is 1 The ASCII value of second. Char is 49 34

Program Example #include <stdio. h> void main() { int first. Int; float second. Float; Program Example #include void main() { int first. Int; float second. Float; first. Int = 9. 8; printf(“Value of first. Int is %dn”, first. Int); second. Float = 123. 4567890123456789; printf(“Value of second. Float is %fn”, second. Float); fflush(stdin); getchar(); } Value of first. Int is 9 Value of second. Float is 123. 456787 35

Program Example #include <stdio. h> void main() { float low. Float; double. Float; low. Program Example #include void main() { float low. Float; double. Float; low. Float = 123. 4567890123456789; double. Float = 123. 4567890123456789; printf(“Value of low. Float is %. 16 fn”, low. Float); printf(“Value of double. Float is %. 16 fn”, double. Float); fflush(stdin); getchar(); } Value of low. Float is 123. 4567871093750000 Value of double. Float is 123. 4567890123456806 36

Program Example #include <stdio. h> void main() { int an. Int = 123456789012345; printf(“Value Program Example #include void main() { int an. Int = 123456789012345; printf(“Value of an. Int is %dn”, an. Int); fflush(stdin); getchar(); } Value of an. Int is -2045911175 37

Program Example #include <stdio. h> #include <limits. h> void main() { printf(“Maximum value of Program Example #include #include void main() { printf(“Maximum value of char = %dn”, CHAR_MAX); printf(“Minimum value of char = %dn”, CHAR_MIN); printf(“Maximum value of int = %dn”, INT_MAX); printf(“Minimum value of int = %dn”, INT_MIN); printf(“Maximum value of long = %dn”, LONG_MAX); printf(“Minimum value of long = %dn”, LONG_MIN); fflush(stdin); getchar(); } Maximum value of char = 255 Minimum value of char = 0 Maximum value of int = 2147483647 Minimum value of int = -2147483648 Maximum value of long = 2147483647 Minimum value of long = -2147483648 38

Program Example #include <stdio. h> #include <float. h> void main() { printf(“Precision of float Program Example #include #include void main() { printf(“Precision of float = %. 16 fn”, FLT_EPSILON); printf(“Precision of double = %. 16 fn”, DBL_EPSILON); printf(“Maximum value of float = %en”, FLT_MAX); printf(“Minimum value of float = %en”, FLT_MIN); printf(“Maximum value of double = %en”, DBL_MAX); printf(“Minimum value of double = %en”, DBL_MIN); fflush(stdin); getchar(); } Precision of float = 0. 0000001192092896 Precision of double = 0. 000000002 Maximum value of float = 3. 402823 e+38 Minimum value of float = 1. 175494 e-38 Maximum value of double = 1. 797693 e+308 Minimum value of double = 2. 225074 e-308 39

Output w printf • function - writes formatted output to stdout • syntax - Output w printf • function - writes formatted output to stdout • syntax - #include • printf(const char*format[, argument, …. ]) • some input argument • %d - int • %f - float and double • %e - prints in exponential format, eg 1. 23 e 5 • %c – ASCII standard 40

Output w Example of printf • • • #include <stdio. h> int main(void) { Output w Example of printf • • • #include int main(void) { int x; x = 3; printf(“Variable x is assigned as %dn”, x); return 0; } 41

Output (Holding the screen) getch • Function : gets character from keyboard, does not Output (Holding the screen) getch • Function : gets character from keyboard, does not echo to screen. • Syntax : #include • int getch(void); w getchar • Function : gets character from stdin. • Syntax : #include • int getchar(void); w fflush • syntax : #include • int fflush(FILE *stream) • eg. fflush(stdin) 42 w

Output (Holding the screen) • example : • • • #include <stdio. h> int Output (Holding the screen) • example : • • • #include int main(void) { int x; printf(“please enter x: “); scanf (“%d”, &x); fflush(stdin); printf(“The number is %dn”, x); getchar(); return 0; } 43

Output w Escape sequence • the backslash character () is used to introduce an Output w Escape sequence • the backslash character () is used to introduce an escape sequence, allowing the visual representation of certain nongraphic characters. 44

Output w Some available escape sequences • • • n newline t horizontal tab Output w Some available escape sequences • • • n newline t horizontal tab v vertical tab a audible sound b backspace f form feed r carriage \ backslash ” double quote ’ single quote null value 45

Program Example #include <stdio. h> void main() { printf(“Hello. I am Cee. n”); printf(“Nice Program Example #include void main() { printf(“Hello. I am Cee. n”); printf(“Nice to show you how output is done in Cn”); } Hello. I am Cee. Nice to show you how output is done in C 46

Program Example #include <stdio. h> void main() { printf(“My personal informationn”); printf(“Namett: Ceen”); printf(“Sextt: Program Example #include void main() { printf(“My personal informationn”); printf(“Namett: Ceen”); printf(“Sextt: Computer is asexualn”); printf(“Agett: Modernn”); printf(“Interestst: Playing CDs, talking to printers, defragmentationn”); fflush(stdin); getchar(); } My personal Name : Sex : Age : Interests : information Cee Computer is asexual Modern Playing CDs, talking to printers, defragmentation 47

Input w scanf • function - scans and formats input from the stdin stream Input w scanf • function - scans and formats input from the stdin stream • syntax - #include • scanf(const char*format[, address, …. ]) • some input argument • %d • %f • %c 48

Input w Example of scanf • • • #include <stdio. h> int main(void) { Input w Example of scanf • • • #include int main(void) { int x; printf(“Please enter an integern : ”); scanf(“%d”, &x); printf(“Variable x is assigned as %dn”, x); return 0; } 49

Program Example #include <stdio. h> void main() { char a. Char; printf(“Enter a characters: Program Example #include void main() { char a. Char; printf(“Enter a characters: “); a. Char = getchar(); printf(“The character entered is %cn”, a. Char); printf(“Press Enter to byen”); fflush(stdin); getchar(); } Enter a characters: C The character entered is C Enter a characters: The character entered is 50 Press Enter to bye

Program Example #include <stdio. h> void main() { int an. Int; float a. Float; Program Example #include void main() { int an. Int; float a. Float; char a. Char; printf(“Enter an integer, a floating-point number, and a character: “); scanf(“%d”, &an. Int); scanf(“%f”, &a. Float); scanf(“%c”, &a. Char); printf(“Entered data includes %d %f ”%c”n”, an. Int, a. Float, a. Char); fflush(stdin); getchar(); } w Enter an integer, a floating point number, and a character: 10 2. 34 C Entered data includes 10 2. 340000 “ ” w Enter an integer, a floating point number, and a character: 10 2. 34 C Entered data includes 10 2. 340000 “C” w Enter an integer, a floating point number, and a character: 102. 34 C Entered data includes 102 0. 340000 “C” 51

Header files w Header files • also called include files, provide function prototype declarations Header files w Header files • also called include files, provide function prototype declarations for library functions. Data types and symbolic constants used with the library functions are also defined in them, along with global variables defined by Borland C++ and by library function. 52

Header files w w math. h - declares prototypes for the math functions. (eg. Header files w w math. h - declares prototypes for the math functions. (eg. pow) stdio. h - defines types and macros needed to standard I/O package. (eg. printf) time. h - defines a structure filled in by the time-conversion routines. (eg. time) string. h - declares several stringmanipulation and memory-manipulation routines. (eg. strlen) w 53

Activity 3 w w w Identify and correct the error. 1) printf (“The value Activity 3 w w w Identify and correct the error. 1) printf (“The value is %dn”, &number 1); no & is needed 2) scanf (“%d%d”, &num 1, num 2); if (num 3 < 7 ); printf (“num 3 is less than 7n”) • first line : &num 2 should be used • second line : delete ; at the end • third line : ; is missing 54