280 likes | 473 Views
Arrays. Arrays. An array is a sequence of same data type. Objects of an array are called elements Group of consecutive memory locations To refer to an element, specify Array name Position number(index number)written in subscript[] First element at position 0
E N D
Arrays • An array is a sequence of same data type. • Objects of an array are called elements • Group of consecutive memory locations • To refer to an element, specify • Array name • Position number(index number)written in subscript[] • First element at position 0 • n element array named c has index values from 0 to n-1: • c[ 0 ], c[ 1 ]...c[ n – 1 ]
Arrays Syntax: arrayname[ position number ]
Declaring Arrays • When declaring arrays, specify • Name • Type of array • Number of elements • Syntax: • arrayTypearrayName[ numberOfElements ]; • Examples: • int c[ 10 ]; • float myArray[ 3284 ]; • Declaring multiple arrays of same type ,Format similar to regular variables • Example: • int b[ 100 ], x[ 27 ];
Initializing Arrays Initialization 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
Example void main() { int a[5]={10,20,30,40,50}; cout<<“Element 1 ”<<a[0]<<endl; cout<<“Element 2 ”<<a[1]<<endl; cout<<“Element 3 ”<<a[2]<<endl; cout<<“Element 4 ”<<a[3]<<endl; cout<<“Element 5 ”<<a[4]<<endl; } We can do it using loop for(int i=0;i<5;i++) cout<<a[i]<<endl;
Character Arrays chara[10]={‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘I’, ‘j’}; cout<<a[0];
Reading array values from keyboard void main() { int array[5]; cout<<"Ente values for array \n"; for(int i=0;i<5;i++) { cout<<"array["<<i<<"] "; cin>>array[i]; } cout<<"Values entered are \n"; for (int k=0;k<5;k++) cout<<array[k]<<"\t"; getch(); }
Reading Strings void main() { char name[20], address[30]; cout<<"Enter name "; cin>>name; cout<<"Enter Address "; cin>>address; cout<<"Name = "<<name <<"\nAddress = "<<address; getch(); } Note : Does not read white spaces
Sorting Arrays Arrange the elements of an array in ascending or descending order Various sorting algorithms are available Selection sort, quick sort, insertion sort etc…
Bubble Sort 35 12 77 101 5 42 • Sorting takes an unordered collection and makes it an ordered one. 101 12 42 35 5 77
Bubble Sort 101 12 42 35 5 77 • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping
Bubble Sort 42 77 101 12 35 5 • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping
Bubble Sort 35 77 101 12 5 42 • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping
Bubble Sort 12 77 101 35 5 42 • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping
Bubble Sort 101 77 35 12 5 42 No need to swap • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping
Bubble Sort 5 101 77 35 12 42 • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping
Bubble Sort 101 5 77 35 12 42 Largest value correctly placed • Traverse a collection of elements • Move from the front to the end • “Bubble” the largest value to the end using pair-wise comparisons and swapping
Bubble Sort void main() { int i, j, tmp; int arr[5]={77,42,35,12,101,5}; for (i = 5; i >=1; i--) for (j = 0; j < i; j++) if (arr[j] > arr[j+1]) { tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } cout<<"Sorted array \n"; for(i=0;i<5;i++) cout<<arr[i]<<endl; getch(); }
Selection Sort One array is sufficient to do our sorting • Search for the smallest value in the array • Place this value in a[0], and place the value that was in a[0] in the location where the smallest was found • Starting at a[1], find the smallest remaining value swap it with the value currently in a[1] • Starting at a[2], continue the process until the array is sorted
Selection Sort void main() { int i, j, minIndex, tmp; int arr[5]={5,3,7,2,1}; for (i = 0; i < 5; i++) { minIndex = i; for (j = i + 1; j < 5; j++) if (arr[j] < arr[minIndex]) minIndex = j; if (minIndex != i) { tmp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tmp; } } cout<<"Sorted array \n"; for(i=0;i<5;i++) cout<<arr[i]<<endl; getch(); }
Multidimensional Arrays • In multidimensional Arrays: • Two sub-scripts [ r] [ c] • Also known as table • Syntax: datatype array_name[row][column]; Example
Multidimensional Arrays void main() { int i, j, tmp; int arr[3][3]={{77,42,35},{12,101,143},{32,190,200}}; cout<<"Array \n"; for (i =0;i<3;i++) for (j = 0; j < 3; j++) cout<<i<<"\t"<<j<<"\t"<<arr[i][j]<<endl; getch(); }
Multidimensional Arrays • C++ allows arrays with multiple index values • char page [30] [100];declares an array of characters named page • page has two index values: The first ranges from 0 to 29 The second ranges from 0 to 99 • Each index in enclosed in its own brackets • Page can be visualized as an array of 30 rows and 100 columns
Two dimensional array example void main() { const int DAYS = 7; const int MAX = 10; char star[DAYS][MAX] = { "Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday" , "Saturday" }; for( int j=0 ; j<DAYS ; j++) //display every string cout << star[j] << endl; getch(); }
Reading elements of 2 dimensional array void main() { const int ROWS=3, COLS=3; int i,j; int a[ROWS][COLS]; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<"Enter value for a["<<i<<"]["<<j<<"]"; cin>>a[i][j]; } } for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<a[i][j]<<"\t"; }cout<<"\n"; }getch(); }
Square Matrix void main() { const int row=3, col=3; int i,j; int A[row][col]; cout << “Enter values for " <<row <<"x"<<col<<"Matrices"<<endl; for(i=0 ; i< row ; i++){ for(j=0 ; j<col; j++){ cout << "A[" << i+1 << "][" << j+1 << "]= "; cin >> A[i][j]; } } for(i=0 ; i< row ; i++) for(j=0 ; j<col; j++) A[i][j] = A[i][j]*A[i][j]; for(i=0 ; i< row ; i++){ for(j=0 ; j<col; j++) cout << A[i][j] << "\t"; cout << endl; } getch(); }