1 mt258 computer programming and problem solving tutorial 03

35
1 MT258 Computer Programming and Problem Solving Tutorial 03

Upload: beatrix-lee-bruce

Post on 18-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

3 C programs and computer systems w Driver w System software w Software libraries

TRANSCRIPT

Page 1: 1 MT258 Computer Programming and Problem Solving Tutorial 03

1

MT258

Computer Programming and

Problem Solving

Tutorial 03

Page 2: 1 MT258 Computer Programming and Problem Solving Tutorial 03

2

UNIT THREE

Page 3: 1 MT258 Computer Programming and Problem Solving Tutorial 03

3

C programs and computer systems

Driver

System software

Software libraries

Page 4: 1 MT258 Computer Programming and Problem Solving Tutorial 03

4

C programs and computer systems

Page 5: 1 MT258 Computer Programming and Problem Solving Tutorial 03

5

Pre-processing Directives # include

• command instructs the pre-processing stage to insert a file into the source file

• Example #include <stdio.h>

• This is a header file contains information of standard input and output library.

• It tells how the concerned functions should be used.• At compilation stage, this information is used to ch

eck whether they are used correctly.

Page 6: 1 MT258 Computer Programming and Problem Solving Tutorial 03

6

Pre-processing Directives

# define• Constant value unconditionally modifying program code

• Example #define rate 20

• It replaces all occurrences of the macro name in the

program with the specified text.

Macro

Text (no ;)

Page 7: 1 MT258 Computer Programming and Problem Solving Tutorial 03

7

Pre-processing Directives

• Example #define HI "Have a nice day!"

• It replaces all occurrences of the macro name in the program with the specified text.

• Example #define RECTANGLE_AREA(x, y) ( (x) * (y) )

• rectArea = RECTANGLE_AREA(a+4, b+7) ;

rectArea = ( (a+4) * (b+7) ) ;• It replaces all occurrences of the macro name in the

program with the specified text.

Macro

TextMacro

Text

Page 8: 1 MT258 Computer Programming and Problem Solving Tutorial 03

8

Commenting in C programs Comments contain text that may describe

• the purposes of a program, • the roles of variables, • the operation of a section of code.

Comments are completely ignored by the compiler.

Comments in C are text enclosed by the symbols /* and */. Anything can appear between this pair including newling characters.

Please refer to the examples in the “Tutorial Notes on C Programming Style Guide”

Page 9: 1 MT258 Computer Programming and Problem Solving Tutorial 03

9

Values and Variables

• Floating point type- Fractional part- Exponent part

Primitive Type

Integral Type Floating Point Type

char int longshort float double

- Integral type– Store digits

• Primitive type

Page 10: 1 MT258 Computer Programming and Problem Solving Tutorial 03

10

Values and Variables Some examples of conversion character

• %c character• %d signed integer• %f floating point• %e exponential• %s string

Example• printf (“%d, %e”, variable1, variable2);

Page 11: 1 MT258 Computer Programming and Problem Solving Tutorial 03

11

Range & Precision

Type Minimum Maximum Precision Bytes

char 0 28 - 1 1 1short -215 215 - 1 1 2int -231 231 - 1 1 4long -231 231 - 1 1 4

float 1.175494e-38 3.402823e38 1.19209289551e-7 4double 2.225074e-308 1.797693e308 2.22e-16 8

Page 12: 1 MT258 Computer Programming and Problem Solving Tutorial 03

12

Range & Precision

Type Minimum Maximum Precisionchar CHAR_MIN CHAR_MAX 1short SHRT_MIN SHRT_MAX 1int INT_MIN INT_MAX 1long LONG_MIN LONG_MAX 1

float FLT_MIN FLT_MAX FLT_EPSILONdouble DBL_MIN DBL_MAX DBL_EPSILON

signed charSCHAR_MIN SCHAR_MAX 1unsigned short USHRT_MIN USHRT_MAX 1unsigned int UINT_MIN UINT_MAX 1unsigned long ULONG_MIN ULONG_MAX 1

Page 13: 1 MT258 Computer Programming and Problem Solving Tutorial 03

13

Range & Precision Defined in limits.h & float.h Report range limits of computer system Different from computer to computer Constant definitions

Sign• signed + / - • unsigned + only

Size measurement• sizeof( )

Page 14: 1 MT258 Computer Programming and Problem Solving Tutorial 03

14

Range & Precision Impact on greater range & precision

• The program uses up more computer more memory

• The program requires more processing time

Types• Floating point types requires longer

processing time than integral types

Page 15: 1 MT258 Computer Programming and Problem Solving Tutorial 03

15

Integer and floating-point division

Integer Division • If both operands are integer values, it is integer

division. • The result is the integer part of the original result.

Floating Point Division• If either one of the operands is float or double, it is

floating point division.• The result is floating point type.

Type Casting• Result = (float) 5 / 2;

Page 16: 1 MT258 Computer Programming and Problem Solving Tutorial 03

16

Array An array is an orderly arrangement of items. It consists of a fixed number of elements. The elements are all of the same base type under

the same name in programming. Start at index of 0 Syntax :

• type identifier[number of elements];• example• float Prices[25];

Page 17: 1 MT258 Computer Programming and Problem Solving Tutorial 03

17

Declaring Arrays When declaring arrays, specify

• Name• Type of array• Number of elements

arrayType arrayName[ numberOfElements ];

• Examples:int c[ 10 ]; float myArray[ 3284 ];

Declaring multiple arrays of same type• Format similar to regular variables• Example:

int b[ 100 ], x[ 27 ];

Page 18: 1 MT258 Computer Programming and Problem Solving Tutorial 03

18

Array

Array elements are like normal variablesc[ 0 ] = 3;printf( "%d", c[ 0 ] );

• Perform operations in subscript. If x equals 3c[ 5 - 2 ] == c[ 3 ] == c[ x ]

Page 19: 1 MT258 Computer Programming and Problem Solving Tutorial 03

19

Arrays

Operations• each elements

• depending on type of elements• whole array

• no operations• no assignment• no comparison

Page 20: 1 MT258 Computer Programming and Problem Solving Tutorial 03

20

Examples Using Arrays We could give values during declaration.

• Example :• int cat[5] = {5,63,175,103,10};

We can also initialize all cells to the same value.• Example :

• Float vec[7] = 0.0; If size omitted, initializers determine it

• int n[ ] = { 1, 2, 3, 4, 5 };

Example of array initialisation• int j;• for (j=0; j<10; j++)• intArray[j] = 0;

Page 21: 1 MT258 Computer Programming and Problem Solving Tutorial 03

21

Array (activity)

For each value, declare a variable in C and initialize the variables :

i) ABC ii) 13 iii) 30 Good Shepherd St. iv) 6.02 x 1023

v) 3.14159

Page 22: 1 MT258 Computer Programming and Problem Solving Tutorial 03

22

Array (activity) For each value, declare a variable in C and initialize the

variables : i) ABC

• char aString[] =“ABC”; ii) 13

• int x = 13; iii) 30 Good Shepherd St.

• char aString[] = “30 Good Shepherd St. “; iv) 6.02 x 1023

• float cons = 6.02e23; v) 3.14159

• float pi=3.14159;

Page 23: 1 MT258 Computer Programming and Problem Solving Tutorial 03

23

Examples Using Arrays

Print the values of all the array• int j;• for (j=0; j<10; j++)• printf(“%d “, intArray[j]);

Sum of all the array elements• int j;• for (j=0; j<10; j++)• sum= sum + intArray[j];

Page 24: 1 MT258 Computer Programming and Problem Solving Tutorial 03

24

Two Dimensional Arrays Syntax :

Type Arrayname[FirstIndexSize][SecondIndexSize]

Example char page[100][100];

Please refer to the example in page 55 of unit 3.

Page 25: 1 MT258 Computer Programming and Problem Solving Tutorial 03

25

Pointer

Definition of pointer• A pointer is a memory cell, it’s content is used

for storing an address of another memory cell and not for storing actual data.

Page 26: 1 MT258 Computer Programming and Problem Solving Tutorial 03

26

Declaration of pointer : int *a; char *a; float *a; double *a;

a is an address pointer pointing to its data location

Page 27: 1 MT258 Computer Programming and Problem Solving Tutorial 03

27

Declaration of pointer :

The below two declaration are the same.

int* a; int *a;

Page 28: 1 MT258 Computer Programming and Problem Solving Tutorial 03

28

Declaration of pointer :

Please refer to examples in page 49-50 in unit 3.

Page 29: 1 MT258 Computer Programming and Problem Solving Tutorial 03

29

Activity 1 Draw a memory location table for the following

declarations and statements. float x=20; float *y, *z; y= &x; z=&x; *y=30.5; *z=40.5; What is the content of x?

Page 30: 1 MT258 Computer Programming and Problem Solving Tutorial 03

30

Activity 1 Draw a memory location table for the following de

clarations and statements. float x=20; float *y, *z; y= &x; z=&x; *y=30.5; *z=40.5; What is the content of x?

Awser : 40.5

Page 31: 1 MT258 Computer Programming and Problem Solving Tutorial 03

31

Meaning of * , & and NULL

* means as at address & means as the address of

NULL is defined in the stdio.h with a value of 0, it indicates that the pointer is not currently pointing at a location .

Page 32: 1 MT258 Computer Programming and Problem Solving Tutorial 03

32

Activity 2 Suppose a program contains the following st

atements:•int num, *ptr;

Write statements to print each of the following:

• .the address of num;• .the content of num;• .the address of ptr;• the address of the location to which the ptr points• .the integer value to which the ptr points.

Page 33: 1 MT258 Computer Programming and Problem Solving Tutorial 03

33

Activity 2

Answers :

(i) printf("the address of num is %d\n",&num); (ii) printf("num = %d\n", num); (iii) printf("the address of ptr is %d\n",&ptr); (iv) printf("the address of the location to which the ptr points %d\n ", ptr); (v) printf("ptr points to value : %d", *ptr);

Page 34: 1 MT258 Computer Programming and Problem Solving Tutorial 03

34

Activity 3 Assume we declare an array as follows: short int array[] = {3, 1997, 2000, 1776, 12888, 1, 9999}

If array is stored at memory location 1000, what is the value of each of the expression below:

• array[3]• &array[2]• array• *array• *(array + 2)

Page 35: 1 MT258 Computer Programming and Problem Solving Tutorial 03

35

Activity 3

(i) 1776 (ii) 1004 (iii)1000 (iv)3 (v) 2000