Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Single Dimensional Array
3.
Initialization of One Dimensional Array
4.
Functions & Array
4.1.
Function Declaration with Array as a Parameter:
4.1.1.
Example 1
4.2.
Function Calling with Array as a Parameter:
4.2.1.
Example 1
4.2.2.
Example 2
5.
FAQs
6.
Key Takeaways:
Last Updated: Mar 27, 2024

Single-Dimensional Arrays

Author Vivek Goswami
0 upvote

Introduction

In C++, we need to initialize a variable before using it in our program. But imagine a situation where we calculate the marks scored by, let’s say, a hundred students in a class in our program that we need to access later. How do we do this then? One straightforward way is to initialize a hundred variables and store their values, which is very cumbersome. Another method is by using arrays. 

Arrays are data structures that provide a method of storing similar data types in a contiguous memory location.  The elements are stored in adjacent memory locations, i.e., next to each other.  With arrays, we can make a container that holds many values of a similar kind.

Now, since we have briefly discussed some concepts of arrays, let us understand what a single-dimensional array is.

Also see, Literals in C.Fibonacci Series in C++

Single Dimensional Array

A single dimension array holds various elements of the same data type. To identify and find the value of a particular component, we use the array name and the value of an index element.  

The syntax for declaration of a One Dimensional Array is:

data_type  array_name [array_size];

Example:

Int marks [100];

The array size gives us the value of the elements stored in the array. For example, the index values, marks [23], marks [44], give us the values of a particular component, respectively. The numbers 23 and 44 are called the array's index and help us find a specific element in the array.

Usually, the value of the index in the array starts from 0 and goes until (size-1). If the starting element’s value is marks [0], we find the next element by incrementing the index to a particular value called the offset.  

Therefore, Base value + offset = next value.

Initialization of One Dimensional Array

The syntax for declaration of One Dimensional Array is as follows:

data_type array_name[array_size]= {value_1, value_2, value_3,......., value_n};

Example:

int marks[5] ={88, 90, 95, 87, 99};

It is also possible to not provide the array size when we initialize the array in the function declaration itself. In this case, the compiler calculates the size of the array. 

As in the above example, the following would also be correct.

int marks[ ]= {88, 90, 95, 87, 99};

Functions & Array

Moving forward with arrays and their use in the program, let us understand how arrays are dealt with in a function. Two possible scenarios that arise here are:

  1. Function Declaration and Definition with array as a parameter
  2. Calling function with arrays as an argument

Function Declaration with Array as a Parameter:

First, we shall discuss how functions are declared and defined in C++ with arrays as parameters. We can pass arrays into function as parameters for further use in it. 

The syntax to declare such functions is:
Function_data_type function_name (data_type array_name [size]);

Example 1

int total (int marks[100]);

We can then define the function as we generally do.

#include <iostream>
using namespace std;
int total (int marks[])
{
   // finds a random value between 0 and 100 
   int p= random()%100;
   // the index value at position p of the array is displayed
   return marks[p];
}

Hence, we can pass an array as a parameter in the above function. This code block illustrates how the concepts we have discussed are implemented. 

Function Calling with Array as a Parameter:

The function call contains the function’s argument, and hence, it must be chosen properly and declared so that then we can pass the array correctly. In this case, we need to pass the value of a memory address only.
Syntax: Function_data_type  function_name (data_type  array_name [size]);

Example 1

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

int addElementsOfArray(int arr[], int N) // Passing array and size of array as parameter
{
    int sum = 0;
    for (int i = 0; i < N; i++)
    {
        sum = sum += arr[i]; // Adding elements of array to sum variable
    }
    return sum;
}

int main()
{
    int N = 10;                                         
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 
    cout << "Sum of all the array elements is equal to: " << addElementsOfArray(arr, N);
    return 0;
}

Output

Sum of all the array elements is equal to: 55

Here, we can even pass an array by reference by using the pointer, but the notable thing here is arrays are by default passed by reference in C++. Whatever changes we will make to array elements inside the function will be reflected array itself.

Example 2

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

void modifyElementsOfArray(int arr[], int N) // Passing array and size of array as parameter
{
    int sum = 0;
    for (int i = 0; i < N; i++)
    {
        arr[i] += 1; // Adding 1 to all elements of array.
    }
}

int main()
{


    int N = 10;                                  
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    cout << "Array without modification: ";
    for (int i = 0; i < N; i++)
    {
        cout << arr[i] << " ";
    }
    cout << endl;
    cout << "Array with modification: ";
    modifyElementsOfArray(arr, N); // calling the modifyELementsofArray function
    for (int i = 0; i < N; i++)
    {
        cout << arr[i] << " ";
    }
    return 0;
}

Output: 
Array without modification: 1 2 3 4 5 6 7 8 9 10 
Array with modification: 2 3 4 5 6 7 8 9 10 11

 

Try and compile with online c++ compiler.

FAQs

  1. How can we implement arrays dynamically in C++?
    We can use vectors for the implementation of dynamic arrays in C++.
     
  2. Do we use call by value or call by reference in C++?
    We don’t need to use call-by-reference because arrays are passed by reference in C++ by default, even if we call them by value.
     
  3. Is there any difference between a declaration of the array as int[ ] a and int a[ ]?
    There is no difference between these two types of declaration, both are legal statements.

Key Takeaways:

The above blog taught us about arrays, mainly Single Dimension Array. We analyzed how the single-dimensional arrays are declared, initialized, and used within a program.  Furthermore, sum of two arrays & the use of arrays within a function as a parameter and an argument was discussed along with their code implementation. For learning more about such programming concepts, visit here. 

Recommended Readings: 

Live masterclass