Vous êtes sur la page 1sur 10

LESSON 1 FUNCTION

Lesson Objectives: 1. learn the advantages of using Function; 2. differentiate the storage classes; and 3. differentiate the call by value and call by reference.

STORAGE CLASSES 2 different ways to characterized variables: 1. By data type refers to the type of information represented by a variable. 2. By storage class refers to the permanence of a variable and its scope within the program that is the portion of the program over which the variable is recognized. 4 specifications of storage-class in C: 1. 2. 3. 4. Automatic variables or Auto External variables or Extern Static variables Registers

AUTOMATIC VARIABLES Always declared within a function and are local to the function in which they are declared. Even if they have the same name, the variables are independent to one another. Any variable declared within a function is considered as Automatic variables unless a different storage class specification is included within the declaration. The keyword AUTO is NOT required during variable declaration. Does not retain its value once the control is transferred out of its defining function.

EXTERNAL VARIABLES Variables that are NOT confined to a single functions therefore, they are recognized in the entire program. Usually span two or more functions, and often an entire program. They retain their assigned values within this scope.

STATIC VARIABLES 2 important and distinct uses: 1. To allow local variable to retain its previous value when the block is re-entered. 2. Use in connection with external declarations. Can be utilized within the function but cannot be accessed outside of their defining function.

REGISTER VARIABLES Tells the compiler that the associated variables should be stored in high-speed memory registers, provided it is physically and semantically possible. Defaults to automatic whenever the compiler cannot allocated an appropriate physical registers. The use of Storage class registers is an attempt to improve execution speed.

SAMPLE PROGRAM WITH EXTERN AND AUTO: #include<stdio.h> #include<conio.h> int a=3; int funct1(int count) { for(count=1; count<=5; ++count) { a=a+count; printf("%d ", a); } return 0; } void main() { clrscr(); int count; funct1(count); getche(); }

SWAPPING (Page 18) An important technique in solving programming problems where the principle is assigning the value of variable A to the value of variable B and vice-versa. 3 complex process stage in Swapping: 1. Put the value of variable A into variable Temp. 2. Put the value of variable B into variable A. 3. Put the value of variable Temp into variable b.

SAMPLE PROGRAM FOR SWAPPING #include<stdio.h> #include<conio.h> void swap(int *x, int *y); void main() { clrscr(); int a=3, b=5; puts("Before"); printf("a is %d and b is %d.", a, b); swap(&a, &b); puts("After"); printf("a is %d and b is %d.", a, b); getch(); return; } void swap(int *x, int *y) { int z; z=*x; *x=*y; *y=z; }

LESSON 2 ARRAYS
(page 27) Lesson Objectives: 1. learn the details of the basic concepts of Arrays; 2. to be familiar with the different uses of Arrays; and 3. to apply the use of arrays in some complicated applications.

ARRAY To process large amounts of data, we need a powerful data structure such as ARRAY. Characteristics of Array: 1. Is a fixed size, sequenced collection of elements of the same data type. 2. Is a sequence of data items that are of the same type, indexible, and that are stored contiguously. 3. Data type that is used to represent a large number of homogeneous values. We refer to the elements of the arrays as first element, second element and so forth. We designate the first element as Number0, the second element as Number1 and so on. Loops are programming constructs that makes array processing easy. 1. Loops can read and write elements in an array. 2. Loops can be used to add, subtract, multiply and divide the elements. 3. Loops can also be used for more complex processing such as calculating averages. Use the square brackets to index the elements of an array.

USING ARRAY IN C An Array must be declared and defined before it can be used. Declaration and Definition tell the compiler the: 1. name of the array; 2. the type of each element; and 3. the size or the number of elements in the array. The size of the array is a constant and must have a value at compilation time. Array is defined in the same manner as ordinary variable except each array must be accompanied by a size specification.

General form of a One dimensional arrays is: storage class data-type array [expression]; int IT[5]; float CS[7]; double ECE[35]; Storage class is optional. One dimensional array string data type a character in a string can be accessed either as an element in an array by making use of a pointer to character. Strings are handled differently by an Array. char college[6] = CCMIT; should be: char college[ ] = CCMIT; means that: college[0] = C; college[1] = C; college[2] = M; college[3] = I; college[4] = T; college[5] = \0; int IT[5] = { 1, 2, 3, 4}; float CS[7]= {1, 2, 3, 4,};

ACCESSING ELEMENTS IN ARRAYS C uses index to access individual elements in an array. Index must be an integral value or an expression that evaluates to an integral value. Arrays name is a symbolic reference for the address to the first byte of the array. When the array name is used, it is actually referring to the first byte of the array. The index represents an offset from the beginning of the array to the element being referred to. C calculates the address of an element in the array using the formula: element address = array address + (sizeof(element) * index)

STORING VALUES IN ARRAY Declaration and definition only reserve space for the elements in the array. No values will be stored.

To store values in an array it must: 1. initialized the elements,  General form is storage class data type arrayname[expression] = {value1, value2, value n};  Done at the time of declaration and definition.  For each element in the array we provide a value.  Values must be enclosed in braces and separated by comma if there are more than one value.  Initial values must appear on the order in which they will be assigned to the individual array elements. 2. read values from the keyboard or a file  Can be done using loop.  The most appropriate looping statement in an array is the For loop because the number of elements are fixed and unknown.  Individual elements can be assigned values using the assignment operator.  You cannot assign one array to another array even if they match full in type and in size. 3. assign values to each individual element.

SAMPLE PROGRAM FOR ARRAY #include<stdio.h> #include<conio.h> // Sorts the 3 values of num void main() { clrscr(); int num[3] = {5, 3, 10 }; int h, i, temp; for(h=0; h<=3;++h) for(i=0; i<h; ++i) { if(num[i] > num[i+1]) { temp = num[i]; num[i] = num[i+1]; num[i+1] = temp; }

} for(i=0; i<3; i++) printf("%d\n", num[i]); getch(); }

#include<conio.h> #include<stdio.h> #define p printf void main() { int bsit[8]; int count, high; clrscr(); p("\nEnter your 8 choosen numbers:\n"); for(count=0; count<8; count++) scanf("%d", &bsit[count]); { high=0; for(count=0; count<8; count++) { if(high<bsit[count]) high=bsit[count]; } p("The highest number is: %d", high); p("\n"); p("Their difference from the highest are \n\n"); for(count=0; count<8; count++) { p("\n %d difference: %d", bsit[count], high-bsit[count]); }} getche(); }

MULTIDIMENTIONAL ARRAYS Multidimensional arrays allow you to store data in a spreadsheet or matrix like format. Multidimensional arrays require two pairs of square brackets. Can initialize the multidimensional array also upon declaration as well as initialize by assignment. Multidimensional array definition can be written as: <data type> array_name[size_of_first_dimension][size_of_second_dimension]; Example 1: int CCMIT[3][3]; float CCMIT[5][5]; Example 2: int IT[3][3]={1,2,3,4,5,6,7,8,9}; float GRADES[2][3]= {1.00,1.25,1.50,1.75,2.00,2.25}; int NUM[4][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; int VAL[3][4] = { {1,2,3},{4,5,6},{7,8,9,10} }; int BIL[3][4] = {1,2,3,4,5,6,7,8,9,10}; Example 1 explanation:  int CCMIT[3][3]; - means that array CCMIT of integer type has 3 rows and 3 columns.  float CCMIT[5][5]; means that array CCMIT of floating point type has 5 rows and 5 columns. Example 2 explanation:  int IT[3][3]={1,2,3,4,5,6,7,8,9}; - means that array IT of integer type has 3 rows and 3 columns with values of 1, 2, 3, 4, 5, 6, 7, 8, and 9 respectively. int CCMIT[4][3]; float CCMIT[2][3];

IT [0][0] = 1 IT [0][1] = 2 IT [0][2] = 3 IT [1][0] = 4 IT [1][1] = 5 IT [1][2] = 6 IT [2][0] = 7 IT [2][1] = 8 IT [2][2] = 9

LESSON 3 POINTERS

Lesson Objectives: 1. 2. 3. 4. 5. learn the concept of Pointers; to be familiar with the important use of pointers; determine the difference between an Arrays and a Pointers; to apply the is of Pointers in advance topic in C; and to be able to develop good programming techniques using Pointers.

CONCEPT OF POINTERS Used to refer to memory location of another variable without using variable identifier itself. They are mainly used in linked lists and call by reference functions. Several uses of Pointer are: 1. 2. 3. 4. Pass information back and forth between a function and its reference point. Permit references to other functions to be specified as arguments to a given function. Provide an alternate way to access individual array elements. Permits a collection of strings to be represented within a single array, even though the individual strings may differ in length.

FUNDAMENTALS OF POINTERS The unary operator & which is called the address operator evaluates the address of its operand. The unary operator which is called indirection operator or dereference access the data item represented by the variable. Sample Program 1:

#include<conio.h> #include<stdio.h> // Sample program that illustrates the relationship between // an address and a pointer main() { int u=2; int v; int *pu; int *pv; clrscr(); pu=&u; v=*pu; pv=&v; printf("\nu = %d printf("\nu = %d getch(); return 1; } &u = %x &v = %x pu = %x pv = %x *pu = %d", u, &u, pu, *pu); *pv = %d", v, &v, pv, *pv);

Vous aimerez peut-être aussi