14
STRUCTURED DATA TYPE: ARRAY CHAPTER: 12

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

Embed Size (px)

Citation preview

Page 1: CHAPTER: 12. 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

STRUCTURED DATA TYPE: ARRAY

CHAPTER: 12

Page 2: CHAPTER: 12. 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

Introducing ArraysArray is a collection of variables of the same data type that are referenced by a common name.

myList[0]

myList[1]

myList[2]

myList[3]

myList[4]

myList[5]

myList[6]

myList[7]

myList[8]

myList[9]

double myList[10];

myList reference

An Array of 10 Elementsof type double

Page 3: CHAPTER: 12. 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. 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

Page 4: CHAPTER: 12. 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

Types of Arrays

1. Single Dimensional Arrays

2. Multi Dimensional Arrays

Page 5: CHAPTER: 12. 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

SINGLE DIMENSIONAL ARRAYS

Page 6: CHAPTER: 12. 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

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.

Page 7: CHAPTER: 12. 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

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

Page 8: CHAPTER: 12. 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

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.

Page 9: CHAPTER: 12. 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

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;

Page 10: CHAPTER: 12. 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

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};

Page 11: CHAPTER: 12. 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

Memory Representation of 1-D Arrays

Example:

double myList[10];

myList[0]

myList[1]

myList[2]

myList[3] . . . . . myList

[9]

2000 2004 2008 2012 . . . . . 2018

Page 12: CHAPTER: 12. 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

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;

Page 13: CHAPTER: 12. 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

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;

}

Page 14: CHAPTER: 12. 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

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.