Bài giảng Lập trình C - Session 6: Arrays

Explain array elements and indices

Define an array

Explain array handling in C

Explain how an array is initialized

Explain string / character arrays

Explain two dimensional arrays

Explain initialization of two dimensional arrays

 

 

ppt14 trang | Chuyên mục: C/C++ | Chia sẻ: dkS00TYs | Lượt xem: 1656 | Lượt tải: 3download
Tóm tắt nội dung Bài giảng Lập trình C - Session 6: Arrays, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Arrays Session 6 Elementary Programming with C/Session 7/ * of 18 Objectives Explain array elements and indices Define an array Explain array handling in C Explain how an array is initialized Explain string / character arrays Explain two dimensional arrays Explain initialization of two dimensional arrays Elementary Programming with C/Session 7/ * of 18 Array Elements & Indices Each member of an array is identified by unique index or subscript assigned to it The dimension of an array is determined by the number of indices needed to uniquely identify each element An index is a positive integer enclosed in [ ] placed immediately after the array name An index holds integer values starting with zero An array with 11 elements will look like - Player[0], player[1], player[2],…. Player[10] Elementary Programming with C/Session 7/ * of 18 . Defining an Array-1 An array has some particular characteristics and has to be defined with them These characteristics include – Storage Class Data Types of the elements in the Array Array Name Which indicates the location of the first  member of the array Array Size a constant evaluating to a +ve value Elementary Programming with C/Session 7/ * of 18 Defining an Array-2 An array is defined in the same way as a variable is defined. The only change is that the array name is followed by one or more expressions, enclosed within square brackets [], specifying the array dimension. Storage_Class data_types array_name[size] int player[11]; Elementary Programming with C/Session 7/ * of 18 Norms with Arrays All elements of an array are of the same type Each element of an array can be used wherever a variable is allowed or required Each element of an array can be referenced using a variable or an integer expression Arrays can have their data types like int, char, float or double Elementary Programming with C/Session 7/ * of 18 Array Handling in C-1 An array is treated differently from a variable in C Two arrays, even if they are of the same type and size cannot be tested for equality It is not possible to assign one array directly to another Values cannot be assigned to an array on the whole, instead values are assigned to the elements of the array Elementary Programming with C/Session 7/ * of 18 Array Handling in C-2 /* Input values are accepted from the user into the array ary[10]*/ #include void main() { int ary[10]; int i, total, high; for(i=0; i high) 	high = ary[i]; } printf(“\nHighest value entered was %d”, high); /*	prints average of values entered for ary[10] */ for(i=0,total=0; i 	void main() 	{ 	 char alpha[26]; 	 int i, j; 	 for(i=65,j=0; i 	#include 	void main () 	{ 	int i, n = 0; 	int item; 	char x[10][12]; 	char temp[12]; 	 	clrscr(); 	printf(“Enter each string on a separate line\n\n”); 	printf(“Type ‘END’ when over \n\n”); 	 	/* read in the list of strings */ 	do 	{ 	printf(“String %d : ”, n+1); 	scanf(“%s”, x[n]); 	} while (strcmp(x[n++], “END”)); 	/*reorder the list of strings */ 	 contd…. 	 Example Elementary Programming with C/Session 7/ * of 18 n = n – 1; for(item=0; item 0) 	{ 	/*interchange two stings */ 	strcpy (temp, x[item]); 	strcpy (x[item], x[i]); 	strcpy (x[i], temp); 	 	} 	 } } /* Display the arranged list of strings */ printf(“Recorded list of strings : \n”); for(i = 0; i < n ; ++i) { 	printf("\nString %d is %s", i+1, x[i]); } } Two-Dimensional Array-2 Example 

File đính kèm:

  • pptBài giảng Lập trình C - Session 6 Arrays.ppt