
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Practice Questions on Arrays in C++
Array is a data structure that store data in the contagious memory location.
Declaring arrays
Declaring arrays is done by the following syntax : int 1D[] - for 1-D array int 2D[][] - for 2-D array
If you initialize an array with lesser number of elements, rest are initialized with 0.
Memory address of elements of the array
1-D array : address[i] = baseAddress + i*size 2-D array (row major) : address[i][j] = baseAddress + (i*n + j) * size
Now, let’s see some practice problem
Predict the output of the following code snippet
int arr[5] = {6, 9}; for(int i = 0; i<5; i++) cout<<arr[i]<<" ";
Output
6 9 0 0 0
The array is initialized with two values and the rest of the values are initialized as 0 which is reflected in the output.
int arr[][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; cout<<arr[1][2];
Output
6
Find the address of the given element of the integer array. If base address is 1420.
1D array : arr[43] address = 1420 + 43*2 = 1506 2D array of size arr[10][10] : arr[5][4], stored as row major address = 1420 + (5*10 + 4)*2 = 1420 + (54)*2 = 1528.
Advertisements