1 / 28

Arrays

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

clara
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays

  2. 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 ]

  3. Arrays Syntax: arrayname[ position number ]

  4. 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 ];

  5. 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

  6. 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;

  7. Character Arrays chara[10]={‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘I’, ‘j’}; cout<<a[0];

  8. 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(); }

  9. 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

  10. 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…

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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(); }

  20. 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

  21. Selection sort

  22. 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(); }

  23. Multidimensional Arrays • In multidimensional Arrays: • Two sub-scripts [ r] [ c] • Also known as table • Syntax: datatype array_name[row][column]; Example

  24. 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(); }

  25. 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

  26. 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(); }

  27. 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(); }

  28. 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(); }

More Related