350 likes | 470 Views
Programming In C++. Spring Semester 2013 Lecture 6. Array. Array provides the facility to store a collection of data of same type under single variable name in memory. Just like the ordinary variable, the array should also be declared properly.
E N D
Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By UmerRana
Array Array provides the facility to store a collection of data of same type under single variable name in memory. Just like the ordinary variable, the array should also be declared properly. The declaration of array includes the type of array, the array name and maximum number of elements. Programming In C++, Lecture 6 By UmerRana
Array To refer to a particular location or element in the array, we specify the name of the array and the index of the particular element in the array. The index specifies the location of the element in the array. The array index starts from zero. The maximum index value will be equal to the size of the array minus one. Programming In C++, Lecture 6 By UmerRana
Name of array (Note that all elements of this array have the same name, c) c[0] 45 c[1] 6 c[2] 0 c[3] 72 c[4] 153 c[5] 89 c[6] 0 c[7] 62 c[8] 3 c[9] 1 Position number of the element within array c Declaring Array When declaring arrays, specify Name Type of array Number of elements arrayTypearrayName[numberOfElements]; Examples: int c[ 10 ]; Declaring multiple arrays of same type Format similar to regular variables Example: int b[ 100 ], x[ 27 ]; Programming In C++, Lecture 6 By UmerRana
Name of array (Note that all elements of this array have the same name, c) c[0] 45 c[1] 6 c[2] 0 c[3] 72 c[4] 153 c[5] 89 c[6] 0 c[7] 62 c[8] 3 c[9] 1 Position number of the element within array c Array Array Group of consecutive memory locations Same name and type To refer to an element, specify Array name Position number Format: arrayname[position number] First element at position 0 n element array named c: c[ 0 ], c[ 1 ]...c[ n – 1 ] Array elements are like normal variables c[ 0 ] = 45; printf( "%d", c[ 0 ] ); Programming In C++, Lecture 6 By UmerRana
Initializers One Dimension Array Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 0 } All elements 0 If size omitted, initializers determine it int n[ ] = { 1, 2, 3, 4, 5 }; 5 initializers, therefore 5 element array Programming In C++, Lecture 6 By UmerRana
Example Array #include <stdio.h> #include <conio.h> void main(void) { int temper[7]; int day; printf("Please Enter the Temperature of the week\n"); for (day=0;day<7;day++) { printf("\nTemperatureof day %d: ",(day+1)); scanf("%d",&temper[day]); } printf("\nThanks for Entering Weekly temperature"); getch(); } Programming In C++, Lecture 6 By UmerRana
Example Array void main(void) { int temper[7]; int day; printf("Please Enter the Temperature of the week\n"); for (day=0;day<7;day++) { printf("\nTemperature of day %d: ",(day+1)); scanf("%d",&temper[day]); } for (day=0;day<7;day++) { printf("\nTemperature of day %d is %d. ",(day+1),temper[day]); }getch(); } Programming In C++, Lecture 6 By UmerRana
Referring to individual Element of the Array Once the Array has been established we need a way to refer to its individual elements. This is done with subscripts the number in brackets following the array name, this number specifies the element’s position in the array. All the array elements are numbered starting at 0. Example: temper[2]; temper[6]; Programming In C++, Lecture 6 By UmerRana
Referring Example void main(void) { int temper[7]; int day; printf("Please Enter the Temperature of the week\n"); for (day=0;day<7;day++) { printf("\nPlease Enter the Temperature of day %d: ",(day+1)); scanf("%d",&temper[day]); } printf("\nWhich Day Temperature you want to see:"); scanf("%d",&day); printf("\nTemperature of day %d is %d. ",day,temper[day-1]); getch(); } Programming In C++, Lecture 6 By UmerRana
Points to Remember If there are fewer initializers than elements in the array, the remaining elements are initialized to zero. It is important to remember that arrays are not automatically initialized to zero. The programmer must at least initialize the first element to zero for the remaining elements to be automatically zeroed. For Example: int n[ 10 ] = { 0 }; Programming In C++, Lecture 6 By UmerRana
More than one Dimension The Two dimensional array is also called a matrix. Two-dimensional array are those type of array, which has finite number of rows and finite number of columns. The declaration form of 2-dimensional array is: Data_typeArray_name [row size][column size]; The type may be any valid type supported by C. The rule for giving the array name is same as the ordinary variable. The row size and column size should be an individual constant. Programming In C++, Lecture 6 By UmerRana
Two Dimension Array The following declares a two-dimensional 3 by 2 array of integers. Data_typeArray_name [row size][column size]; int matrix [3][2]; Programming In C++, Lecture 6 By UmerRana
Two Dimension Array int main(void) { intstud[4][2]={ {1,56}, {2,44}, {3,67}, {4,77} }; int i; printf("\n EnterdReg.No & Marks are"); for (i=0;i<=3;i++) { printf("\n%d %d",stud[i][0],stud[i][1]); } getch(); } Programming In C++, Lecture 6 By UmerRana
Two Dimension Array int main(void) { int stud[4][2]; intI; for (i=0;i<=3;i++) { printf("\n Enter Reg.No and Marks Please\n"); scanf("\n%d%d",&stud[i][0],&stud[i][1]); } printf("\n EnterdReg.No & Marks are"); for (i=0;i<=3;i++) { printf("\n%d %d",stud[i][0],stud[i][1]); } getch(); } Programming In C++, Lecture 6 By UmerRana
Three Dimension Array The declaration form of Three-dimensional array is : Data_typeArray_name[size1][size2][size3]; Data_typeArray_name[No of Sets][Set Rows][Set Column]; Programming In C++, Lecture 6 By UmerRana
Three Dimension Array Example: intarr[3][2][2]; A Three dimensional array can be thought of as an array of arrays of arrays. The outer array has three elements, each of which is a two dimensional array. Programming In C++, Lecture 6 By UmerRana
Three Dimension Array Intarr[3][2][2]= { { 1 {1,1},{3,3} }, { 2 {2,2},{3,3} }, { 3{3,2},{4,4} } }; Programming In C++, Lecture 6 By UmerRana
Write Array of 5 number in ascending order Programming In C++, Lecture 6 By UmerRana
intmain() { int a[5],i,j,temp; printf("Enter 5 nos.\n\n"); for (i=0;i<5;i++) scanf("%d",&a[i]); for (i=0;i<5;i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("Ascending Order is:"); for(j=0;j<5;j++) printf("\n%d",a[j]); getch(); } Programming In C++, Lecture 6 By UmerRana
Write Array of 5 number in Descending order Programming In C++, Lecture 6 By UmerRana
main () { inti,j,temp,a[5]; printf("Enter the numbers \n"); for (i=0; i<5; ++i) scanf ("%d",&a[i]); for (i=0; i<n; ++i) { for (j=i+1; j<n; ++j) { if (a[i] < a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } printf(“Descending order are given below\n"); for (i=0; i<n; ++i) printf("%d\n",a[i]); getche(); } Programming In C++, Lecture 6 By UmerRana
String A string in C is actually a character array. As an individual character variable can store only one character, so we need an array of characters to store strings. Each character in a string occupies one location in an array. A string constant is one dimensional array of characters terminated by the null character ‘\0’, is put after the last character. This is done so that program can tell when the end of the string has been reached. Character arrays or strings are used to manipulate text such as words and sentences. Programming In C++, Lecture 6 By UmerRana
String For example, char str1[25]; the string ”I LIKE C PROGRAMMING” is stored as follow. Thus, in C, a string is a one-dimensional array of characters terminated a null character. The terminating null character is important. Programming In C++, Lecture 6 By UmerRana
String void main(void) { char str1[25]={'I',' ','l','i','k','e',' ','C',' ','P','r','o','g','r','a','m','m','i','n','g'}; int i=0; while(str1[i]!='\0') { printf("%c",str1[i]); i++; } getch(); } Programming In C++, Lecture 6 By UmerRana
String void main(void) { char str1[25]={'I',' ','l','i','k','e',' ','C',' ','P','r','o','g','r','a','m','m','i','n','g'}; int i=0; printf("%s",str1); getch(); } Programming In C++, Lecture 6 By UmerRana
String #include <stdio.h> #include <conio.h> int main(void) { char name[25]; scanf("%s",&name); printf("%s",name); getch(); } Programming In C++, Lecture 6 By UmerRana
String #include <stdio.h> #include <conio.h> void main(void) { char name[25]; gets(name); puts(name); getch(); } Programming In C++, Lecture 6 By UmerRana
Two Dimensional String • A string is an array of characters; so, an array of strings is an array of arrays of characters. • We can declare a two dimensional character array of MAX strings of size SIZE as follows: • char names[MAX][SIZE]; Programming In C++, Lecture 6 By UmerRana
Two Dimensional String void main(void) { char name[4][10]={ "Umer", "Ali", "Sarah", "Adil" }; for(int i=0;i<4;i++) { printf("%s\n",name[i]); } getch(); } Programming In C++, Lecture 6 By UmerRana
strlen String function #include <cstring> intmain(void) { char name[25]="Umer"; int a=strlen(name); printf("%d",a); char name2[4][10]={ "Umer", "Ali", "Sarah", "Adil" }; puts("\n\n 2 Dimention String"); for(int i=0;i<4;i++) { printf("%d\n",strlen(name2[i])); } getch(); } Programming In C++, Lecture 6 By UmerRana
Quiz Q. An array is a collection of? Q. Is this correct array declaration? intnum(25); Q. Which element of the array does this expression refference? num[4]; Q. What is the difference between the 5’s in these two expressions? intnum[5]; num[5]=10; Q. Which is declaration of two dimension array? intnum[4][2]; intnum[3][4][2]; Programming In C++, Lecture 6 By UmerRana
Quiz Q. string is a collection of? Q. Is this correct character array declaration? char str[25]; Q. Which is more appropriate for reading in multi-word string? gets() printf() scanf() puts() Q. What is the difference between the 5’s in these two expressions? char num[5]; num[5]=‘x’; Q. Which string function use to get length of the string? strlen() strcpy() Programming In C++, Lecture 6 By UmerRana