550 likes | 685 Views
MT258 Computer Programming and Problem Solving. Unit 1. Important points. Method for calculating the final grade. The procedure for requesting of extensions. The procedure for handing in TMAs. Programming style. Contents. Course Outline Questions and Answers Revision of Unit 1
E N D
Important points • Method for calculating the final grade. • The procedure for requesting of extensions. • The procedure for handing in TMAs. • Programming style.
Contents • Course Outline • Questions and Answers • Revision of Unit 1 • Questions and Answers • What to do next?
Course Overview • Basics of writing programs 4 weeks • Loops and logic 5 weeks • Data and structures 4 weeks • Break-down and build-up 5 weeks • Abstract data and hidden code 4 weeks • Search and order 4 weeks • Divide and conquer 4 weeks • Building lists and trees 4 weeks • From algorithms to problem solving 4 weeks • Revision 2 weeks
Study Material • Course Material (Unit 1 - 9) (Blue Books) • Text Book • C How to Program
TEXT BOOK • Deitel/Deitel C HOW TO PROGRAM Prentice Hall
Reference Book • Angela Shiflet Problem Solving in C Including Breadth and Laboratories West • Kruse, R.L., Leung, B.P., and Tondo, C.L. Data Structures and Program Design in C (2nd Edition). Prentice Hall
Reference Book • Herbert Schildt The Complete Reference (3rd Edition) Osborne McGraw-Hill
Reference Book (recommended by Celia) • Brian W. Kernighan, Dennis M. Ritchie The C Programming Language Prentice Hall Software Series
On line Reference • MT258 SUPPLEMENT http://learn.ouhk.edu.hk/~mt258/mt258/MT258MainFrame.htm • Self Study Centre • Supplementary notes • Errata to Study Units • Study Units • Perform Laboratory • Specimen Examination paper • Specimen TMA
Online Reference • OLE • Discussion board
How to Success • Follow course schedule • Fully understand • Blue books • Submit TMA on time • Study TMA solution
First Program Body #include <stdio.h> 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 bye\n”); fflush(stdin); getchar(); } 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 Semicolon
Second Program declares a variable #include <stdio.h> void main() { int age; printf(“My name is Celia. Tell me your age: “); scanf(“%d”, &age); printf(“Oh! You are only %d\n”, age); printf(“Very young! ;) \n”); printf(“You may press Enter to bye\n”); 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 input statement output statement
Overview of basic elements in C • Output • Output of a program may be directed to the user through the screen, an external device such as a printer, or another program. • 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. • Variables • Variables allow data to be temporarily stored in memory. A variable lives as long as the program lives.
Variable • 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.
Variables • The declaration has two parts. • The type of the variable. • The name that identifies the variable – Identifier.
Data types • char • short int • unsigned int • int • unsigned long • long • float • double • long double • no string • no Boolean
Activity 1 • Name any 4 fundamental data types of C.
Activity 1 • Name any 4 fundamental data types of C. • int • float • double • char
Identifier • 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.
Rules concerning the identifier • 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.
Keywords in C • auto default float register struct volatile • break do for return switch while • case double goto short typedef • char else if signed union • const enum int sizeof unsigned • continue extern long static void
Activity 2 • Which of the following variable identifiers are invalid ? • const • a* • xchar • b_ • cont • 3phases • enum • num • profit+tax • NuMbEr
Activity 2 • Which of the following variable identifiers are invalid ? • const (invalid) • a* (invalid) • xchar (valid) • b_ (valid) • cont (valid) • 3phases (invalid) • enum (invalid) • num (valid) • profit+tax (invalid) • NuMbEr(valid)
Using variables in your programs • declaring a variable of a certain type • setting a value to a variable • getting the value from a variable
Variables declaration & initialisation • Declaration examples : • int aInteger; • float aRealNumber; • char aChar; • Before a variable is being initialized or assigned to a value, its value is undefined. • Initialisation examples : • int aInteger; or int aInteger = 3 ; • aInteger = 3; • char aChar = ‘c’; • (single quote is a character) • char aChar = “c”; (Not correct) • (double quote is a string) • char x[10]="computer";
Variable Precision • caused by assignment from one type to another type • float aFloat = 3.6; • int aInteger; • aInterger = aFloat; • (value in aInterger will be 3 instead of 3.6) • caused by limited precision of the type • float aFloat = 3.141592612345 • (value in aFloat will be 3.141593) • caused by limited in storing values to most primitive data types, machine and operating system dependent.
Program Example #include <stdio.h> void main() { char aChar = ‘B’; printf(“The value of aChar is %c\n”, aChar); printf(“The ASCII value of aChar is %d\n”, aChar); printf(“The value of ‘B’ is %c\n”, ‘B’); printf(“The ASCII value of ‘B’ is %d\n”, ‘B’); fflush(stdin); getchar(); } The value of aChar is B The ASCII value of aChar is 66 The value of ‘B’ is B The ASCII value of ‘B’ is 66
Program Example #include <stdio.h> void main() { char aChar = 66; printf(“The value of aChar is %c\n”, aChar); printf(“The ASCII value of aChar is %d\n”, aChar); fflush(stdin); getchar(); } The value of aChar is B The ASCII value of aChar is 66
Program Example #include <stdio.h> void main() { char firstChar = ‘1’; char secondChar; secondChar = firstChar; printf(“The value of secondChar is %c\n”, secondChar); printf(“The ASCII value of secondChar is %d\n”, secondChar); fflush(stdin); getchar(); } The value of secondChar is 1 The ASCII value of secondChar is 49
Program Example #include <stdio.h> void main() { int firstInt; float secondFloat; firstInt = 9.8; printf(“Value of firstInt is %d\n”, firstInt); secondFloat = 123.4567890123456789; printf(“Value of secondFloat is %f\n”, secondFloat); fflush(stdin); getchar(); } Value of firstInt is 9 Value of secondFloat is 123.456787
Program Example #include <stdio.h> void main() { float lowFloat; double doubleFloat; lowFloat = 123.4567890123456789; doubleFloat = 123.4567890123456789; printf(“Value of lowFloat is %.16f\n”, lowFloat); printf(“Value of doubleFloat is %.16f\n”, doubleFloat); fflush(stdin); getchar(); } Value of lowFloat is 123.4567871093750000 Value of doubleFloat is 123.4567890123456806
Program Example #include <stdio.h> void main() { int anInt = 123456789012345; printf(“Value of anInt is %d\n”, anInt); fflush(stdin); getchar(); } Value of anInt is -2045911175
Program Example #include <stdio.h> #include <limits.h> void main() { printf(“Maximum value of char = %d\n”, CHAR_MAX); printf(“Minimum value of char = %d\n”, CHAR_MIN); printf(“Maximum value of int = %d\n”, INT_MAX); printf(“Minimum value of int = %d\n”, INT_MIN); printf(“Maximum value of long = %d\n”, LONG_MAX); printf(“Minimum value of long = %d\n”, 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
Program Example #include <stdio.h> #include <float.h> void main() { printf(“Precision of float = %.16f\n”, FLT_EPSILON); printf(“Precision of double = %.16f\n”, DBL_EPSILON); printf(“Maximum value of float = %e\n”, FLT_MAX); printf(“Minimum value of float = %e\n”, FLT_MIN); printf(“Maximum value of double = %e\n”, DBL_MAX); printf(“Minimum value of double = %e\n”, DBL_MIN); fflush(stdin); getchar(); } Precision of float = 0.0000001192092896 Precision of double = 0.0000000000000002 Maximum value of float = 3.402823e+38 Minimum value of float = 1.175494e-38 Maximum value of double = 1.797693e+308 Minimum value of double = 2.225074e-308
Output • printf • function - writes formatted output to stdout • syntax - #include<stdio.h> • printf(const char*format[,argument,….]) • some input argument • %d - int • %f - float and double • %e - prints in exponential format, eg 1.23e5 • %c – ASCII standard
Output • Example of printf • #include <stdio.h> • int main(void) • { • int x; • x = 3; • printf(“Variable x is assigned as %d\n”,x); • return 0; • }
Output (Holding the screen) • getch • Function : gets character from keyboard, does not echo to screen. • Syntax : #include <conio.h> • int getch(void); • getchar • Function : gets character from stdin. • Syntax : #include <stdio.h> • int getchar(void); • fflush • syntax : #include <stdio.h> • int fflush(FILE *stream) • eg. fflush(stdin)
Output (Holding the screen) • example : • #include <stdio.h> • int main(void) • { int x; • printf(“please enter x: “); • scanf (“%d”,&x); • fflush(stdin); • printf(“The number is %d\n”,x); • getchar(); • return 0; • }
Output • Escape sequence • the backslash character (\) is used to introduce an escape sequence, allowing the visual representation of certain nongraphic characters.
Output • 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 • \0 null value
Program Example #include <stdio.h> void main() { printf(“Hello. I am Cee. \n”); printf(“Nice to show you how output is done in C\n”); } Hello. I am Cee. Nice to show you how output is done in C
Program Example #include <stdio.h> void main() { printf(“My personal information\n”); printf(“Name\t\t: Cee\n”); printf(“Sex\t\t: Computer is asexual\n”); printf(“Age\t\t: Modern\n”); printf(“Interests\t: Playing CDs, talking to printers, defragmentation\n”); fflush(stdin); getchar(); } My personal information Name : Cee Sex : Computer is asexual Age : Modern Interests : Playing CDs, talking to printers, defragmentation
Input • scanf • function - scans and formats input from the stdin stream • syntax - #include<stdio.h> • scanf(const char*format[,address,….]) • some input argument • %d • %f • %c
Input • Example of scanf • #include <stdio.h> • int main(void) • { • int x; • printf(“Please enter an integer\n :”); • scanf(“%d”,&x); • printf(“Variable x is assigned as %d\n”,x); • return 0; • }
Program Example #include <stdio.h> void main() { char aChar; printf(“Enter a characters: “); aChar = getchar(); printf(“The character entered is %c\n”, aChar); printf(“Enter a characters: “); aChar = getchar(); printf(“The character entered is %c\n”, aChar); printf(“Press Enter to bye\n”); fflush(stdin); getchar(); } Enter a characters: C The character entered is C Enter a characters: The character entered is Press Enter to bye