Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana.

Similar presentations


Presentation on theme: "Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana."— Presentation transcript:

1 Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana

2 Array Programming In C++, Lecture 6 By Umer Rana 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.

3 Array Programming In C++, Lecture 6 By Umer Rana 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.

4 Declaring Array Programming In C++, Lecture 6 By Umer Rana When declaring arrays, specify Name Type of array Number of elements arrayType arrayName[numberOfElements]; Examples: int c[ 10 ]; Declaring multiple arrays of same type Format similar to regular variables Example: int b[ 100 ], x[ 27 ]; Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c c[6] c[0] c[1] c[2] c[3] c[9] c[8] c[7] c[5] c[4]

5 Array Programming In C++, Lecture 6 By Umer Rana 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 ] ); Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c c[6] c[0] c[1] c[2] c[3] c[9] c[8] c[7] c[5] c[4]

6 Initializers One Dimension Array Programming In C++, Lecture 6 By Umer Rana 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

7 Example Array Programming In C++, Lecture 6 By Umer Rana #include 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]); } printf("\nThanks for Entering Weekly temperature"); getch(); }

8 Example Array Programming In C++, Lecture 6 By Umer Rana 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(); }

9 Referring to individual Element of the Array Programming In C++, Lecture 6 By Umer Rana 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];

10 Referring Example Programming In C++, Lecture 6 By Umer Rana 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(); }

11 Points to Remember Programming In C++, Lecture 6 By Umer Rana 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 };

12 More than one Dimension Programming In C++, Lecture 6 By Umer Rana 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_type Array_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.

13 Two Dimension Array Programming In C++, Lecture 6 By Umer Rana The following declares a two-dimensional 3 by 2 array of integers. Data_type Array_name [row size][column size]; int matrix [3][2];

14 Two Dimension Array Programming In C++, Lecture 6 By Umer Rana int main(void) { int stud[4][2]={ {1,56}, {2,44}, {3,67}, {4,77} }; int i; printf("\n Enterd Reg.No & Marks are"); for (i=0;i<=3;i++) { printf("\n%d %d",stud[i][0],stud[i][1]); } getch(); }

15 Two Dimension Array Programming In C++, Lecture 6 By Umer Rana int main(void) { int stud[4][2]; int I; 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 Enterd Reg.No & Marks are"); for (i=0;i<=3;i++) { printf("\n%d %d",stud[i][0],stud[i][1]); } getch(); }

16 Three Dimension Array Programming In C++, Lecture 6 By Umer Rana The declaration form of Three-dimensional array is : Data_type Array_name [size1][size2][size3]; Data_type Array_name [No of Sets][Set Rows][Set Column];

17 Three Dimension Array Programming In C++, Lecture 6 By Umer Rana Example: int arr[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.

18 Three Dimension Array Programming In C++, Lecture 6 By Umer Rana Int arr[3][2][2]= { 1{1,1},{3,3} }, { 2{2,2},{3,3} }, { 3{3,2},{4,4} } };

19 Write Array of 5 number in ascending order Programming In C++, Lecture 6 By Umer Rana

20 int main() { 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 Umer Rana

21 Write Array of 5 number in Descending order Programming In C++, Lecture 6 By Umer Rana

22 main () { int i,j,temp,a[5]; printf ("Enter the numbers \n"); for (i=0; i<5; ++i) scanf ("%d",&a[i]); for (i=0; i

23 String Programming In C++, Lecture 6 By Umer Rana 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.

24 String Programming In C++, Lecture 6 By Umer Rana 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. ILIKECPROGRAMMING\0

25 String Programming In C++, Lecture 6 By Umer Rana 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(); }

26 String Programming In C++, Lecture 6 By Umer Rana 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(); }

27 String Programming In C++, Lecture 6 By Umer Rana #include int main(void) { char name[25]; scanf("%s",&name); printf("%s",name); getch(); }

28 String Programming In C++, Lecture 6 By Umer Rana #include void main(void) { char name[25]; gets(name); puts(name); getch(); }

29 Two Dimensional String Programming In C++, Lecture 6 By Umer Rana 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];

30 Two Dimensional String Programming In C++, Lecture 6 By Umer Rana void main(void) { char name[4][10]={ "Umer", "Ali", "Sarah", "Adil" }; for(int i=0;i<4;i++) { printf("%s\n",name[i]); } getch(); }

31 Programming In C++, Lecture 6 By Umer Rana String Functions:- strlen returns the length of a string strlwr it will convert all characters stored in the character array string to lowercase strupr it will convert all characters stored in the character array string to UPPERCASE strcat strcat(str1,str2) concatenates a copy of the string pointed by str2 to the string pointed by str1 and terminates str1 with a null) strcpy strcpy(str1,str2) copies the string pointed to by str2 to the string pointed to by str1 and terminates str1 with a null.

32 Programming In C++, Lecture 6 By Umer Rana strcmp strcmp(str1,str2) compares str1 and str2. Returns a negative value if str1 str2. strchr strchr(str,ch) finds ch in str. Returns a pointer to the first occurrence of the character ch in str, if ch does not occur in str,strchr returns NULL. strstr strstr(str1,str2) finds the first occurrence of substring str2 in str1.Returns a pointer to the elements in str1 that contain str2, or NULL if str2 does not occur in str1. strrev strrev(str) reverses all characters in str.

33 Programming In C++, Lecture 6 By Umer Rana #include int main(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(); } strlen String function

34 Programming In C++, Lecture 6 By Umer Rana Quiz Q. An array is a collection of? Q. Is this correct array declaration? int num(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? int num[5]; num[5]=10; Q. Which is declaration of two dimension array? int num[4][2]; int num[3][4][2];

35 Programming In C++, Lecture 6 By Umer Rana 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()


Download ppt "Programming In C++ Spring Semester 2013 Lecture 6 Programming In C++, Lecture 6 By Umer Rana."

Similar presentations


Ads by Google