Â
What are 'ARRAYS' ?
An Array is known as the collection of Similar data types.
An Array is actually a data structure which consists of a group of elements of same type.
Arrays store same type of elements in consecutive memory location one after another.
Examples :-Â
Suppose there is an array which consists of 5 Elements of integer type let's say the elements are 4,6,2,7,9.
So, if in the above array the address of the memory location of the element 4 is 'a123480' then the address of the memory location of the next element 6 will be 'a123481' and that of the next element i.e. 2 will be 'a123482' and further elements will be followed.
array element           address
4Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 'a123480'
6Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 'a123481'
2Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 'a123482'
7Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 'a123483'
9Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 'a123484'
Now let's dig into how to declare and create an array in different programming languages:Â
C
C++
JAVA
For Python we don't have as such any specific syntax for declaration of array in python you just use the list for the array purpose and for multi-dimensional arrays you indeed need to import a module named 'numpy'
Now Lets see some examples of Arrays in C, C++, Java and Python:
C
#include <stdio.h>
int main()
{
int n;
printf("Enter the size of Array\n");
scanf("%d", &n);
//declaring array
int arr[n];
// loop to take array elements from the user
printf("Enter the Elements of Array\n");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// printing the array elements
printf("\nArray elements you inputed are :- \n");
for (int i = 0; i < n; i++)
{
printf("%d", arr[i]);
printf("\n");
}
return 0;
}
  Â
Output For C
C++
#include<iostream>
using namespace std;
int main()
{
//Declaring Variable to store length of array
int n;
//Taking input from user for length of array
cout<<"Enter the length of array\n";
cin>>n;
//Declaring an array of sizen n, and type integer
int arr[n] ;
//Taking input of elements of array
cout<<"Enter the elemnets of the Array\n";
for (int i =0 ; i <n ; i++)
{
cin>>arr[i] ;
}
//Printing the elements of the array.
cout<<"\nThe array elements are: \n";
for(int i=0 ; i <n ; i++)
{
cout<<arr[i]<<'\n' ;
}
return 0 ;
}
  Â
Output For C++
Python
# declaring an empty list that is to be used as an array
arr = []
# taking the length of the array from the user
n = int(input("Enter the no of elements required in the array/list\n"))
# using for loop for taking the array elements
print("Enter the elements for array/list")
for i in range(n):
a = input("")
arr.append(a)
# finally printing the array
print("The array elements are\n", arr)
  Â
Output For Python
JAVA
package com.company;
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Size of Array");
int n = sc.nextInt();
// declaring array
int[] arr = new int[n];
// for loop to take the array elements from the user
System.out.println("Enter the elements of Array");
for (int i=0; i<n;i++)
{
arr[i] = sc.nextInt();
}
// printing the array elements
System.out.println("The Inputed values are :-");
for (int i=0;i<n;i++)
{
System.out.println(arr[i]);
}
}
}
Output For Java
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP