DEV Community

VipulDeshmukh95
VipulDeshmukh95

Posted on

Arrays

Simple code to show how the Accessing and Modifying of array elements is done:

// AccessingAndModifyingArrayElements.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

include "pch.h"

include

using namespace std;

int main()
{
//std::cout << "Hello World!\n";

//1. Accessing array elements
cout << "1. Accessing array elements : " << endl;
int arr[]{ 100, 20, 30, 50, 60 };

cout << "Array element 1: " << arr[0] << endl;
cout << "Array element 2: " << arr[1] << endl;
cout << "Array element 3: " << arr[2] << endl;
cout << "Array element 4: " << arr[3] << endl;
cout << "Array element 5: " << arr[4] << endl;

//2. Arrays are not check for out of bounds :
// Most probably code will crash in such situations.
//cout << "enter any number for arr[5] : " << endl;

//cin >> arr[5];

//3. Initializing array:
cout << "\n";
cout << "===============" << endl;
cout << "\n";
//int arr1[5];
//int arr2[5]{};
int arr3[5]{100, 90};

cout << "Array element 1: " << arr3[0] << endl;
cout << "Array element 2: " << arr3[1] << endl;
cout << "Array element 3: " << arr3[2] << endl;
cout << "Array element 4: " << arr3[3] << endl;
cout << "Array element 5: " << arr3[4] << endl;

//4. Modifying array elements
cout << "Enter 5 nums for arr3 : ";
cin >> arr3[0];
cin >> arr3[1];
cin >> arr3[2];
cin >> arr3[3];
cin >> arr3[4];

cout << "MOdified array : " << endl;
cout << arr3[0] << endl;
cout << arr3[1] << endl;
cout << arr3[2] << endl;
cout << arr3[3] << endl;
cout << arr3[4] << endl;

cout << "the value of array name is : " << arr3 << endl;  // this shows the address value of 1st element of array.

return 0;
Enter fullscreen mode Exit fullscreen mode

}

Output:

Image description

Top comments (0)