ME 172
Array
2
One dimensional array
Declaration
type variable_name [size];
Example: int numb1[10];
float numb2[15];
3
Taking inputs of a one dimensional array
#include<stdio.h>
void main (void)
{
int i[5], j;
for( j=0; j<5; j++)
{
scanf(“%d”,&i[j]);
}
for( j=0; j<5; j++)
{
printf(“i[%d]=%d\n”,j,i[j]);
}
getch( );
}
Input: 10 1 5 6 8
i[0] i[1] i[2] i[3] i[4]
4
Example:Array-1
Write a program that will take 10 numbers as input
and find average of them
int i[10], j,sum=0;
for( j=0; j<10; j++)
scanf("%d",&i[j]);
printf("\n");
for( j=0; j<10; j++)
sum+=i[j];
printf("The average of:");
for( j=0; j<10; j++)
printf("%3d",i[j]);
printf("=%d",sum/10);
5
Two dimensional array
Declaration:
type variable_name [row size][column size];
Example: int matrix [5] [5];
float number[10][15];
6
Example: Taking inputs of a two dimensional array
#include<stdio.h>
void main (void)
{
int i[2][3], j, k; i[0][0] i[0][1] i[0][2]
for( k=0; k<3; k++)
{ i[1][0] i[1][1] i[1][2]
for( j=0; j<2; j++)
{
scanf(“%d”,&i[j][k]);
}
}
getch( );
}
Input: 1 2 3 4 5 6
i[0][0] i[1][0] i[0][1] i[1][1] i[0][2] i[1][2]
7
Exercise :two dimensional Array
Write a program that declares a 3×4 array and will take the following
matrix elements as input and prints them on the screen as follows:
Input 1 2 3 4 5 6 7 8 9 10 11 12
Output
1 4 7 10
2 5 8 11
3 6 9 12
8
Solution
int i[3][4], j, k;
for( j=0; j<3; j++)
{ for( k=0; k<4; k++)
{ scanf("%d",&i[j][k]); }}
for( j=0; j<3; j++)
{
for( k=0; k<4; k++)
{
printf("%5d",i[j][k]);
}
printf("\n");
}
9
Exercise
Write a program that will take a number as
input and find summation of all the digits
of the number.
991%10=1
Example: 991
991/10=99
Summation of all the digit is 9+9+1=19
10
Solve
printf("Enter the number(integer)");
scanf("%d",&a);
b=a;
for(i=1;;i++)
{
c[i]=b%10;
b=b/10;
s+=c[i];
if(b==0)
break;
}
printf("The summation of all the digit of the number %d is %d",a,s);
11
array passed to a function
main()
{
const int n=5;
int a[n];
func_name(a,n);
………...
}
int func_name(int a[ ],int n)
{
statements;
}
12
Now, do the same problem of Exercise
1 using function
#define R 3
#define C 3
void two_d_array(int a[R][C]);
void main (void)
{int i,j,k,n,b[R][C];
printf("Enter elements row wise");
for(i=0;i<R;i++)
for(j=0;j<C;j++)
scanf("%d",&b[i][j]); getch();
two_d_array(b);
getch();
}
Continued to the next page
void two_d_array(int a[R][C])
{
int i,j;
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}}
14
Write a program that will take the elements of a 3×4 matrix
as input and finds the transpose of that matrix.
print both matrices on the screen.
Hints:
0 4 2
0 5 6 15 5 7
Transpose
4 3
3 1 11
6 1 8
2 7 8 9
3×4 15 11 9 4×3
#define R 3
#define C 4
void transpose(int a[R][C]);
void main (void)
{
int i,j,k,n,b[R][C];
printf("Enter elements row wise");
for(i=0;i<R;i++)
for(j=0;j<C;j++)
scanf("%d",&b[i][j]);
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
{
printf("%5d",b[i][j]);
}printf("\n\n");
}
void transpose(int a[R][C])
{
int c[C][R],i,j;
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
c[j][i]=a[i][j];
}
printf("The transposed matrix:\n");
for(i=0;i<C;i++)
{
for(j=0;j<R;j++)
printf("%4d",c[i][j]);
printf("\n");
}
}
17
Exercise
Write a C program for the multiplication of two matrices
5[0][0] 3[0][1] 6[0][0] 7[0][1]
4[1][0] 2[1][1] 4[1][0] 8[1][1]
5*6+3*4 5*7+3*8
[0][0] [0][1]
4*6+2*4 4*7+2*8
[1][0] [1][1]
18
Exercise on 2D Array
(solution)
#include<stdio.h>
#include<math.h>
main(void)
{ int a[20][20],b[20][20],c[20][20];
int i,j,k,r1,c1,r2,c2;
printf("Give No of Rows and Columns of Matrix1 \n");
scanf("%d %d", &r1, &c1);
printf("\nGive No of Rows and Columns of Matrix2 \n");
scanf("%d %d",&r2,&c2);
if(c1!=r2)
printf("Reverse dimension of Matrix ");
continued to the next page……..
19
……..continued from the previous page
else {
printf("put the Elements of Matrix 1 \n");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[ i ][ j ] );
printf("put the Elements of matrix 2 \n");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++) scanf("%d",&b[ i ][ j ]);
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
{ c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
continued to the next page……..
20
……..continued from the previous page
printf("Out put\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
getch();
}
21
Write a program that will take a 2 2 matrix as input
in the calling function (main function) and find it’s
determinant using different function. Print the input
matrix and output in the main function.