mod1_array
mod1_array
•An array is a collection of data that holds fixed number of values of same type.
•The size and type of arrays can not be changed after its declaration.
• Example: If you want to store marks of100 students you can create an array for it.
float marks[100];
Types of Arrays
1.One-dimensionalarrays.
2.Two-dimensionalarrays.
3.Multidimensionalarrays.
•A list of item can be given one variable name using only one subscript and such a variable is
called a single subscripted variable or a one-dimensional array.
•Example:
float height[50];
int group[10];
char name[10];
Array Declaration
The data type specifies the type of the element that will be contained in the array, such as int,
float, or char and the size indicates the maximum number of elements that can be stored inside
the array.
int mark[5];
•Here we declared an array, with array name mark, type of array int for storing 5 values.
Array Initialization
Method 1
int mark[5] = {78,45,12,89,56};
Method 2
int mark [ ] = {78,45,12,89,56};
Here, mark[0] is equal to 78
mark[1] is equal to 45
mark[2] is equal to 12
mark[3] is equal to 89
mark[4] is equal to 56
#include<stdio.h>
void main()
{
int array[5], i;
//Reading elements of the array
printf("Enter 5 numbers to store them in array \n");
for(i=0; i<5; i++)
{
scanf("%d", &array[i]);
}
//Displaying the elements of the array
printf("Element in the array are: \n");
for(i=0; i<5;i++)
{
printf("Element stored at a[%d] =%d \n", i, array[i]);
}
}
Write a program to read the elements of a 1D array from user and add 10 to all elements in
the array.
#include<stdio.h>
void main()
{
int array[50], i, n;
printf("Enter the number of elements to be stored in the array");
scanf("%d", &n);
printf("Enter the numbers to store them in array \n");
for(i=0; i<n; i++)
{
scanf("%d", &array[i]);
array[i]=array[i]+10;
}
printf("Element in the array after adding 10 \n");
for(i=0; i<n; i++)
{
printf("%d \n", array[i]);}
}
}
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
for (i = 0; i < n; ++i)
{
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
#include<stdio.h>
void main()
{
int n, a[20],i;
printf("Enter the size of Array");
scanf("%d", &n);
printf("Enter the number");
for(i=0; i<n; i++)
{
scanf("%d", &a[i]);
}
for(i=0; i<n;i++)
{
if(a[i]%2 == 0)
{
printf("%d\t", a[i]);
}
}
}
Write a program to read the elements of a 1D array from user and add 10 to all odd numbers
in the array.
Two-dimensionalArrays:
A two dimensional array can be considered as a table which will have m number of rows and n
number of columns.
•type arrayName[x][y];
•Eg: int x[3][5]
A two dimensional array x which contains three rows and three columns can be shown as
follows Two dimensional Arrays:
Thus,every element in the array a is identified by an element name of the form x[i][j], where ‘x’ is
the name of the array, and ‘i’ and ‘j’ are the subscripts that uniquely identifies each element in
‘x’.
Initializing Two-DimensionalArrays:
#include<stdio.h>
int main()
{
Int a[5][2] ={{0,0},{1,2},{2,4},{3,6},{4,8}};
int i, j;
for(i=0;i<5;i++)
{
for(j=0; j<2;j++)
{
printf(“a[%d] [%d] =%d\n”,i,j,a[i][j]);
}
}
return0;
}
Output:
a[0][0]:0
a[0][1]:0
a[1][0]:1
a[1][1]:2
a[2][0]:2
a[2][1]:4
a[3][0]:3
a[3][1]:6
a[4][0]:4
a[4][1]:8
#include<stdio.h>
void main()
{
/* 2D array declaration*/
int a[3][2];
/*Counter variables for the loop*/
int i, j;
/*Reading elements of 2D array*/
printf("Enter the elements of 2D array");
for(i=0; i<3; i++)
{
for(j=0;j<2;j++)
{
scanf("%d", &a[i][j]);
}
}
/*Displaying elements of 2D array*/
printf("The elements of 2D array are\n");
for(i=0; i<3; i++)
{
for(j=0;j<2;j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
}
Matrix Addition
#include <stdio.h>
void main()
{
int r1, c1, r2, c2,i,j ,a[10][10], b[10][10], sum[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &r1, &c1);
printf("Enter the elements of first matrix\n");
for (i= 0; i< r1; i++)
for (j= 0; j < c1; j++)
scanf("%d", &a[i][j]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &r2, &c2);
printf("Enter the elements of second matrix\n");
for (i= 0; i< r2; i++)
for (j= 0; j < c2; j++)
scanf("%d", &b[i][j]);
if(c1==c2 && r1==r2)
{
printf("Sum of entered matrices:-\n");
for (i= 0; i< r1; i++)
{
for (j= 0; j < c1; j++)
{
sum[i][j] = a[i][j] + b[i][j];
printf("%d\t", sum[i][j]);
}
printf("\n");
}
}
else
printf("The matrices are not conformable for addition");
}
#include<stdio.h>
int main()
{
int mat[12][12];
int i,j,row,col,sum=0;
printf("Enter the number of rows and columns for 1st matrix\n");
scanf("%d%d",&row,&col);
printf("Enter the elements of the matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("The matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
//To add diagonal elements
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum=sum+mat[i][j];
}
}
}
printf("The sum of diagonal elements of a square matrix = %d\n",sum);
return 0;
}
********************************************************************************