0% found this document useful (0 votes)
4 views29 pages

4 Array

Uploaded by

ravi167784
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
4 views29 pages

4 Array

Uploaded by

ravi167784
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 29

Basic Computer Programming & Data

Structure – I
PPT - IV

By Prof. Nikita Khandelwal

Basic Computer Programming By Prof. Nikita Khandelwal


C++ Arrays
• In C++, an array is a variable that can store multiple values of the same type.

• For example,

• Suppose a class has 27 students, and we need to store the grades of all of them.
Instead of creating 27 separate variables, we can simply create an array:

double grade[27];

Here, grade is an array that can hold a maximum of 27 elements of double type. In
C++, the size and type of arrays cannot be changed after its declaration.
Basic Computer Programming By Prof. Nikita Khandelwal
C++ Array Declaration
dataType arrayName[arraySize];
For example,
int x[6];
Here,
• int - type of element to be stored
• x - name of the array
• 6 - size of the array

Basic Computer Programming By Prof. Nikita Khandelwal


Access Elements in C++ Array
• In C++, each element in an array is associated with a number. The
number is known as an array index. We can access elements of an
array by using those indices.

// syntax to access array elements


array[index];

Basic Computer Programming By Prof. Nikita Khandelwal


• Consider the array x we have seen above.

Elements of an array in C++

Basic Computer Programming By Prof. Nikita Khandelwal


Few Things to Remember:

• The array indices start with 0. Meaning x[0] is the first element stored
at index 0.
• If the size of an array is n, the last element is stored at index (n-1). In
this example, x[5] is the last element.
• Elements of an array have consecutive addresses. For example,
suppose the starting address of x[0] is 2120d. Then, the address of the
next element x[1] will be 2124d, the address of x[2] will be 2128d and
so on.
Here, the size of each element is increased by 4. This is because the size
of int is 4 bytes.
Basic Computer Programming By Prof. Nikita Khandelwal
C++ Array Initialization
• In C++, it's possible to initialize an array during declaration. For example,
• // declare and initialize and array
• int x[6] = {19, 10, 8, 17, 9, 15};

C++ Array elements and their data


Basic Computer Programming By Prof. Nikita Khandelwal
• Another method to initialize array during declaration:
// declare and initialize an array
int x[] = {19, 10, 8, 17, 9, 15};
Here, we have not mentioned the size of the array.
In such cases, the compiler automatically computes the size.

Basic Computer Programming By Prof. Nikita Khandelwal


C++ Array With Empty Members
• In C++, if an array has a size n, we can store upto n number of
elements in the array. However, what will happen if we store less than
n number of elements.
• For example,
• // store only 3 elements in the array
• int x[6] = {19, 10, 8};
• Here, the array x has a size of 6. However, we have initialized it with
only 3 elements.
• In such cases, the compiler assigns random values to the remaining
places. Oftentimes, this random value is simply 0.

Basic Computer Programming By Prof. Nikita Khandelwal


Empty array members are automatically assigned the value 0

Basic Computer Programming By Prof. Nikita Khandelwal


How to Insert and Print Array Elements?
int mark[5] = {19, 10, 8, 17, 9}
// change 4th element to 9
mark[3] = 9;
// take input from the user
// store the value at third position
cin >> mark[2];
// take input from the user
// insert at i𝑡ℎ position
cin >> mark[i-1];
// print first element of the array
cout << mark[0];
// print i𝑡ℎ element of the array
cout >> mark[i-1];
Basic Computer Programming By Prof. Nikita Khandelwal
Example 1: Displaying Array Elements
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {7, 5, 6, 12, 35};
cout << "The numbers are: ";
// Printing array elements
for (int i = 0; i < 5; ++i) {
cout << numbers[i] << " ";
}
return 0;
}
Basic Computer Programming By Prof. Nikita Khandelwal
Example 2: Take Inputs from User and Store Them in an Array

#include <iostream> cout << "The numbers are: ";


using namespace std; // print array elements
int main() { for (int n = 0; n < 5; ++n) {
int numbers[5]; cout << numbers[n] << " ";
cout << "Enter 5 numbers: " << }
endl; return 0;
// store input from user to array }
for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}

Basic Computer Programming By Prof. Nikita Khandelwal


Output
Enter 5 numbers:
11
12
13
14
15
The numbers are: 11 12 13 14 15

Basic Computer Programming By Prof. Nikita Khandelwal


Example 3: Display Sum and Average of Array Elements Using for Loop

#include <iostream>
using namespace std; cout << "\nTheir Sum = " << sum
int main() { << endl;
double numbers[] = {7, 5, 6, 12, 35, 27}; // find the average
double sum = 0; average = sum / count;
double count = 0; cout << "Their Average = " <<
average << endl;
double average;
return 0;
cout << "The numbers are: ";
}
for (const double &n : numbers) {
cout << n << " ";
sum += n;
++count;
} Basic Computer Programming By Prof. Nikita Khandelwal
Output
The numbers are: 7 5 6 12 35 27
Their Sum = 92
Their Average = 15.3333

Basic Computer Programming By Prof. Nikita Khandelwal


C++ Multidimensional Arrays
• In C++, we can create an array of an array, known as a
multidimensional array. For example:
int x[3][4];
Here, x is a two-dimensional array. It can hold a maximum of 12
elements.
We can think of this array as a table with 3 rows and each row has 4
columns as shown below

Basic Computer Programming By Prof. Nikita Khandelwal


Elements in two-dimensional array in C++ Programming
Basic Computer Programming By Prof. Nikita Khandelwal
• Three-dimensional arrays also work in a similar way. For example:

float x[2][4][3];

• This array x can hold a maximum of 24 elements.


• We can find out the total number of elements in the array simply by
multiplying its dimensions:

2 x 4 x 3 = 24

Basic Computer Programming By Prof. Nikita Khandelwal


Multidimensional Array Initialization
• Like a normal array, we can initialize a multidimensional array in
more than one way.
1. Initialization of two-dimensional array
int test[2][3] = {2, 4, 5, 9, 0, 19};
The above method is not preferred. A better way to initialize this array
with the same array elements is given below:
int test[2][3] = { {2, 4, 5}, {9, 0, 19}};
This array has 2 rows and 3 columns, which is why we have two rows
of elements with 3 elements each.

Basic Computer Programming By Prof. Nikita Khandelwal


Initializing a two-dimensional array in C++

Basic Computer Programming By Prof. Nikita Khandelwal


2. Initialization of three-dimensional array
int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23, 2, 13, 4, 56,
3, 5, 9, 3, 5, 5, 1, 4, 9};
• This is not a good way of initializing a three-dimensional array. A
better way to initialize this array is:
int test[2][3][4] = { { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
{ {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} } };
• Notice the dimensions of this three-dimensional array.

Basic Computer Programming By Prof. Nikita Khandelwal


• The first dimension has the value 2. So, the two elements comprising
the first dimension are:
Element 1 = { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} }
Element 2 = { {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} }
• The second dimension has the value 3. Notice that each of the
elements of the first dimension has three elements each:
{3, 4, 2, 3}, {0, -3, 9, 11} and {23, 12, 23, 2} for Element 1.
{13, 4, 56, 3}, {5, 9, 3, 5} and {5, 1, 4, 9} for Element 2.
• Finally, there are four int numbers inside each of the elements of the
second dimension:
{3, 4, 2, 3}
{0, -3, 9, 11}
... .. ... ... .. ...
Basic Computer Programming By Prof. Nikita Khandelwal
Example 1: Two Dimensional Array
// C++ Program to display all // use of nested for loop
elements // access rows of the array
// of an initialised two dimensional for (int i = 0; i < 3; ++i) {
array

// access columns of the array


#include <iostream>
for (int j = 0; j < 2; ++j) {
using namespace std;
cout << "test[" << i << "]["
int main() { << j << "] = " << test[i][j] << endl;
int test[3][2] = {{2, -5}, }
{4, 0}, }
{9, 1}};
return 0;
}
Basic Computer Programming By Prof. Nikita Khandelwal
Output
test[0][0] = 2
test[0][1] = -5
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1

Basic Computer Programming By Prof. Nikita Khandelwal


Example 2: Taking Input for Two Dimensional Array
#include <iostream> cout << "The numbers are: " << endl;
using namespace std;
// Printing array elements
int main() { for (int i = 0; i < 2; ++i) {
int numbers[2][3]; for (int j = 0; j < 3; ++j) {
cout << "numbers[" << i << "]["
cout << "Enter 6 numbers: " << endl; << j << "]: " << numbers[i][j] << endl;
}
// Storing user input in the array }
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) { return 0;
cin >> numbers[i][j]; }
}
}

Basic Computer Programming By Prof. Nikita Khandelwal


Output
Enter 6 numbers: 1 2 3 4 5 6
The numbers are:
numbers[0][0]: 1
numbers[0][1]: 2
numbers[0][2]: 3
numbers[1][0]: 4
numbers[1][1]: 5
numbers[1][2]: 6

Basic Computer Programming By Prof. Nikita Khandelwal


Example 3: Three Dimensional Array
// C++ Program to Store value entered // Displaying the values with
by user in proper index.
// three dimensional array and display for (int i = 0; i < 2; ++i) {
it. for (int j = 0; j < 3; ++j) {
#include <iostream> for (int k = 0; k < 2; ++k) {
using namespace std; cout << "test[" << i <<
int main() { "][" << j << "][" << k << "] = " <<
// This array can store upto 12 test[i][j][k] << endl;
elements (2x3x2) }
int test[2][3][2] = { }
{{1, 2},{3, 4},{5, 6}}, }
{{7, 8}, {9, 10}, {11, 12}} return 0;
Basic Computer Programming By Prof. Nikita Khandelwal
}; }
Output
test[0][0][0] = 1
test[0][0][1] = 2
test[0][1][0] = 3
test[0][1][1] = 4
test[0][2][0] = 5
test[0][2][1] = 6
test[1][0][0] = 7
test[1][0][1] = 8
test[1][1][0] = 9
test[1][1][1] = 10
test[1][2][0] = 11
test[1][2][1] = 12
Basic Computer Programming By Prof. Nikita Khandelwal

You might also like