0% found this document useful (0 votes)
3 views

Array

The document explains the concept of arrays in programming, comparing them to a parking lot for two-wheelers, where each bike represents an element and its parking space represents an index. It provides C++ examples for declaring arrays, accessing elements, traversing arrays, taking user inputs, and calculating the sum and average of array elements. Overall, it illustrates how arrays are used to store and manage collections of similar data types in contiguous memory.

Uploaded by

Votex 2099
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Array

The document explains the concept of arrays in programming, comparing them to a parking lot for two-wheelers, where each bike represents an element and its parking space represents an index. It provides C++ examples for declaring arrays, accessing elements, traversing arrays, taking user inputs, and calculating the sum and average of array elements. Overall, it illustrates how arrays are used to store and manage collections of similar data types in contiguous memory.

Uploaded by

Votex 2099
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Array

Imagine you have a parking lot at a mall that only allows two-wheelers. Now, think of an array
as a line-up of these two-wheelers in the parking lot. All the bikes are parked right next to
each other, one after the other. Each parking space is like a memory slot, and the entire line-
up is our array.

So, an array is like a row of identical things (in this case, two-wheelers) parked together in a
designated area. The cool thing is, you can easily access any bike in the line-up by knowing its
position, just like you access elements in an array using their index. It's a way of organizing
and keeping track of similar items in a sequence.
An Array is a data structure used to store blocks of information in contiguous
memory allocation. The data can be integer, strings, characters, class objects, etc.

In this memory representation, 2, 7, 4, 8,1, 6 are the elements of the array stored in adjacent
locations, and 0, 1, 2, 3, 4, 5 are the indexes of these elements. Index position starts from 0
and goes up to size-1. In this example, the index goes up to 5, but actually, there are a total
of 6 elements.

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

Example 1: The C++ Program to Illustrate How to Access Array Elements

#include <bits/stdc++.h>
using namespace std;

int main()
{

int arr[3];

// Inserting elements in an array


arr[0] = 5;
arr[1] = 6;
arr[2] = 7;

// Accessing and printing elements of the array


cout << "arr[0]: " << arr[0] << endl;
cout << "arr[1]: " << arr[1] << endl;
cout << "arr[2]: " << arr[2] << endl;

return 0;
}

Example 2: The C++ Program to Illustrate How to Traverse an Array

// C++ Program to Illustrate How to Traverse an Array


#include <bits/stdc++.h>
using namespace std;

int main()
{

// Initialize the array


int arr[10]
= { 2, 5, 6, 4, 1, 7, 8, 12, 18, 20 };
// Traverse the array using for loop
for (int i = 0; i < 10; i++) {
// Print the array elements using indexing
cout << arr[i] << " ";
}

return 0;
}

Example 3: Take Inputs from User and Store Them in an Array

#include <bits/stdc++.h>
using namespace std;

int main() {

int numbers[5];

// store input from user to array


for (int i = 0; i < 5; ++i) {
cin >> numbers[i];
}

cout << "The numbers are: ";

// print array elements


for (int n = 0; n < 5; ++n) {
cout << numbers[n] << " ";
}

return 0;
}

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

#include <bits/stdc++.h>
using namespace std;
int main() {

// initialize an array without specifying size


double arr[6] = {7, 5, 6, 12, 35, 27};

double sum = 0;

double average;

cout << "The numbers are: ";

// print array elements


// use of range-based for loop
for (int i=0;i<6;i++) {
cout << arr[i] << " ";

// calculate the sum


sum = sum + arr[i];

// print the sum


cout << "\nTheir Sum = " << sum << endl;

// find the average


average = sum / 6;
cout << "Their Average = " << average << endl;

return 0;
}

You might also like