Professional Documents
Culture Documents
Array
Array
• Using arrays, other data structures like linked lists, stacks, queues, trees, graphs etc
can be implemented.
• Two-dimensional arrays are used to represent matrices.
• Arrays represent multiple data items of the same type using a single name.
• In arrays, the elements can be accessed randomly by using the index number.
• It is better and convenient way of storing the data of same datatype with same size.
• It allows us to store known number of elements in it.
• It allocates memory in contiguous memory locations for its elements. It does not
allocate any extra space/ memory for its elements. Hence there is no memory
overflow or shortage of memory in arrays.
Disadvantage of Array
One Dimensional
Multi Dimensional
One Dimensional Array
• single dimensional arrays are used to store a row of values. In single dimensional
array, data is stored in linear form.
• Single dimensional arrays are also called as one-dimensional arrays, Linear
Arrays or simply 1-D Arrays.
• An array which has only one subscript is termed as one dimensional array.
• For example:
• Char name[25];
Declaration of one dimensional array
• Data_type array_name[array_size];
Syntax:
data_type array_name[array_size];
Example:
char name[25];
int a[10];
float temperature[25];
float marks[25];
char fname[20];
Array Initialization :One dimensional array
• Syntax:
Data_type array_name[array_size]={val1,val2,val3,………,valn};
For example:
int marks[4]={76,78,98,55};
#include <stdio.h> }
void main()
{ printf("\nElements in array are:
int arr[10]; ");
int i; for(i=0; i<10; i++)
printf("Input 10 elements in the {
array :\n"); printf("%d ", arr[i]);
for(i=0; i<10; i++) }
{ printf("\n");
printf("element - %d : ",i); }
scanf("%d", &arr[i]);
WRITE A PROGRAM TO READ AGE OF N PERSONS AND DISPLAY ONLY
THOSE PERSONS WHOSE BETWEEN 50 AND 60.
#include<stdio.h> }
#include<conio.h>
void main() for (i=1;i<=n;i++)
{ {
int i,n,age[100],count=0; if(age[i]>50 && age[i] < 60)
printf("Enter the number of persons :: "); count++;
scanf("%d",&n); }
for (i=1;i<=n;i++) printf("\n\nNumber of persons whose age
{ between 50-60 are :: %d",count);
printf("\nEnter age of %d persons :: ",i); getch();
scanf("%d",&age[i]); }
WAP to input ‘n’ numbers and sort them in ascending order
For example:
int a[m][n];
Here a[0][0] is initial and a[m-1][n-1] is the last element.
Initialization of multi dimensional array
2-D array can be initialized in a way similar to that of 1-D array. for example:-
int mat[4][3]={11,12,13,14,15,16,17,18,19,20,21,22};
These values are assigned to the elements row wise, so the values of
elements after this initialization are
Mat[0][0]=11, Mat[1][0]=14, Mat[2][0]=17 Mat[3][0]=20
Mat[0][1]=12, Mat[1][1]=15, Mat[2][1]=18 Mat[3][1]=21
Mat[0][2]=13, Mat[1][2]=16, Mat[2][2]=19 Mat[3][2]=22
• While initializing we can group the elements row wise using inner
braces.
for example:-
int mat[4][3]={{11,12,13},{14,15,16},{17,18,19},{20,21,22}};
• If we initialize an array as
int mat[4][3]={{11},{12,13},{14,15,16},{17}};
Then the compiler will assume its all rest value as 0,which are not defined.
Mat[0][0]=11, Mat[1][0]=12, Mat[2][0]=14, Mat[3][0]=17
Mat[0][1]=0, Mat[1][1]=13, Mat[2][1]=15 Mat[3][1]=0
Mat[0][2]=0, Mat[1][2]=0, Mat[2][2]=16, Mat[3][2]=0
WAP to input data in 2-dimensional array and display in matrix form
#include <stdio.h> }
void main() }
{ printf("Transpose of matrix is \n");
int array[10][10]; for (j = 0; j < n; ++j)
int i, j, m, n; {
printf("Enter the order of the matrix \n"); for (i = 0; i < m; ++i)
scanf("%d %d", &m, &n); {
for (i = 0; i < m; ++i) printf(" %d", array[i][j]);
{ }
for (j = 0; j < n; ++j) printf("\n");
{ }
scanf("%d", &array[i][j]); }
WAP to add two matrices
#include <stdio.h> for(j=0;j<3;j++)
#include <conio.h> c[i][j]=a[i][j]+b[i][j];
void main() printf("\nTHE VALUES OF MATRIX C
{ ARE:\n");
int a[2][3],b[2][3],c[2][3],i,j; for(i=0;i<2;i++)
clrscr(); for(j=0;j<3;j++)
printf("\nENTER VALUES FOR printf("%5d",c[i][j]);
MATRIX A:\n"); printf("\n");
for(i=0;i<2;i++) getch();
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nENTER VALUES FOR
MATRIX B:\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<2;i++)
Assignments