1 mt258 computer programming and problem solving unit 1

54
1 MT258 Computer Programming and Problem Solving Unit 1

Upload: leon-patterson

Post on 30-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 1 MT258 Computer Programming and Problem Solving Unit 1

1

MT258

Computer Programming and

Problem Solving

Unit 1

Page 2: 1 MT258 Computer Programming and Problem Solving Unit 1

2

Important points

Method for calculating the final grade.

The procedure for requesting of extensions. The procedure for handing in TMAs.

Programming style.

Page 3: 1 MT258 Computer Programming and Problem Solving Unit 1

3

Contents

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

Page 4: 1 MT258 Computer Programming and Problem Solving Unit 1

4

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

Page 5: 1 MT258 Computer Programming and Problem Solving Unit 1

5

Study Material

Course Material (Unit 1 - 9)

(Blue Books)

Text Book• C How to Program

Page 6: 1 MT258 Computer Programming and Problem Solving Unit 1

6

TEXT BOOK

Deitel/Deitel

C HOW TO PROGRAM

Prentice Hall

Page 7: 1 MT258 Computer Programming and Problem Solving Unit 1

7

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

Page 8: 1 MT258 Computer Programming and Problem Solving Unit 1

8

Reference Book

Herbert Schildt

The Complete Reference

(3rd Edition)

Osborne McGraw-Hill

Page 9: 1 MT258 Computer Programming and Problem Solving Unit 1

9

Reference Book (recommended by Celia)

Brian W. Kernighan, Dennis M. Ritchie

The C Programming Language

Prentice Hall Software Series

Page 10: 1 MT258 Computer Programming and Problem Solving Unit 1

10

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

Page 11: 1 MT258 Computer Programming and Problem Solving Unit 1

11

Online Reference

OLE• Discussion board

Page 12: 1 MT258 Computer Programming and Problem Solving Unit 1

12

How to Success

Follow course schedule Fully understand

• Blue books Submit TMA on time Study TMA solution

Page 13: 1 MT258 Computer Programming and Problem Solving Unit 1

13

UNIT ONE

Page 14: 1 MT258 Computer Programming and Problem Solving Unit 1

14

History of Programming Language and C

Page 15: 1 MT258 Computer Programming and Problem Solving Unit 1

15

Writing Your First Prgram

Body

Page 16: 1 MT258 Computer Programming and Problem Solving Unit 1

16

First Program#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 lif

e. :) \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

Body

Semicolon

Page 17: 1 MT258 Computer Programming and Problem Solving Unit 1

17

Second Program#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: 20Oh! You are only 20Very young! ;)You may press Enter to bye

declares a variable

input statement

output statement

Page 18: 1 MT258 Computer Programming and Problem Solving Unit 1

18

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.

Page 19: 1 MT258 Computer Programming and Problem Solving Unit 1

19

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.

Page 20: 1 MT258 Computer Programming and Problem Solving Unit 1

20

Variables

The declaration has two parts.

• The type of the variable.

• The name that identifies the variable – Identifier.

Page 21: 1 MT258 Computer Programming and Problem Solving Unit 1

21

Data types char short int unsigned int int unsigned long long float double long double

no string no Boolean

Page 22: 1 MT258 Computer Programming and Problem Solving Unit 1

22

Activity 1

Name any 4 fundamental data types of C.

Page 23: 1 MT258 Computer Programming and Problem Solving Unit 1

23

Activity 1

Name any 4 fundamental data types of C.

• int• float• double• char

Page 24: 1 MT258 Computer Programming and Problem Solving Unit 1

24

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.

Page 25: 1 MT258 Computer Programming and Problem Solving Unit 1

25

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.

Page 26: 1 MT258 Computer Programming and Problem Solving Unit 1

26

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

Page 27: 1 MT258 Computer Programming and Problem Solving Unit 1

27

Activity 2 Which of the following variable identifiers

are invalid ?• const

• a*

• xchar

• b_

• cont

• 3phases

• enum

• num

• profit+tax

• NuMbEr

Page 28: 1 MT258 Computer Programming and Problem Solving Unit 1

28

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)

Page 29: 1 MT258 Computer Programming and Problem Solving Unit 1

29

Using variables in your programs

declaring a variable of a certain type setting a value to a variable getting the value from a variable

Page 30: 1 MT258 Computer Programming and Problem Solving Unit 1

30

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";

Page 31: 1 MT258 Computer Programming and Problem Solving Unit 1

31

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.

Page 32: 1 MT258 Computer Programming and Problem Solving Unit 1

32

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 BThe ASCII value of aChar is 66The value of ‘B’ is BThe ASCII value of ‘B’ is 66

Page 33: 1 MT258 Computer Programming and Problem Solving Unit 1

33

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”, a

Char); fflush(stdin); getchar();}

The value of aChar is BThe ASCII value of aChar is 66

Page 34: 1 MT258 Computer Programming and Problem Solving Unit 1

34

Program Example#include <stdio.h>void main() { char firstChar = ‘1’; char secondChar; secondChar = firstChar; printf(“The value of secondChar is %c\n”, secondCha

r); printf(“The ASCII value of secondChar is %d\n”, secon

dChar); fflush(stdin); getchar();}

The value of secondChar is 1The ASCII value of secondChar is 49

Page 35: 1 MT258 Computer Programming and Problem Solving Unit 1

35

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”, secondFlo

at); fflush(stdin); getchar();}

Value of firstInt is 9Value of secondFloat is 123.456787

Page 36: 1 MT258 Computer Programming and Problem Solving Unit 1

36

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”, double

Float); fflush(stdin); getchar();}

Value of lowFloat is 123.4567871093750000Value of doubleFloat is 123.4567890123456806

Page 37: 1 MT258 Computer Programming and Problem Solving Unit 1

37

Program Example

#include <stdio.h>void main() { int anInt = 123456789012345; printf(“Value of anInt is %d\n”, anIn

t); fflush(stdin); getchar();}

Value of anInt is -2045911175

Page 38: 1 MT258 Computer Programming and Problem Solving Unit 1

38

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 = 255Minimum value of char = 0Maximum value of int = 2147483647Minimum value of int = -2147483648Maximum value of long = 2147483647Minimum value of long = -2147483648

Page 39: 1 MT258 Computer Programming and Problem Solving Unit 1

39

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.0000001192092896Precision of double = 0.0000000000000002Maximum value of float = 3.402823e+38Minimum value of float = 1.175494e-38Maximum value of double = 1.797693e+308Minimum value of double = 2.225074e-308

Page 40: 1 MT258 Computer Programming and Problem Solving Unit 1

40

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

Page 41: 1 MT258 Computer Programming and Problem Solving Unit 1

41

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;• }•

Page 42: 1 MT258 Computer Programming and Problem Solving Unit 1

42

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)

Page 43: 1 MT258 Computer Programming and Problem Solving Unit 1

43

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;

• }

Page 44: 1 MT258 Computer Programming and Problem Solving Unit 1

44

Output

Escape sequence• the backslash character (\) is used to introduce a

n escape sequence, allowing the visual representation of certain nongraphic characters.

Page 45: 1 MT258 Computer Programming and Problem Solving Unit 1

45

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

Page 46: 1 MT258 Computer Programming and Problem Solving Unit 1

46

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

Page 47: 1 MT258 Computer Programming and Problem Solving Unit 1

47

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 informationName : CeeSex : Computer is asexualAge : ModernInterests : Playing CDs, talking to printers, defragmentation

Page 48: 1 MT258 Computer Programming and Problem Solving Unit 1

48

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

Page 49: 1 MT258 Computer Programming and Problem Solving Unit 1

49

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;• }

Page 50: 1 MT258 Computer Programming and Problem Solving Unit 1

50

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: CThe character entered is CEnter a characters:The character entered is

Press Enter to bye

Page 51: 1 MT258 Computer Programming and Problem Solving Unit 1

51

Program Example#include <stdio.h>

void main() {

int anInt;

float aFloat;

char aChar;

printf(“Enter an integer, a floating-point number, and a character: “);

scanf(“%d”, &anInt);

scanf(“%f”, &aFloat);

scanf(“%c”, &aChar);

printf(“Entered data includes %d %f \”%c\”\n”, anInt, aFloat, aChar);

fflush(stdin);

getchar();

} Enter an integer, a floating point number, and a character: 10 2.34 C

Entered data includes 10 2.340000 “ ” Enter an integer, a floating point number, and a character: 10 2.34

CEntered data includes 10 2.340000 “C”

Enter an integer, a floating point number, and a character: 102.34CEntered data includes 102 0.340000 “C”

Page 52: 1 MT258 Computer Programming and Problem Solving Unit 1

52

Header files

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.

Page 53: 1 MT258 Computer Programming and Problem Solving Unit 1

53

Header files math.h - declares prototypes for the math f

unctions. (eg. pow) stdio.h - defines types and macros needed t

o standard I/O package. (eg. printf) time.h - defines a structure filled in by the

time-conversion routines. (eg. time) string.h - declares several string-manipulati

on and memory-manipulation routines. (eg. strlen)

Page 54: 1 MT258 Computer Programming and Problem Solving Unit 1

54

Activity 3 Identify and correct the error. 1) printf (“The value is %d\n”, &number1);

no & is needed 2) scanf (“%d%d”, &num1, num2);

if (num3 < 7 );

printf (“num3 is less than 7\n”)• first line : &num2 should be used • second line : delete ; at the end• third line : ; is missing