Download presentation
Presentation is loading. Please wait.
Published bySonny Halim Modified over 5 years ago
1
Arrays Imran Rashid CTO at ManiWeber Technologies
2
Outline What & Why One Dimensional Array Initializing Arrays Lab Work
2-D Arrays
3
What & Why
4
What & Why What is an array? Why we need arrays?
Array is a collection of data of same types stored in sequential memory location It is a linear data structure, where data is stored sequentially one after the other The elements in an array is accessed using an index Why we need arrays? In programming, one of the frequently arising problem is to handle numerous data of same type Consider this situation, you are taking a survey of 100 people & you have to store their ages & to solve this you can create an integer array having 100 elements
5
One Dimensional Array
6
One Dimensional Array An array in which data are arranged linearly in only one dimension is called one dimensional array It is commonly known as 1-D array Syntax/Example datatype array_name[size]; int ages[5]; // declares an integer array with 5 elements float arr[5]; // declares an float array with 5 elements char n[50]; // declares an character array with 50 elements 1 2 3 4 ages[0] ages[1] ages[2] ages[3] ages[4]
7
Initializing Arrays
8
Initializing Arrays You can initialize C++ array elements either one by one or using a single statement int ages[5]; ages[0] = 10; ages[1] = 20; ages[2] = 30; ages[3] = 40; ages[4] = 50; int ages[5] = {10, 20, 30, 40, 50}; float cgpas[5] = {3.5, 2.9, 2.0, 4.0, 3.8}; char vowels[5] = {‘a’, ‘o’, ‘i’, ‘e’, ‘u’}; double balance[3] = {1000.0, 2.0, 3.4}; 10 20 30 40 50 ages[0] ages[1] ages[2] ages[3] ages[4]
9
Lab Work
10
Lab Work Write a C++ program to find the sum and average of one dimensional integer array? Enter number of elements you want to insert 5 Enter element in ascending order Enter element 1: 8 Enter element 2: 1 Enter element 3: 10 Enter element 4: 5 Enter element 5: 6 The sum of Array is :30 The average of Array is :6
11
Lab Work Solution
12
Lab Work Write a C++ program to reverse the element of an integer 1-D array? Enter number of elements you want to insert 6 Enter element 1: 13 Enter element 2: 69 Enter element 3: 30 Enter element 4: 51 Enter element 5: 11 Enter element 6: 81 Reverse array
13
Lab Work Solution
14
Lab Work Write a C++ program to find the largest and smallest element of an array? Enter number of elements you want to insert 5 Enter element 1: 13 Enter element 2: 69 Enter element 3: 30 Enter element 4: 51 Enter element 5: 11 Largest element is : 69 Smallest element is : 11
15
Lab Work Solution
16
2-D Arrays
17
m[0][0] m[0][1] m[0][2] m[0][3] m[1][0] m[1][1] m[1][2] m[1][3]
2-D Arrays In C++ programming, you can create an array of arrays known as multidimensional array int m[3][4]; Column 01 [0] Column 02 [1] Column 03 [2] Column 04 [3] Row 01 m[0][0] m[0][1] m[0][2] m[0][3] Row 02 m[1][0] m[1][1] m[1][2] m[1][3] Row 03 m[2][0] m[2][1] m[2][2] m[2][3]
18
2-D Arrays Initializing 2-D Arrays (guess the addresses) int m[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}; Column 01 [0] Column 02 [1] Column 03 [2] Column 04 [3] Row 01 1 2 3 4 Row 02 5 6 7 8 Row 03 9 10 11 12
19
Quiz Consider the given 2-D array & convert rows to columns 1 2 3 4 5
6 7 8 9 1 4 7 2 5 8 3 6 9
20
Any ?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.