4 Arrays In 'c' Ppt

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View 4 Arrays In 'c' Ppt as PDF for free.

More details

  • Words: 696
  • Pages: 19
Day – 3.

Arrays in ‘C’

22-Jul-2008

1

Arrays ) Introducing

Arrays ) Declaring Array Variables, Creating Arrays, and Initializing Arrays ) Passing Arrays to Methods ) Copying Arrays ) Multidimensional Arrays ) Search and Sorting Methods

22-Jul-2008

2

Introducing Arrays Array is a data structure that represents a collection of the same types of data. int num[10]; num

reference

22-Jul-2008

num [0] num [1] num [2] num [3] num [4] num [5] num [6] num [7] num [8] num [9]

An Array of 10 Elements of type int

3

Declaring Array Variables ) datatype

arrayname[index];

Example:

int list[10]; char num[15]; float hat[20];

22-Jul-2008

4

Creating Arrays datatype array-name[size]; Example: int num[10]; num[0] references the first element in the array. num[9] references the last element in the array. 22-Jul-2008

5

Declaring and Creating in One Step ) datatype

arrayname[arraySize]= {values seperated by comma}; Example : char c[5] = {‘a’,’F’,’4’,’’=‘,’n’}; int i[4] = {12,15,0,2};

22-Jul-2008

6

The Length of Arrays ) Once

an array is created, its size is fixed. It cannot be changed.

For example, int arr[10]; You can not insert any number to arr[11] location because it is not initialized.

22-Jul-2008

7

Initializing Arrays ) Declaring,

creating, initializing in one step:

float hat[4] = {1.9, 2.9, 3.4, 3.5};

This shorthand syntax must be in one statement.

22-Jul-2008

8

Declaring, creating, initializing Using the Shorthand Notation float list[4] = {1.9, 2.9, 3.4, 3.5};

This shorthand notation is equivalent to the following statements: float list[4]; list[0] = 1.9; list[1] = 2.9; list[2] = 3.4; list[3] = 3.5; 22-Jul-2008

9

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: float list; list = {1.9, 2.9, 3.4, 3.5};

22-Jul-2008

10

Example: Copying Arrays The program simply creates two arrays and attempts to copy one to the other, using an assignment statement.

22-Jul-2008

11

Copying Arrays Before the assignment list2 = list1; list1

list2

Contents of list1

Contents of list2

After the assignment list2 = list1; list1

list2

Contents of list1

Contents of list2

Garbage

22-Jul-2008

12

Copying Arrays With direct assignment: int array1[5] = {2, 3, 1, 5, 10}; int array2[5]; array2 = array1;

22-Jul-2008

13

Multidimensional Arrays Declaring Variables of Multidimensional Arrays and Creating Multidimensional Arrays int matrix[10][10]; for (i=0; i<10; i++) for (j=0; j<10; j++) { matrix[i][j] = i * j; } float mat[5][5]; 22-Jul-2008

14

Multidimensional Array Illustration 0 1 2 3 4

0 1 2 3 4

0

1

2

2

3

0

0

0

1

1

1

4

5

6

2

2

2

7

8

9

3

3

10

11

12

4

4

int matrix[5][5];

22-Jul-2008

7

matrix[2][1] = 7;

3

1

int[][] array ={ {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};

15

Shorthand Notations You can also use a shorthand notation to declare, create and initialize a two-dimensional array. For example, int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} };

This is equivalent to the following statements: array[0][0] array[1][0] array[2][0] array[3][0]

22-Jul-2008

= = = =

1; array[0][1] = 2; array[0][2] = 4; array[1][1] = 5; array[1][2] = 7; array[2][1] = 8; array[2][2] = 10; array[3][1] = 11; array[3][2]

3; 6; 9; = 12;

16

Ragged Arrays Each row in a two-dimensional array is itself an array. So, the rows can have different lengths. Such an array is known as a ragged array. For example, int[][] matrix = { {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} }; 22-Jul-2008 17

Exercise : Bubble Sort int i[] = {2, 9, 5, 4, 8, 1, 6}; // Unsorted Pass 1: Pass 2: Pass 3: Pass 4: Pass 5: Pass 6: 22-Jul-2008

2, 5, 4, 8, 1, 6, 9 2, 4, 5, 1, 6, 8, 9 2, 4, 1, 5, 6, 8, 9 2, 1, 4, 5, 6, 8, 9 1, 2, 4, 5, 6, 8, 9 1, 2, 4, 5, 6, 8, 9 18

Created By, ) Mr.

Tushar B Kute,

Lecturer in Information Technology, K. K. Wagh Polytechnic, Nashik. [email protected]

22-Jul-2008

19

Related Documents

4 Arrays In 'c' Ppt
November 2019 11
Lab C++ 08 - Arrays
May 2020 9
Arrays
November 2019 37
Arrays
May 2020 23
Arrays
November 2019 37
Arrays
November 2019 39