Sei sulla pagina 1di 7

1.

Write a program that calculates the sum of the elements of the primary and the
secondary diagonal of some matrix. The matrix is input from keyboard.

#include<stdio.h>
int main()
{
int i,j,n;
float a[100][100],s=0,s1=0;
printf("Enter the number of rows/columns: ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%f",&a[i][j]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)
s+=a[i][j];
if(i+j==n-1)
s1+=a[i][j];
}
printf("The sum of the elements of the primary diagonal is: %4.1f\n",s);
printf("The sum of the elements of the secondary diagonal is: %4.1f\n",s1);
return 0;
}

2. Write a program that swaps the biggest and the smallest element of some
matrix. The matrix is input from keyboard.

#include<stdio.h>
int main()
{
int a[100][100],i,j,n,m,iMax,iMin,jMax,jMin;
int max,min,temp;
printf("Enter the number of rows/columns: ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
min=max=a[0][0];
iMin=jMin=iMax=jMax=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
iMax=i;
jMax=j;
}
if(a[i][j]<min)
{
min=a[i][j];
iMin=i;
jMin=j;
}
}
temp=a[iMax][jMax];
a[iMax][jMax]=a[iMin][jMin];
a[iMin][jMin]=temp;
printf("The new matrix is:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("a[%d][%d]=%d\t",i,j,a[i][j]);
}
return 0;
}

3. Write a program that multiplies two matrices.

#include<stdio.h>
int main()
{
int i,j,n,m,k,p;
float a[100][100],b[100][100],c[100][100];
printf("Enter the number of rows of the 1st matrix: ");
scanf("%d",&n);
printf("Enter the number of columns of the 1st matrix = rows of the 2nd matrix: ");
scanf("%d",&m);
printf("Enter the number of columns of the 2nd matrix: ");
scanf("%d",&p);
printf("Enter the 1st matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%f",&a[i][j]);
}
printf("Enter the 2nd matrix:\n");
for(i=0;i<m;i++)
for(j=0;j<p;j++)
{
printf("b[%d][%d]=",i,j);
scanf("%f",&b[i][j]);
}
printf("Enter the 1st matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<p;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("The new matrix is:\n");
printf("Enter the 1st matrix:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<p;j++)
printf("c[%d][%d]=%3.1f\t",i,j,c[i][j]);
}
return 0;
}

4. Write a program that checks if the inputted matrix is symmetrical.

#include<stdio.h>
int main()
{
int a[25][25],sym=1,i,n,j;
printf("Enter the number of rows/columns: ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[i][j]!=a[j][i])
{
sym=0;
break;
}
}
}

printf("\nThe matrix:\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
}
if(sym)
printf("\nIs symmetrical");
else
printf("\nIs NOT symmetrical");
return 0;
}

5. Write a program that mirror swaps the elements according to the horizontal.

#include<stdio.h>
int main()
{
int a[100][100],i,n,j,temp;
printf("Enter the number of rows/columns: ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
printf("\nThe matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
for(i=0;i<n/2;i++)
for(j=0;j<n;j++)
{
temp=a[i][j];
a[i][j]=a[n-i-1][j];
a[n-i-1][j]=temp;
}
printf("\nMirror swap accordint to the horisontal is:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
return 0;
}

6. Write a program that shifts the elements in one matrix in this way:
1 2 3 9 1 2
4 5 6 3 4 5

7 8 9 6 7 8

#include<stdio.h>
int main()
{
int a[10][10],b[100],i,j,n,temp,count=0;
printf("Enter the number of rows/columns: <10 ");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
b[count]=a[i][j];
count++;
}
printf("\nOriginal matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
count--;
temp=b[count];
for(i=count;i>=1;i--)
b[i]=b[i-1];
b[0]=temp;
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
a[i][j]=b[count];
count++;
}
printf("\nRearranged matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
return 0;
}

7. Write a program that finds the smallest elements of each row of some matrix,
and then finds the biggest element of them. The matrix is input from keyboard.

#include<stdio.h>
int main()
{
int a[25][25],b[25],n,m,i,j,min,max,pos;
printf("Enter the number of rows: ");
scanf("%d",&m);
printf("Enter the number of columns: ");
scanf("%d",&n);
printf("Enter the matix:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf("a[%d][%d]=",i,j);
scanf("%d",&a[i][j]);
}
for(i=0;i<m;i++)
{
min=a[i][j];
for(j=0;j<n;j++)
if(a[i][j]<=min)
min=a[i][j];
b[i]=min;
}
max=b[0];
for(i=1;i<m;i++)
if(b[i]>max)
{
max=b[i];
pos=i;
}
printf("\nThe biggest value of the smallest values in each row\n");
printf("has the element in the %d-row, and it has value %d.\n",pos,max);
return 0;
}

Potrebbero piacerti anche