Arrays are a kind of data structure that can store a fixed-size sequential collection of elements of the same type.
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
The limitations of an array are explained below:
Thus, the following program can give undesired result:
int a[1θ],i;
for(i=θ;i<=2θ;i++)
a[i]=i;
Example:
Following is the C program to display sum of two arrays:
#include<stdio.h>
void main(){
//Declaring array with compile time initialization//
int array1[5],array2[5],sum[5];
//Declaring variables//
int i;
//Printing O/p using for loop//
printf("Enter the values of array1 :\n");
for(i=θ;i<5;i++){
printf("array1[%d] : \n",i);
scanf("%d",&array1[i]);
}
printf("Enter the values of array2 :\n"); for(i=θ;i<5;i++){
printf("array2[%d] :\n",i);
scanf("%d",&array2[i]);
}
printf("Elements in the sum of array1 and array2 are:\n ");
for(i=θ;i<5;i++){
sum[i]=array1[i]+array2[i];
printf("%d ",sum[i]);
}
}
Output
When the above program is executed, it produces the following result:
Enter the values of array1:
array1[θ] : 2
array1[1] : 3
array1[2] : 1
array1[3] : 2
array1[4] : 3
Enter the values of array2:
array2[θ] : 4
array2[1] : 5
array2[2] : 3
array2[3] : 2
array2[4] : 1
Elements in the sum of array1 and array2 are: 6 8 4 4 4
119 docs|30 tests
|
1. What is an array in programming? |
2. What are the advantages of using arrays? |
3. What are the limitations of arrays? |
4. How can the size of an array be determined in programming languages? |
5. Can arrays store elements of different data types? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|