Menu
Coddy logo textTech

Operating an Array

Lesson 2 of 26 in Coddy's Arrays in C++ course.

Before operating an array we should keep in mind two terms: Element and index.

  • An Element is data that has been stored at the place.
  • An Index is a position at which the data has been stored. 

For example, 

{3,8,1,0,5,-2,32}  in this array the number 3 is at the 0th position, the number 8 is at the 1st position, and so on.

 

 

If we receive multiple elements from the input,  then we can use a for loop for that and store the values as

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

Similarly, we can output the elements of the array using for loop.


for(int i=0;i<5;i++){
   cout<<arr[i]<<" ";
}
cout<<endl;
challenge icon

Challenge

Declare an array of integers of size 5 names as arr.

Take input from the user, and print the square of each element separated by space.

Try it yourself

#include<iostream>
using namespace std;

int main(){

  //Write your code here 

    return 0;
}   

All lessons in Arrays in C++