Download presentation
Presentation is loading. Please wait.
Published byBeatrix Lee Bruce Modified over 9 years ago
1
1 MT258 Computer Programming and Problem Solving Tutorial 03
2
2 UNIT THREE
3
3 C programs and computer systems w Driver w System software w Software libraries
4
4 C programs and computer systems
5
5 Pre-processing Directives w # include command instructs the pre-processing stage to insert a file into the source file Example #include 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 check whether they are used correctly.
6
6 Pre-processing Directives w # 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. Macr o Text (no ;)
7
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. Macr o Text Macr o Text
8
8 Commenting in C programs w Comments contain text that may describe the purposes of a program, the roles of variables, the operation of a section of code. w Comments are completely ignored by the compiler. w Comments in C are text enclosed by the symbols /* and */. Anything can appear between this pair including newling characters. w Please refer to the examples in the “Tutorial Notes on C Programming Style Guide”
9
9 Values and Variables Floating point type - Fractional part - Exponent part Primitive Type Integral Type Floating Point Type charint long short floatdouble - Integral type –Store digits Primitive type
10
10 Values and Variables w Some examples of conversion character %ccharacter %dsigned integer %ffloating point %eexponential %sstring w Example printf (“%d, %e”, variable1, variable2);
11
11 Range & Precision TypeMinimumMaximumPrecisionBytes char 0 2 8 - 1 1 1 short -2 15 2 15 - 1 1 2 int -2 31 2 31 - 1 1 4 long -2 31 2 31 - 1 1 4 float 1.175494e -38 3.402823e 38 1.19209289551e -7 4 double2.225074e -308 1.797693e 308 2.22e -16 8
12
12 Range & Precision Type Minimum Maximum Precision char CHAR_MIN CHAR_MAX 1 short SHRT_MIN SHRT_MAX 1 int INT_MIN INT_MAX 1 long LONG_MIN LONG_MAX 1 float FLT_MIN FLT_MAX FLT_EPSILON double DBL_MIN DBL_MAX DBL_EPSILON signed charSCHAR_MINSCHAR_MAX 1 unsigned shortUSHRT_MINUSHRT_MAX1 unsigned intUINT_MINUINT_MAX1 unsigned longULONG_MINULONG_MAX1
13
13 Range & Precision w Defined in limits.h & float.h w Report range limits of computer system w Different from computer to computer w Constant definitions w Sign signed+ / - unsigned+ only w Size measurement sizeof( )
14
14 Range & Precision w Impact on greater range & precision The program uses up more computer more memory The program requires more processing time w Types Floating point types requires longer processing time than integral types
15
15 Integer and floating-point division w Integer Division If both operands are integer values, it is integer division. The result is the integer part of the original result. w Floating Point Division If either one of the operands is float or double, it is floating point division. The result is floating point type. w Type Casting Result = (float) 5 / 2;
16
16 Array w An array is an orderly arrangement of items. w It consists of a fixed number of elements. w The elements are all of the same base type under the same name in programming. w Start at index of 0 w Syntax : type identifier[number of elements]; example float Prices[25];
17
17 Declaring Arrays w When declaring arrays, specify Name Type of array Number of elements arrayType arrayName[ numberOfElements ]; Examples: int c[ 10 ]; float myArray[ 3284 ]; w Declaring multiple arrays of same type Format similar to regular variables Example: int b[ 100 ], x[ 27 ];
18
18 Array w Array elements are like normal variables c[ 0 ] = 3; printf( "%d", c[ 0 ] ); Perform operations in subscript. If x equals 3 c[ 5 - 2 ] == c[ 3 ] == c[ x ]
19
19 Arrays w Operations each elements depending on type of elements whole array no operations no assignment no comparison
20
20 Examples Using Arrays w We could give values during declaration. Example : int cat[5] = {5,63,175,103,10}; w We can also initialize all cells to the same value. Example : Float vec[7] = 0.0; w If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; w Example of array initialisation int j; for (j=0; j<10; j++) intArray[j] = 0;
21
21 Array (activity) w For each value, declare a variable in C and initialize the variables : w i) ABC w ii) 13 w iii) 30 Good Shepherd St. w iv) 6.02 x 10 23 w v) 3.14159
22
22 Array (activity) w For each value, declare a variable in C and initialize the variables : w i) ABC char aString[] =“ABC”; w ii) 13 int x = 13; w iii) 30 Good Shepherd St. char aString[] = “30 Good Shepherd St. “; w iv) 6.02 x 10 23 float cons = 6.02e23; w v) 3.14159 float pi=3.14159;
23
23 Examples Using Arrays w Print the values of all the array int j; for (j=0; j<10; j++) printf(“%d “, intArray[j]); w Sum of all the array elements int j; for (j=0; j<10; j++) sum= sum + intArray[j];
24
24 Two Dimensional Arrays w Syntax : Type Arrayname[FirstIndexSize][SecondIndexSize] w Example char page[100][100]; w Please refer to the example in page 55 of unit 3.
25
25 Pointer w 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.
26
26 Declaration of pointer : w int *a; w char *a; w float *a; w double *a; w a is an address pointer pointing to its data location
27
27 Declaration of pointer : w The below two declaration are the same. w int* a;
28
28 Declaration of pointer : w Please refer to examples in page 49-50 in unit 3.
29
29 Activity 1 w Draw a memory location table for the following declarations and statements. w float x=20; w float *y, *z; w y= &x; w z=&x; w *y=30.5; w *z=40.5; w What is the content of x?
30
30 Activity 1 w Draw a memory location table for the following declarations and statements. w float x=20; w float *y, *z; w y= &x; w z=&x; w *y=30.5; w *z=40.5; w What is the content of x? w Awser : 40.5
31
31 Meaning of *, & and NULL w * means as at address w & means as the address of w NULL is defined in the stdio.h with a value of 0, it indicates that the pointer is not currently pointing at a location.
32
32 Activity 2 w Suppose a program contains the following statements: intnum, *ptr; w 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.
33
33 Activity 2 w Answers : w (i)printf("the address of num is %d\n",&num); w (ii)printf("num = %d\n", num); w (iii) printf("the address of ptr is %d\n",&ptr); w (iv) printf("the address of the location to which the ptr w points %d\n ", ptr); w (v) printf("ptr points to value : %d", *ptr);
34
34 Activity 3 w Assume we declare an array as follows: w short int array[] = {3, 1997, 2000, 1776, 12888, 1, 9999} w 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)
35
35 Activity 3 w (i)1776 w (ii)1004 w (iii)1000 w (iv)3 w (v)2000
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.