8 Multidimensional Arrays حل-1
8 Multidimensional Arrays حل-1
Faculty of Engineering
2nd Level Dep. of Electronics
Engineering
Programming Sheet # 8
(Multidimensional Arrays)
1) Write a program that declares an array of type (double) with size 3*3, and initializes the elements,
and then prints it on the screen.
#include<stdio.h>
main()
{
int i,j;
double x[3][3]={{1,2,3},{4,5,6},{7,8,9}};
printf("\nThe array x[3][3]=\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf("%lf\t",x[i][j]);
printf("\n");
}
}
2) Write a program that declares two arrays of type (int) with size 2*2, then takes the arrays elements
from the user, finally determines the summation and subtraction of the two arrays and prints the
results.
#include<stdio.h>
main()
{
int i,j;
int x[2][2],y[2][2],z[2][2],w[2][2];
for(i=0;i<=1;i++)
for(j=0;j<=1;j++)
{
z[i][j]=x[i][j]+y[i][j];
w[i][j]=x[i][j]-y[i][j];
}
printf("\n");
}
printf("\nx-y=\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
printf("%d\t",w[i][j]);
printf("\n");
}
}
3) Write a program that declares an array of type (float) with size 3×4, takes the array elements from
the user, then find the transpose of the array and prints the result.
#include<stdio.h>
main()
{
int i,j;
float x[3][4],T[3][4];
printf("\n");
}
for(i=0;i<=3;i++)
for(j=0;j<=2;j++)
T[i][j]=x[j][i];
printf("\n");
}
}
Sheet #8 - Multidimensional Arrays Eng. Ali Al-Ashwal 2
4) Write a program that declares two arrays of type (int) with size 2*2, then takes the arrays elements
from user, finally determines the product of the two arrays and prints the results.
#include<stdio.h>
main()
{
int i,j,k;
int x[2][2],y[2][2],z[2][2];
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
{
z[i][j]=0;
for(k=0;k<=1;k++)
z[i][j]+=x[i][k]*y[k][j];
}
}
printf("\nx*y=\n");
for(i=0;i<=1;i++)
{
for(j=0;j<=1;j++)
printf("%d\t",z[i][j]);
printf("\n");
}
}
for(i=0;i<=4;i++)
{
printf("Enter three degrees of student (%d):\n",i+1);
for(j=0;j<=2;j++)
scanf("%f",&x[i][j]);
}
printf("\n\tdeg(1)\tdeg(2)\tdeg(3)\tsum\taverage\n");
for(i=0;i<=4;i++)
{
sum=0;
printf("stud(%d)\t",i+1);
for(j=0;j<=2;j++)
{
printf("%.2f\t",x[i][j]);
sum+=x[i][j];
}
printf("%.2f\t%.2f\t",sum,sum/3);
printf("\n");
printf("---------------------------------------------");
printf("\n");
}
}