Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

Friday, 24 May 2013

Multiplication of two matrices


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],b[3][3],mul[3][3];
printf("Enter the elements of array A\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
printf("\n");
}

printf("\nEnter the elements of array B\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array A are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,a[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array B are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,b[i][j]);
}
printf("\n");
}


printf("\n\nMultiplication of two matrices is\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
mul[i][j]=a[i][j]*b[i][j];
printf("\na[%d][%d] = %d ",i,j,mul[i][j]);

}
printf("\n");
}

getch();
}

Diffference of two matrices


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],b[3][3],diff[3][3];
printf("Enter the elements of array A\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
printf("\n");
}

printf("\nEnter the elements of array B\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array A are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,a[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array B are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,b[i][j]);
}
printf("\n");
}


printf("\n\ndiffrence of two matrices is\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
diff[i][j]=a[i][j]-b[i][j];
printf("\na[%d][%d] = %d ",i,j,diff[i][j]);

}
printf("\n");
}

getch();
}

Addition of two Matrices


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[3][3],b[3][3],sum[3][3];
printf("Enter the elements of array A\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
printf("\n");
}

printf("\nEnter the elements of array B\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = ",i,j);
scanf("%d",&b[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array A are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,a[i][j]);
}
printf("\n");
}

printf("\n\nThe elements of array B are\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\na[%d][%d] = %d ",i,j,b[i][j]);
}
printf("\n");
}


printf("\n\nSum of two matrices is\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum[i][j]=a[i][j]+b[i][j];
printf("\na[%d][%d] = %d ",i,j,sum[i][j]);

}
printf("\n");
}

getch();
}
                               

Monday, 20 May 2013

Insert an element at desired position in array



#include<stdio.h>
#include<conio.h>
void main()
{
 clrscr();
 int a[10]={22,33,44,55,77,88};
 int i,j,k,n,t,x,z=0;
 printf("The elements of array are");
 for(i=0;i<=5;i++)
 {
   printf("\na[%d]= %d",i,a[i]);
 }
 printf("\nenter the location where you want to insert the element in the array");
 scanf("%d",&k);

 for(j=i-1;j>=k;j--)
  {
   a[j+1]=a[j];
   printf("\nNow a[%d] contains %d",j+1,a[j]);
  }
 printf("\nNow a[%d] contains %d",j+1,z);

 for(j=k-1;j>=0;j--)
 {
  printf("\nNow a[%d] contains %d",j,a[j]);
 }
 printf("enter the element to be inserted into the array");
 scanf("%d",&n);
 a[k]=n;
 t=i+1;
 printf("\n\nNew array after insertion is\n\n");
 for(i=0;i<t;i++)
  {
  printf("\nNow a[%d] contains %d",i,a[i]);
 }
 getch();
}





Saturday, 18 May 2013

Insertion of an element at last of array

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]={1,2,3,4,5};
int n;
printf("\nAt present elements of array are");
for(int i=0;i<=4;i++)
{
printf("\n%d",a[i]);
}
printf("\nEnter the element you want to insert");
scanf("%d",&n);
a[i]=n;
printf("\n New array after insertion is");
for(i=0;i<=5;i++)
{
printf("\n%d",a[i]);
}
getch();
}

Thursday, 16 May 2013

Accessing array elements using base address


#include<stdio.h>
#include<conio.h>
void main()
{
int data[5]={1,2,3,4,5};
int i;
int *base_add;
base_add=data;
clrscr();
printf("\t ACCESSING ARRAY ELEMENT USING BASE ADDRESS\N");
printf("\nHere base address is %u",base_add);
for(i=0;i<=4;i++)
{
printf("\nAt address= %u",base_add);
printf("\nWe have data[%d] i.e %d",i,*base_add);
base_add++;
}
getch();
}

Traversing an Array

Traversing an array means to access each and very element of array .It can be accessed using for loops.The data stored in array can be travesred by using for loop and the elements can be accessed by incerement the for loop. The loop starts to traverse array from the start of the array and continue to traverse array until the for lopp continues its execution until the for loop ends.UIn the program below the elements are stored in an array and are travesred using for loop . The loop prints all the elements of array using traversing.
 

Program for traversing an array.

#include<iostream.h>
#include<conio.h>
void main()
{
int data[5]={1,2,3,4,5};
int i;
clrscr();
cout<<""Elements of array Data are";
for(i=0;i<=4;i++)
{
cout<<data[i];
}
getch();
}

 

full details in json
Proudly Powered by Blogger.
back to top