1 - Page Developed By: Thilak Reddy
1 - Page Developed By: Thilak Reddy
1 - Page Developed By: Thilak Reddy
Array Definition
An array can be defined as ordered list of same type of data elements. These elements may be
data type int, float, char or double. All these elements are stored in consecutive memory location.
An array is described by a single name or identifier. And each element in an array is referenced
by subscript (an index) enclosed in a pair of square brackets. This subscript indicates the position of an
individual data item in an array. The index must be positive integer.
Characteristic features of an array
1. Array is a data structure storing a group of elements, all of which are of the same data type.
2. All the elements of an array share the same name, and they are distinguished from one another
with the help of an index.
3. Random access to every element using a numeric index of an array.
Classification of array
Array are classified into two types:
1. One dimensional arrays
2. Multi dimensional array
Declaration of an array
An array must be declared before it appears in a c program. This is done using an appropriate data
type. At the same time the size of an array must be specified .This allow the compiler to decide on how
much memory has to be reserved for the given array.
Syntax of an array
data_type arrayname[size];
data type : any basic data type or a user defined data type
array name: is the name of an array
size: number of elements of type data_type. And the size must be an integer constant specified within
a pair of square brackets.
Example:-
#include <stdio.h>
void main()
{
int number[10];
printf("The total size of array is : %d\n",sizeof(number));
}
The total size of a one-dimensional array of 10 integers is 40 bytes. Because the size of an integer data is
4 bytes.
static constant variable that allows you to define a constant value inside a class body.
Example
#include<stdio.h>
void main()
{
int age[5]={2,4,34,3,4};
printf("age %d",age[3]);
}
int age[5]={2,4,34,3,4};
int age[]={2,4,34,3,4};
In this case, the compiler determines the size of array by calculating the number of elements of an
array.
printf("%d\n",marks[i]);
#include <stdio.h>
int main(){
int i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
int marks[n];
for(i=0;i<n;++i){
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}
Output
#include <stdio.h>
void main()
{
int i, j, a, n, number[30];
printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (number[i] > number[j])
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
4. Write a C program to accept two integer arays and find the sum of the corresponding
elements of these two arrays.
#include<stdio.h>
void main()
{
int i , n;
printf(" Enter the size of Array A and B\n");
scanf("%d" , &n);
int a[n];
int b[n];
int sum[n];
printf(" Enter the elements of Array A\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &a[i]);
}
printf(" Enter the elements of Array B\n");
for(i=0 ; i<n ; i++)
{
scanf("%d" , &b[i]);
}
for(i=0 ; i<n ; i++)
{
sum[i] = a[i] + b[i];
}
printf(" Sum of elements of A and B\n");
for(i=0 ; i<n ; i++)
{
printf("%d\n" , sum[i] );
}
}
5. This Program computes the Sum of Even and Odd Numbers stored in an Array of N
#include<stdio.h>
void main()
{
int i , even , odd , n;
printf("Enter the size of Array\n");
scanf("%d" , &n);
int num[n];
even = 0;
odd = 0;
printf(" Enter Array elements\n");
for(i=0 ; i<n ; i++)
2. Multidimensional Arrays:-
This multi dimensional arrays are classified into two dimensional, three dimensional and so on n-
dimensional array. The dimensionality of an array is determined by the number of subscript present in
the given array. If there is one subscript, then it is called a one dimensional array. If there are two
subscript, it is called a two-dimensional array and so on. The dimensionality is determined by the
number of pairs of square brackets placed after the array name
Example
1. array1[] (one dimensional array)
2. array2[][] ( Two dimensional array)
3. array3[][][] (Three dimensional array)
Two dimensional array
It is an ordered table of homogeneous elements. It is generally, referred to as a matrix of some rows
and some columns. It is also called two subscripted variable.
Syntax
Datatype arrayname [rows][columns];
Example
int marks[5][3];
This first example specifies that the marks is two dimensional array of 5 rows and 3 columns. Row
may represent students and the columns represent tests.
This indicates that for each row (Students number) element, the colums elements(tests) are rapidly
processed. This type of arrangement is known as row majoring or row major order. And the column
major order is one where for each column, the row elements are rapidly processed.
Like initialization to a one dimensional array elements, the two dimensional array elements can also be
initialized at the time of declaration.
Syntax
Datatype arrayname[size1][size2]={e1,e2,e3,..en};
Example
Int matrix[3][3]={1,2,3,4,5,6,7,8,9};
If the number of elements to be assigned are less than the total number of elements that a two
dimensional array has contained, then all the remaining elements of an array are assigned to zeros
1. The number of sets of initial value must be equal to the number of rows in the arrayname.
2. One to one mapping is preserved. That is the first set of initial values is assigned to the first row
element and the second set of initial value is assigned to the second row elements and so on
3.If the number of initial values in each initializing set is less than the number of the corresponding
row elements, then all the elements of that row are automatically assigned to zeros.
4. If the number of initial value in each initializing set exceeds the number of the corresponding row
elements then there will be a compilation error.
#include <stdio.h>
int main(){
float a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrix\n");
/* Reading two dimensional Array with the help of two for loop. If there was an array
of 'n' dimension, 'n' numbers of loops are needed for inserting data to array.*/
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("Enter the elements of 2nd matrix\n");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
for(i=0;i<2;++i)
for(j=0;j<2;++j){
/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */
}
printf("\nSum Of Matrix:");
for(i=0;i<2;++i)
for(j=0;j<2;++j){
printf("%.1f\t",c[i][j]);
if(j==1) /* To display matrix sum in order. */
printf("\n");
}
return 0;
}
Ouput
#include <stdio.h>
int main(){
int r,c,a[100][100],b[100][100],sum[100][100],i,j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d",&r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d",&c);
printf("\nEnter elements of 1st matrix:\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
Output:
8 -11
17 6
5 5
/* If colum of first matrix in not equal to row of second matrix, asking user to enter the
size of matrix again. */
while (c1!=r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d%d",&r2, &c2);
}
Output
Output Matrix:
24 29
6 25