To Be Familiarize With Storage Class and 2D Array
- WAP to show concept of global variables
- WAP to show concept of static variables
- WAP to find the sum of all the diagonals elements of 3*3 matrix you entered
- WAP to find the transpose of 3*3 matrix you entered
- WAP to enter elements in a 2D 3*3 matrix and check the multiplication can be done or not
- WAP to find the product of two Matrices. Check whether the multiplication can be done or not.
================================================================
1.) Question 1
#include <stdio.h>
#include <conio.h>
void a();
int i=99;
void main ()
{
//int i=89;
clrscr ();
a();
printf ("\n%d",i);
getch ();
}
void a ()
{
printf ("%d",i);
}
===================================================================
2.) Question 2
#include <stdio.h>
#include <conio.h>
void display ();
void main ()
{
int i;
clrscr ();
for (i=1;i<=3;i++)
{
display ();
}
getch ();
}
void display ()
{
static int a=0;
a++;
printf ("%d\n",a);
}
=========================================================================
3.) Question 3
#include <stdio.h>
#include <conio.h>
void main ()
{
int a[3][3],i,j,d=0;
clrscr ();
printf ("Enter elements for matrix:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf ("Enter data a%d%d: ",i+1,j+1);
scanf ("%d",&a[i][j]);
}
}
printf ("Your matrix is:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf ("%d ",a[i][j]);
}
printf ("\n");
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (i==j)
d=d+a[i][j];
}
}
printf ("The diagonal sum is %d.\n",d);
getch ();
}
===========================================================================
4.) Question 4
#include <stdio.h>
#include <conio.h>
void main ()
{
int a[3][3],i,j;
clrscr ();
printf ("Enter elements for matrix:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf ("Enter data a%d%d: ",i+1,j+1);
scanf ("%d",&a[i][j]);
}
}
printf ("Your matrix is:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf ("%d ",a[i][j]);
}
printf ("\n");
}
printf ("The transpose is: \n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (i==j)
printf ("%d ",a[i][j]);
else
printf ("%d ",a[j][i]);
}
printf ("\n");
}
getch ();
}
====================================================================
5.) Question 5
#include <stdio.h>
#include <conio.h>
void main ()
{
int a[3][3],i,j,c=0;
clrscr ();
printf ("Enter elements for matrix:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf ("Enter data a%d%d: ",i+1,j+1);
scanf ("%d",&a[i][j]);
}
}
printf ("Your matrix is:\n");
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
printf ("%d ",a[i][j]);
}
printf ("\n");
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
if (a[i][i]==1||a[i][j]==0)
c++;
}
}
if (c==9)
printf ("Identity. \n");
else
printf ("Not Identity. \n");
getch ();
}
====================================================================
6.) Question 6
#include <stdio.h>
#include <conio.h>
void main ()
{
int a[10][10],b[10][10],op[10][10],r1,c1,r2,c2,i,j,k;
clrscr ();
printf("Enter row for 1st matrix:\n");
scanf ("%d",&r1);
printf("Enter column for 1st matrix:\n");
scanf ("%d",&c1);
printf("Enter row for 2nd matrix:\n");
scanf ("%d",&r2);
printf("Enter column for 2nd matrix:\n");
scanf ("%d",&c2);
while (c1!=r2)
{
printf ("*Error* Matrix multiplication cannot be done.\n");
printf("Enter row for 1st matrix:\n");
scanf ("%d",&r1);
printf("Enter column for 1st matrix:\n");
scanf ("%d",&c1);
printf("Enter row for 2nd matrix:\n");
scanf ("%d",&r2);
printf("Enter column for 2nd matrix:\n");
scanf ("%d",&c2);
}
printf ("Enter elements for 1st matrix:\n");
for (i=0;i<r1;i++)
{
for (j=0;j<c1;j++)
{
printf ("Enter data a%d%d: ",i+1,j+1);
scanf ("%d",&a[i][j]);
}
}
printf ("Your 1st matrix is:\n");
for (i=0;i<r1;i++)
{
for (j=0;j<c1;j++)
{
printf ("%d ",a[i][j]);
}
printf ("\n");
}
printf ("Enter elements for 2nd matrix:\n");
for (i=0;i<r2;i++)
{
for (j=0;j<c2;j++)
{
printf ("Enter data a%d%d: ",i+1,j+1);
scanf ("%d",&b[i][j]);
}
}
printf ("Your 2nd matrix is:\n");
for (i=0;i<r2;i++)
{
for (j=0;j<c2;j++)
{
printf ("%d ",b[i][j]);
}
printf ("\n");
}
for (i=0;i<r1;i++)
{
for (j=0;j<c2;j++)
{
op[i][j]=0;
}
}
for (i=0;i<r1;i++)
{
for (j=0;j<c2;j++)
{
op[i][j]+=a[i][j]*b[i][j];
}
}
printf ("Your result matrix is:\n");
for (i=0;i<r1;i++)
{
for (j=0;j<c2;j++)
{
printf ("%d ",op[i][j]);
}
printf ("\n");
}
getch ();
}
No comments