Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

ARRAYS

An array is a data structure in which a collection of data items of the same data type are stored.
Characteristic of Arrays

 Data items of the same data type are stored.


 Items are referenced by index
 Multi-dimensional array has several rows and columns

There exist the following types of arrays:-


(i) 1-Dimension array(Has X co-ordinates)
(ii) 2- Dimension array(Has X Y co-ordinates)
(iii) 3- Dimension array(Has XYZ co-ordinates)
(iv) Nth- Dimension array(Has nth co-ordinates)

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:

int table[5][5][20]; /* array can hold 500 integer-type elements */

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

Int score[6]; / * Array called score having 6 elements of integer type */

READING elements in an array


Use a for loop
e.g
For(i=0; i<5; ++i)
{
Printf(“Enter a number”);
Scanf(“%i”,&score[i]);
}

OUTPUTTING elements in an array


Use a for loop
e.g
For(i=0; i<6; ++i)
{
Printf(“%i”,score[i]);
}
Develop a C program to read six integer values into an Array called Score then display the
numbers and the sum.

#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]);
}

Printf(“sum is %i” , sum);


}
C program for matrix addition  

#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
 

You might also like