tailieunhanh - Lecture Introduction to computer and programming - Lecture No 18
The contents of this chapter include all of the following: Recursive functions, introduction to array, accessing elements of array, a simple array program, a program to calculate percentage marks, array initialization, bound checking, passing array element to a function, pointer arithmetic. | CSC103: Introduction to Computer and Programming Lecture No 18 Previous lecture Recursive functions Introduction to Array Accessing elements of array A simple array program Today’s lecture outline A program to calculate percentage marks Array initialization Bound checking Passing array element to a function Pointer arithmetic Marks program Go to program Value and address of array elements Write a program Array initialization Bounds Checking In C there is no check to see if the subscript used for an array exceeds the size of the array Data entered with a subscript exceeding the array size will simply be placed in memory outside the array To see to it that we do not reach beyond the array size is entirely the programmer’s botheration and not the compiler’s 0 1 2 3 4 5 6 7 8 9 500 504 508 512 516 520 524 528 532 530 Passing Array Elements to a Function Array elements can be passed to a function by calling the function by value, or by address In the call by value we pass values of array elements to the function In the call by reference we pass addresses of array elements to the function Value of array element pass to a function 55 65 75 56 78 78 90 0 1 2 3 4 5 6 500 504 508 512 516 520 524 i = 0 i = 1 display(marks[o]) display(55) Program output 55 display(marks[1]) display(65) 65 i = 2 display(marks[2]) display(75) 75 i = 3 i =4 i = 5 i = 6 display(marks[3]) display(56) 56 display(marks[4]) display(78) 78 display(marks[5]) display(78) 78 i = 7 display(marks[6]) display(90) 90 Address of array element pass to a function 55 65 75 56 78 78 90 0 1 2 3 4 5 6 500 504 508 512 516 520 524 i = 0 i = 1 display(&marks[o]) display(500) Program output 55 display(&marks[1]) display(504) 65 i = 2 display(&marks[2]) display(508) 75 i = 3 i =4 i = 5 i = 6 display(&marks[3]) display(512) 56 display(&marks[4]) display(516) 78 display(&marks[5]) display(520) 78 i = 7 display(&marks[6]) display(524) 90 *n 500 *(500) = 55 *n *(504) = 65 504 *n *(508) = 75 508 *n 512 *(512) = 56 *n 516 . | CSC103: Introduction to Computer and Programming Lecture No 18 Previous lecture Recursive functions Introduction to Array Accessing elements of array A simple array program Today’s lecture outline A program to calculate percentage marks Array initialization Bound checking Passing array element to a function Pointer arithmetic Marks program Go to program Value and address of array elements Write a program Array initialization Bounds Checking In C there is no check to see if the subscript used for an array exceeds the size of the array Data entered with a subscript exceeding the array size will simply be placed in memory outside the array To see to it that we do not reach beyond the array size is entirely the programmer’s botheration and not the compiler’s 0 1 2 3 4 5 6 7 8 9 500 504 508 512 516 520 524 528 532 530 Passing Array Elements to a Function Array elements can be passed to a function by calling the function by value, or by address In the call by value we pass values of array .
đang nạp các trang xem trước