ARRAY IMPLEMENTATION:
Syntax:
Definition of Array:
Collection
of elements of slimier Data types is known as Array.
i.e with in one
Name we can store any number of
elements.
Syntax:
Declaration :
Ex:int a[5];
float b[3];
Initialization :
Datatype Arrayname [size]={elements...};
Ex:- int a[5]={2,6,8,7,8};
Elements
|
2
|
6
|
8
|
7
|
8
|
Index
|
0
|
1
|
2
|
3
|
4
|
- Memory will be allocated continuously for all the elements.
- Each element is identified with the help of index number.
- The index of the array starts from ‘0’ and end at ‘size-1’.
- The size of the array must be constant.
/*3.Write a C program to
Demonstrate one Dimensional Array */
|
/*4.Write a C program to
Demonstrate one
Dimensional Array
using loops */
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[5]={9,4,6,2,5};
clrscr();
printf(“\nsizeof
a=%d bytes”,sizeof(a));
printf(“\a[0]=%d”,a[0]);
printf(“\a[1]=%d”,a[1]);
printf(“\a[2]=%d”,a[2]);
printf(“\a[3]=%d”,a[3]);
printf(“\a[4]=%d”,a[4]);
getch();
}
Result:
Output:
Size
of a=10 bytes
a[0]=9
a[1]=4
a[2]=6
a[3]=2
a[4]=5
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[5]={9,4,6,2,5};
int
i;
clrscr();
printf(“\nsizeof
a=%d bytes”,sizeof(a));
for(i=0;i<5;i++)
{
printf(“\na[%d]=%d”,i,a[i]);
}
getch();
}
Result:
Output:
Size
of a=10 bytes
a[0]=9
a[1]=4
a[2]=6
a[3]=2
a[4]=5
|
/*.Write a C program to accept the 5
Integer to an one Dimensional Array
and Display it */
|
/*.Write a C program to accept the
n integers to an one Dimensional
Array and Display it */
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[5];
int
i;
clrscr();
for(i=0;i<5;i++)
{
printf(“\nEnter
%d Element:”,i+1);
scanf(“%d”,&a[i]);
}
for(i=0;i<5;i++)
{
printf(“\na[%d]=%d”,i,a[i]);
}
getch();
}
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[5];
inti,n;
clrscr();
printf(“enter
the value of n:”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter
%d Element:”,i+1);
scanf(“%d”,&a[i]);
}
for(i=0;i<n;i++)
{
printf(“\na[%d]=%d”,i,a[i]);
}
getch();
}
|
Result:
Input:
Enter
1 Element:21
Enter
2 Element:63
Enter
3 Element:52
Enter
4 Element:4
Enter
5 Element:9
Output:
a[0]=21
a[1]=63
a[2]=52
a[3]=4
a[4]=9
|
Result:
Input:
Enter the value of n:3
Enter
1 Element:2
Enter
2 Element:6
Enter
3 Element:5
Output:
a[0]=2
a[1]=6
a[2]=5
|
/*Write a C program to accept the n
Integers Display reverse order */
|
/*.Write a C program to accept the
n integers Display Ascending order
*/
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[20],i,n,temp;
clrscr();
printf("Enter
the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter
%d element:",i+1);
scanf("%d",&a[i]);
}
printf("\noriginal
order:");
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
for(i=0;i<n/2;i++)
{
temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;
}
printf("\nReverse order:");
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
getch();
}
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[20],i,j,n,temp;
clrscr();
printf("Enter
the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter
%d element:",i+1);
scanf("%d",&a[i]);
}
printf("\noriginal order:");
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nAscending order:");
for(i=0;i<n;i++)
{
printf("%3d",a[i]);
}
getch();
}
|
Result:
Input:
Enter
the value of n:5
Enter
1 element:9
Enter
2 element:6
Enter
3 element:4
Enter
4 element:1
Enter
5 element:3
Output:
original order: 9 6
4 1 3
Reverse order: 3 1
4 6 9
|
Result:
Input:
Enter
the value of n:5
Enter
1 element:21
Enter
2 element:55
Enter
3 element:41
Enter
4 element:78
Enter
5 element:3
Output:
original order: 21 55 41 78 3
Ascending order: 3 21 41 55 78
|
Two Dimensional Arrays:
Collection
of elements of one dimensional Array is known as a two dimensional Array.
Syntax:
Declaration :
Datatype
Arrayname [row size][col size];
Ex:int a[5][3];
Initialization :
Datatype
Arrayname [size]={elements...};
Datatype Arrayname [size]={{row
elements},{row elements}...};
Ex:- int
a[3][3]={2,6,8,7,9,1,5,4,3};
int
a[3][3]={{2,6,8},{7,9,1},{5,4,3}};
columns
|
||||
r
o
w
s
|
a
|
0
|
1
|
2
|
0
|
2
|
6
|
8
|
|
1
|
7
|
9
|
1
|
|
2
|
5
|
4
|
3
|
Output:
a[0][0]=2, a[0][1]=6…..,a[2][2]=3
/*Write a C program to demonstrate two
Dimensional Array and display in the form of
matrices */
|
/*Write a C program to accept the elements for
two Dimensional Array and display in the form
of 3x3 matrices */
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[3][3]= {{2,6,8},{7,9,1},{5,4,3}};
int
i,j;
clrscr();
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%3d",a[i][j]);
}
}
getch();
}
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[3][3],i,j;
clrscr();
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("enter
%d row %d column Element:",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%3d",a[i][j]);
}
}getch();
}
|
Result:
Output:
2
6 8
7
9 1
5
4 3
|
Result:
Input:
enter
1 row 1 column Element:5
enter
1 row 2 column Element:9
enter
1 row 3 column Element:4
enter
2 row 1 column Element:3
enter
2 row 2 column Element:2
enter
2 row 3 column Element:1
enter
3 row 1 column Element:4
enter
3 row 2 column Element:6
enter
3 row 3 column Element:3
Output:
5
9 4
3
2 1
4
6 3
|
/*Write a C program to accept the elements for
two Dimensional Array and display in the form
of 3x3 matrices */
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[10][10],i,j,r,c;
clrscr();
printf("enter
row and column Size:");
scanf("%d%d",&r,&c);
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("enter
%d row %d column Element:",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
{
printf("%3d",a[i][j]);
}
}
getch();
}
|
Result:
Input:
enter
row and column Size:2
3
enter
1 row 1 column Element:8
enter
1 row 2 column Element:5
enter
1 row 3 column Element:4
enter
2 row 1 column Element:1
enter
2 row 2 column Element:9
enter
2 row 3 column Element:6
8
5 4
1
9 6
|
/*Write a C program to find sum of two
matrices */
|
/*Write a C program to find product of two
matrices */
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[10][10],b[10][10],c[10][10];
int
r1,c1,r2,c2,r3,c3,i,j;
clrscr();
printf("\nEnter
row and column Size Matrix A:");
scanf("%d%d",&r1,&c1);
printf("\nEnter
row and column Size Matrix B:");
scanf("%d%d",&r2,&c2);
if(r1==r2&&c1==c2)
{
printf("\nenter elements for Matrix A:");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
{
printf("enter
%d row %d column Element:",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nenter elements for Matrix B:");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("enter
%d row %d column Element:",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
r3=r1;//r3=r2;
c3=c1;//c3=c2;
for(i=0;i<r3;i++)
{
for(j=0;j<c3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nMatrix A:");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
{
printf("%3d",a[i][j]);
}
}
printf("\nMatrix B:");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%3d",b[i][j]);
}
}
printf("\n Addition of Matrix
A and B:");
for(i=0;i<r3;i++)
{
printf("\n");
for(j=0;j<c3;j++)
{
printf("%3d",c[i][j]);
}
}
}
else
{
printf("\nMatrix
Addition is not possible");
}
getch();
}
|
#include<stdio.h>
#include<conio.h>
void
main()
{
int a[10][10],b[10][10],c[10][10];
int
r1,c1,r2,c2,r3,c3,i,j,k;
clrscr();
printf("\nEnter
row and column Size of Matrix A:");
scanf("%d%d",&r1,&c1);
printf("\nEnter
row and column Size of Matrix B:");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("\nenter Element for
Matrix A:");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
{
printf("enter
%d row %d column Element:",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nenter Element for
Matrix B:");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("enter
%d row %d column Element:",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
r3=r1;
c3=c2;
for(i=0;i<r3;i++)
{
for(j=0;j<c3;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nMatrix A:");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
{
printf("%3d",a[i][j]);
}
}
printf("\nMatrix B:");
for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%3d",b[i][j]);
}
}
printf("\nproduct of Matrix A
and B:");
for(i=0;i<r3;i++)
{
printf("\n");
for(j=0;j<c3;j++)
{
printf("%3d",c[i][j]);
}
}
}
else
{
printf("\nMatrix
product is not possible");
}
getch();
}
|
Result:
Input:
Enter
row and column Size Matrix A:2
2
enter
elements for Matrix A:
enter
1 row 1 column Element:1
enter
1 row 2 column Element:2
enter
2 row 1 column Element:3
enter
2 row 2 column Element:4
enter
elements for Matrix B:
enter
1 row 1 column Element:5
enter
1 row 2 column Element:6
enter
2 row 1 column Element:7
enter
2 row 2 column Element:8
Output:
Matrix A:
1 2
3 4
Matrix B:
5 6
7 8
Addition of Matrix
A and B:
6 8
10 12
|
Result:
Input:
Enter
row and column Size of Matrix A:2
2
Enter
row and column Size of Matrix B:2
2
enter
element for Matrix A:
enter
1 row 1 column element:1
enter
1 row 2 column element:1
enter
2 row 1 column element:1
enter
2 row 2 column element:1
enter
element for Matrix B:
enter
1 row 1 column element:1
enter
1 row 2 column element:1
enter
2 row 1 column element:1
enter
2 row 2 column element:1
Output:
Matrix A:
1 1
1 1
Matrix B:
1 1
1 1
product of Matrix A
and B:
2 2
2 2
|
No comments:
Post a Comment