Professional Documents
Culture Documents
C Programming Arrays
C Programming Arrays
An array is a data structure in which a collection of data items of the same data type are stored.
Characteristic of Arrays
ARRAY DECLARATION
1-Dimension array :
Int score[6]; / * Array called score having 6 elements of integer type */
2- Dimension array:
Int score[6][4]; / * Array called Table having 6 rows and 4 columns of integer type */
3- Dimension array:
1-DIMENSION ARRAYS
Below is an array called score havig 6 elements of int data type .
Elements of the array are referenced using the array name followed by subscripts
i.e score[0], score[1],score[2] …score[n-1]
score
16 12 6 4 7 20
ARRAY DECLARATION
#include<stdio.h>
Void main(void)
{
Int score[6],sum, i ;
i=0;
Sum=0;
For(i=0; i<5; ++i)
{
Printf(“Enter a number”); Reading six elements in an array called score and
Scanf(“%i”,&score[i]); calculating their sum.
Sum=sum+score[i];
}
For(i=0; i<5; ++i)
{ Printing the six numbers.
Printf(“%i”,score[i]);
}
#include <stdio.h>
int main()
{
int nocols, norows, i, j;
int matrix1[10][10], matrix2[10][10], sum[10][10];
printf("Please enter the number of rows of matrix\n");
scanf("%d", &norows);
printf("Please enter number of columns of matrix\n");
scanf("%d", & nocols);
printf("Please enter the elements of first matrix one by one\n");
for ( i = 0 ; i < norows ; i++ )
{
for ( j = 0 ; j < nocols ; j++ )
{
scanf("%d", & matrix1 [i][j]);
}
}
printf("Please enter the elements of second matrix one by one\n");
for ( i = 0 ; i < norows ; i++ )
{
for ( j = 0 ; j < nocols ; j++ )
{
scanf("%d", & matrix2 [i][j]);
}
}
for ( i = 0 ; i < norows ; i++ )
{
for ( j = 0 ; j < nocols ; j++ )
{
sum[i][j] = matrix1 [i][j] + matrix2 [i][j];
}
}
printf("The sum of entered matrices is below:\n");
for ( i = 0 ; i < norows ; i++ )
{
for ( j = 0 ; j < nocols ; j++ )
printf("%d\t", sum[i][j]);
printf("\n");
}
return 0;
}
Output:
Please enter the number of rows of matrix
2
Please enter number of columns of matrix
2
Please enter the elements of first matrix one by one
3
3
3
3
Please enter the elements of second matrix one by one
4
4
4
4
The sum of entered matrices is below:
7 7
7 7