1 / 14

Tutorial 7

Tutorial 7. Shruti Rathee Concordia University. Arrays. An array is a group of items that can be identified as similar because they are of the same nature. Arrays come in two flavors: one dimensional and multi-dimensional arrays. . On dimensional array. Declaration :

austin
Download Presentation

Tutorial 7

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. Tutorial 7 Shruti Rathee Concordia University

  2. Arrays • An array is a group of items that can be identified as similar because they are of the same nature. • Arrays come in two flavors: one dimensional and multi-dimensional arrays.

  3. On dimensional array • Declaration : DataTypeArrayName[order] • Int num[5]; • Float mark[10]; • Double angle[360];

  4. Initialization • DataTypeArrayName[dimension] = { element1, element2, …, elementn}; • intnumber[2] = {18, 12,}; • double distance[5] = {44.14, 720.52, 96.08, 468.78, 6.28};

  5. Access an array • #include <iostream> using namespace std; intmain() { intarr[] = {4, 7, 9, 4, 6}; cout << ”array 1: " << array [0] << endl; cout << "array2: " << array [1] << endl; cout << "array3: " << array [2] << endl; cout << "array4: " << array [3] << endl; cout << "arrayDistance5: " <array [4] << endl; return 0; }

  6. Excercise • Take input from user for an array of length 10 and find the sum of all elelments in that array. • Declare an array like numbers[] = {8, 25, 36, 44, 52, 60, 75, 89} and then ask the user to input a number using cin and check whether it is in array.

  7. Solution 1. #include <iostream> using namespace std; int main() { // We know that we need a constant number of elements constint max = 10; int number[max]; // We will calculate their sum int sum = 0; cout << "Please type 10 integers.\n”; for( int i = 0; i < max; i++ ) { cout << "Number " << i + 1 << ": "; cin >> number[i]; sum += number[i]; } cout << "\n\nThe sum of these numbers is " << Sum << "\n\n"; return 0; }

  8. Solution 2 #include <iostream> using namespace std; int main() { // Declare the members of the array int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89}; int find; int i, m = 8; cout << "Enter a number to search: "; cin >> find; for (i = 0; (i < m) && (Numbers[i] != Find); ++i) continue; / Find whether the number typed is a member of the array if (i == m) cout << find << " is not in the list" << endl; else cout << find << " is the " << i + 1 << "th element in the list" << endl; return 0; }

  9. Multidimensional array • An array of arrays is called a multidimensional array. • intanArray[3][5]; // a 3-element array of 5-element arrays • [0][0] [0][1] [0][2] [0][3] [0][4] [1][0] [1][1] [1][2] [1][3] [1][4] [2][0] [2][1] [2][2] [2][3] [2][4]

  10. Initialize • int anArray[3][2] = { { 1, 2 }, // row 0 { 6, 7 }, // row 1 { 11, 12 } // row 2 };

  11. Excercise • Write a program that calculates and prints a multiplication table for all values between 1 and 9 using two dimensional array. • Write a program to find the smallest number of the elements of an array

  12. Solution 3 // Declare a 10x10 array constintnNumRows = 10; constintnNumCols = 10; intnProduct[nNumRows ][nNumCols ] = { 0 }; // Calculate a multiplication table for (intnRow = 0; nRow < nNumRows; nRow++) for (intnCol = 0; nCol < nNumCols; nCol++) nProduct[nRow][nCol] = nRow * nCol; // Print the table for (intnRow = 1; nRow < nNumRows; nRow++) { for (intnCol = 1; nCol < nNumCols; nCol++) cout << nProduct[nRow][nCol] << "\t"; cout << endl; }

  13. Solution 4 #include <iostream> using namespace std; int main() { // The members of the array int numbers[] = {8, 25, 36, 44, 52, 60, 75, 89}; int minimum = numbers[0]; int a = 8; // Compare the members for (int i = 1; i < a; ++i) { if (numbers[i] < minimum) minimum = numbers[i]; } // Announce the result cout << "The lowestmembervalue of the array is " << minimum << "." << endl; return 0; }

  14. For more info • http://www.learncpp.com/cpp-tutorial/65-multidimensional-arrays/ • http://www.functionx.com/cpp/Lesson12.htm

More Related