1 / 14

Structured data type: array

Structured data type: array. CHAPTER: 12. Introducing Arrays. Array is a collection of variables of the same data type that are referenced by a common name. . An Array of 10 Elements of type double. NEED FOR ARRAYS. Problem: How to store numerous values of same data type.

dustin
Télécharger la présentation

Structured data type: array

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. Structured data type: array CHAPTER: 12

  2. Introducing Arrays Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elementsof type double

  3. NEED FOR ARRAYS • Problem: • How to store numerous values of same data type. • E.g. store 100 numbers and find max. of those. • Can we store like: • int a, b, c, d, e……. z, A, B, C ……. Z, aa, ab, ac…. az, Aa, Ab, Ac…. Av; • Solution: • int a[100]; // an array

  4. Types of Arrays • Single Dimensional Arrays • Multi Dimensional Arrays

  5. Single Dimensional Arrays

  6. Declaring Array Variables • Form: datatype arrayname[arraySize]; • Example: double myList[10]; • Each element in the array is referred to by an index. myList[0]references the first element in the array. myList[9]references the last element in the array.

  7. The Length of Arrays • Once an array is created, its size is fixed. It cannot be changed. You can find its size using sizeof(arrayName); For example, sizeof(myList) returns 10

  8. Initializing Arrays • Using a loop: for (int i = 0; i < sizeof(myList); i++) myList[i] = i; // cin>>myList[i]; • Declaring, creating, initializing in one step: double myList[] = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement.

  9. Declaring, creating, initializing Using the Shorthand Notation double myList[] = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double myList[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;

  10. CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double myList[]; myList = {1.9, 2.9, 3.4, 3.5};

  11. Memory Representation of 1-D Arrays • Example: double myList[10];

  12. Printing Arrays Arrays are easily printed using for loops. However, formatting the output may require some thought. Determine the output in each case shown below. Example 1: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int j = 0; j < 12; j++) cout << Grades[ j ] << endl; Example 2: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int j = 0; j < 12; j+=3) cout << Grades[ j ] << Grades[j+1] << Grades[j+2] << endl;

  13. Example 3: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int Row = 0; Row <= 2; Row++) for (int Col = 0; Col <=3; Col++) cout << Grades[ 4*Row+Col ] << endl; Example 4: int Grades[12]={78,80,82,84,86,88,90,92,94,96,98,100}; for (int Row = 0; Row <= 2; Row++) { for (int Col = 0; Col <=3; Col++) cout << Grades[ 4*Row+Col ]; cout << endl; }

  14. Home Assignment Write a program to accept a list of numbers (e.g. 10 numbers) from user and search for a specific number given by user.

More Related