1 / 67

Basic Array Definition

Basic Array Definition. // Subscripts are 0 through 99. Example Definitions. Suppose const int N = 20; const int M = 40; const int MaxStringSize = 80; const int MaxListSize = 1000; Then the following are all correct array definitions int A[10]; // array of 10 ints

bedardj
Download Presentation

Basic Array Definition

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. Basic Array Definition // Subscripts are 0 through 99

  2. Example Definitions • Suppose • const int N = 20; • const int M = 40; • const int MaxStringSize = 80; • const int MaxListSize = 1000; • Then the following are all correct array definitions • int A[10]; // array of 10 ints • char B[MaxStringSize]; // array of 80 chars • double C[M*N]; // array of 800 floats • int Values[MaxListSize]; // array of 1000 ints • Rational D[N-15]; // array of 5 Rationals

  3. Subscripting • Suppose • int A[10]; // array of 10 ints A[0], … A[9] • To access individual element must apply a subscript to list name A • A subscript is a bracketed expression also known as the index • First element of list has index 0 • A[0] • Second element of list has index 1, and so on • A[1] • Last element has an index one less than the size of the list • A[9] • Incorrect indexing is a common error • A[10] // does not exist

  4. Array Elements • Suppose int A[10]; // array of 10 uninitialized ints • To access an individual element we must apply a subscript to list name A

  5. Array Element Manipulation • Consider int i = 7, j = 2, k = 4; A[0] = 1;

  6. Array Element Manipulation • Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5;

  7. Array Element Manipulation • Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3;

  8. Array Element Manipulation • Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0];

  9. Array Element Manipulation • Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12;

  10. Array Element Manipulation • Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; cin >> A[k]; // where next input value is 3

  11. Array Initialization During Declaration • suppose • int x[10] = {8,5,4} • x = {8,5,4,0,0,0,0,0,0,0} • suppose • int y[10] = {0} • y = {0,0,0,0,0,0,0,0,0,0}

  12. Array Element Manipulation • For loop is the typical construct to be used for array element manipulation • Declare and initializes a double type array of ten elements with 10.0 stored at each index/location of the array, use for loop for initialization double arr1[10]; for (int index= 0; index<10; index++) arr1[index] = 10.0;

  13. Array As an Entity • Suppose int x[10]; int y[10]; y = x; illegal cin>>x; illegal cout<<y illegal if (x<=y) ... illegal

  14. Array Element Input/Output To fill an array from user’s input double arr1[10]; for (int index= 0; index<10; index++) cin>>arr1[index]; To output an array onto the screen for (int index= 0; index<10; index++) cout<<arr1[index] <<“ “;

  15. Array Element Manipulation • Write down a code that declares two double type arrays of 10 elements each, it gets them initialized from the user and finally decides and prints whether the arrays are equal or not.

  16. Array Element Manipulation double arr1[10]; double arr2[10]; for (int index= 0; index<10; index++) { cin>>arr1[index]; cin>>arr2[index]; } for (int index= 0; index<10; index++) if (arr1[index] != arr2[index] ...

  17. Printing in reverse order using arrays • How to print an array in the reverse order

  18. Printing in reverse order using arrays int main() { int item[5]= {5,6,7,8,9}; //Declare an array item of five //components for (counter = 4; counter >= 0; counter--) cout << item[counter] << " "; cout << endl; return 0; }

  19. How to check whether an Array is sorted Write down a code that checks whether an array (named arr2 of 10 ints, already initialized) is already sorted in ascending order or not.

  20. How to check whether an Array is sorted int arr2[10]; …//array initialized bool sorted = true; for (int index=0; index<9; index++) { if (arr2[index + 1]<arr2[index]) {sorted = false; break; } }

  21. (Generic and complete code) const int size = 5; int arr2[size]= {1,2,3,4,5}; bool sorted = true; for (int index=0; index<size-1; index++) { if (arr2[index +1]<arr2[index]) {sorted = false; break; } }

  22. (Generic and complete code) if (sorted==true) cout<< "sorted"; else cout<< "unsorted";

  23. Max of an array • Write down a code that finds out the element having maximum value within the array, the code finds out the location of the max as well. • How to start?

  24. Max of an array const int listSize= 100; int list[listSize]; int index; for (index = 0; index < listSize; index++) cin>>list[index]; int maxIndex = 0; //Assume the first element is the largest for (index = 1; index < listSize; index++) if (list[maxIndex] < list[index]) maxIndex = index; cout<<list[maxIndex]<<“ “<< maxIndex;

  25. Sorting of Arrays • Bubble Sort (Ascending order) • Compare the first element with the second one, swap if second is smaller • Do the same process with element 2 and 3, and so on, till the end of the array (Name this process as a “Pass”) • Choose the worst case that is an array already sorted in descending order to find out the number of passes • Choose the following array to apply the above algorithm • {9, 8,7, 5, 4, 3, 2, 1} • Find out the number of comparisons/pass and no. of passses

  26. Sorting of arrays

  27. Sorting of arrays

  28. Sorting of arrays

  29. Sorting of arrays One “Pass” completed

  30. Sorting of arrays

  31. Sorting of arrays

  32. Sorting of arrays

  33. Sorting of arrays 2nd “Pass” completed

  34. Sorting of arrays

  35. Sorting of arrays

  36. Sorting of arrays

  37. Sorting of arrays 3rd “Pass” completed. Sorting completed.

  38. Sorting (Bubble Sort) int main() { const int arraySize = 10; int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; int hold; // temporary variable // bubble sort // loop to control number of passes for ( int pass = 0; pass < arraySize - 1; pass++ ) // loop to control number of comparisons per pass for ( int j = 0; j < arraySize - 1; j++ )

  39. Sorting (Bubble Sort) // compare side-by-side elements and swap them if // first element is greater than second element if ( a[ j ] > a[ j + 1 ] ) { hold = a[ j ]; a[ j ] = a[ j + 1 ]; a[ j + 1 ] = hold; } // end if return 0; } // end main

  40. Base Address of a Function int main() { int myList[5] = {0, 2, 4, 6, 8}; int yourList[5]; cout << "Line 3: Base address of myList: " << myList << endl; return 0; }

  41. Base Address of a Function • A sample run of this program is: • // Line 3: Base address of myList: 0012FEC4 • This is hexadecimal representation of the binary number 00000000000100101111111011000100 • The decimal representation of this number is 1,244,868

  42. Passing Arrays to Functions Passed by reference only The symbol & not used Normally size of the array also passed as another int type variable Functions cannot return a value of type array

  43. const int ARRAY_SIZE = 10; void fillArray(int x[],int sizeX); int main() { int listA[ARRAY_SIZE] = {0}; fillArray(listA, ARRAY_SIZE); return 0; } void fillArray(int list[], int listSize) { int index; for (index = 0; index < listSize; index++) cin >> list[index]; } Passing Arrays to Functions

  44. Passing Arrays to Functions We can prevent an array from being changed by the function being called by using the reserve word “const”

  45. Passing Arrays to Functions const int ARRAY_SIZE = 10; int sumArray(const int x[],int sizeX); void fillArray(int x[],int sizeX); void copyArray(const int x[], int y[], int length); int main() { int listA[ARRAY_SIZE] = {0}; int listB[ARRAY_SIZE]; fillArray(listA, ARRAY_SIZE); cout << sumArray(listA, ARRAY_SIZE) << endl; copyArray(listA, listB, ARRAY_SIZE); return 0; }

  46. Passing Arrays to Functions int sumArray(const int list[], int listSize) { int index; int sum = 0; for (index = 0; index < listSize; index++) sum = sum + list[index]; return sum; } void copyArray(const int listOne[], int listTwo[], int listOneSize) { int index; for (index = 0; index < listOneSize; index++) listTwo[index] = listOne[index]; }

  47. Character Arrays • Character Array: An array of Characters • Can be declared by user • Its size is determined by the user’s statement • E.g. char name[5]= {‘A’, ‘l’, ‘i’} • char name[5]=“Ali” • char name[]=“Ali”

  48. C Strings A predefined data type in C++, that is similar to character arrays, but has the following differences Size of strings is automatic A string always terminates at null character (‘\0’) We shall use the concept of character arrays and strings interchangeably therefore we redefine the previous examples as follows char name[5]= {‘A’, ‘l’, ‘i’, ‘\0’} char name[5]=“Ali” char name[]=“Ali” Now we can treat them as strings

  49. Length of the character array Write down a code segment that finds out and displays the length (excluding the null character), of a character array. character array is obviously null terminated and defined and initialized somewhere else in the code. int len =0; while (arr1[len] != '\0') { len++; }

  50. Question • Write down a code segment that finds the smallest number present in five different int arrays. The arrays are already initialized and have the names • arr1 having arrSize1 • arr2 having arrSize2 and so on • Last array is arr5 having arrSize5 Use a general function smallestNumber to find the smallest within an array. You can use this function repeatedly to solve the problem. You can use a temporary array having size equal to 5, if required. Do not declare or initialize the actual five arrays, they are already declared and initialized.

More Related