tailieunhanh - Lecture Introduction to computer and programming - Lecture No 17

The contents of this chapter include all of the following: Define virtual memory; describe the hardware and control structures that support virtual memory; describe the various OS mechanisms used to implement virtual memory; describe the virtual memory management mechanisms in UNIX, Linux, and Windows 7. | CSC103: Introduction to Computer and Programming Lecture No 17 Previous lecture Function call by address or pointer Function return value Use pointers to get results in calling function from called function Today’s lecture outline Recursive functions Introduction to Array Accessing elements of array A simple array program Write a program Recursive function In C, it is possible for the functions to call themselves A function is called ‘recursive’ if a statement within the body of a function calls the same function Example program - factorial Go to program Example program – factorial using recursion Go to program Cont. Cont. main() { . . fact = rec(3); printf ( "%d ", fact); } rec ( 3 ) { int f ; if ( 3 == 1 ) return ( 1 ) ; else f = 3 * rec ( 3 - 1 ) ; return ( f ) ; } false rec ( 2 ) { int f ; if ( 2 == 1 ) return ( 1 ) ; else f = 2 * rec ( 2 - 1 ) ; return ( f ) ; } rec ( 1 ) { int f ; if ( 1 == 1 ) return ( 1 ) ; else f = 2 * rec ( 2 - 1 ) ; return ( f ) ; } false true f = 2 * 1; f = 2 f = 3 * 2; f = 6 fact = 6 Go to program Example program 2 Write a definition of a function that adds n integers using recursion and then return the sum. Prototype of the function is below int sum_number(int); Write a program Array Offers a simple way of grouping like variables for easy access It is a group of elements having same data type An array is a collective name given to a group of ‘similar quantities’ Arrays in C share a few common attributes Variables in an array share the same name Variables in an array share the same data type Individual variables in an array are called elements Elements in an array are accessed with an index number Cont. Ordinary variables are capable of holding only one value at a time There are situations in which we would want to store more than one value at a time in a single variable Cont. For example, suppose we want to arrange the percentage marks obtained by 100 students in ascending order In such a case we have two options to store these . | CSC103: Introduction to Computer and Programming Lecture No 17 Previous lecture Function call by address or pointer Function return value Use pointers to get results in calling function from called function Today’s lecture outline Recursive functions Introduction to Array Accessing elements of array A simple array program Write a program Recursive function In C, it is possible for the functions to call themselves A function is called ‘recursive’ if a statement within the body of a function calls the same function Example program - factorial Go to program Example program – factorial using recursion Go to program Cont. Cont. main() { . . fact = rec(3); printf ( "%d ", fact); } rec ( 3 ) { int f ; if ( 3 == 1 ) return ( 1 ) ; else f = 3 * rec ( 3 - 1 ) ; return ( f ) ; } false rec ( 2 ) { int f ; if ( 2 == 1 ) return ( 1 ) ; else f = 2 * rec ( 2 - 1 ) ; return ( f ) ; } rec ( 1 ) { int f ; if ( 1 == 1 ) return ( 1 ) ; else f = 2 * rec ( 2 - 1 ) ; return ( f ) ; } false true f = 2 * 1;

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.