Introduction & Implementation of Array in C++


Hey Shouters!! Today we will learn about an array in c++ and its implementation in C++.

In the former blog, we discussed the introduction of data structure. In this blog, we will further discuss our first data structure i.e. arrays.

What is an Array?

An array is a collection of items stored at contiguous memory locations. The array is one of the simplest data structures in C++.  Its main aim is to keep all the data together in a continuous manner. Thus calculating the value of index becomes easier.

Declaration of Array in C++ –

Examples of Array Declaration in C++

  1. int num[3];
  2. int num[]={2,3,4};

Similarly, character array, we can implement as char str[6]=”Hello”;

As i suggested earlier also, for data structure you should concentrate on the figure more and understand how its working. How this array is stored in memory is shown in the fig below—->>>>>

array in C++

For changing the value at a particular index, we can directly access the index and assign the value.e.g.

A simple programme that takes array from the user and prints it is given in the following figure along with its output

In this case, we used for loop for traversing over the array. We can also use other loops such as while and do while loop. The programme is shown as below.

array in C++

Using while loop

array in C++

Using a do-while loop

array in C++

So in this blog we learnt how to deal with arrays. In the next blog, we will carry out our discussions about the next data structure which is linked list. Till then…..

We daily add Multiple Choice Questions for placement preparation questions and technical interview preparation on the Instagram page. Check the Instagram account: Shout Coders

Recommended Posts –

  1. Introduction to C language
  2. Structure of C program
  3. C Program to add two numbers
  4. C Program to reverse a number
  5. Difference between C and C ++.

Happy Learnings!!!!!!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Close Bitnami banner