第一题#include
void shangsan(int (*p)[3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==1&&j==0)
printf("%c",32);
else if(i==2&&(j==0||j==1))
printf("%c",32);
else
printf("%d",(*(p+i))[j]); }
printf("\n");
} }
void main()
{ int s[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
scanf("%d",&s[i][j]);
shangsan(s);}第二题#include
int mystrcmp(char *p1,char *p2)
{ for(int i=0;i<15;i++,p1++,p2++)
{ if(*p1!=*p2)
return *p1-*p2;
}
return 0;
}
void main()
{
int M;
char s1[15],s2[15];
gets(s1);
gets(s2);
M=mystrcmp(s1,s2);
if(M==0)
printf("字符串相等!");
else
printf("字符串不相等!差值是:%d",M); }第三题#include
float HH(float score[])
{ float Max=0,Min=32767,sum=0;
for(int i=0;i<10;i++)
{
sum+=score[i];
if(score[i]>Max)
Max=score[i];
else if(Min>score[i])
Min=score[i];
}
score[0]=Max;
score[1]=Min;
return sum/10;}
void main()
{ float shuzu[10];
for(int i=0;i<10;i++)
scanf("%f",&shuzu[i]);
printf("平均分是:%f\n最高分数是:%f\n最低分数是:%f\n",HH(shuzu),shuzu[0],shuzu[1]);
}
你在国外上学吧,这是国外的作业直接在线翻译过来的! 不过我可以帮你。
两个文件,其中一个是头文件,一个是.cpp文件,我把运行结果复制到程序下面了,你可以看一下!在VS2005下编译通过!!
//Matrix.cpp
//两个矩阵的加,减,乘 及 矩阵转置
#include
#include
#include "matrix.h"
#include
#include
using namespace std;
void main()
{
matrixobj a,b,c;
float x;
cout<<"Enter a 3 by 3 matrix (A): \n";
a.readMatrix();
cout<<"Enter a 3 by 3 matrix (B): \n";
b.readMatrix();
cout<<"Matrix (A) is : \n";
a.displayMatrix();
cout<<"Matrix (B) is : \n";
b.displayMatrix();
cout<<"Matrices (A) + (B) is : \n";
c = a + b;
c.displayMatrix();
getchar();
cout<<"Matrices (A) - (B) is : \n";
c = a - b;
c.displayMatrix();
getchar();
cout<<"Matrices (A) * (B) is : \n";
c = a * b;
c.displayMatrix();
getchar();
x = a.matrixDeterminant(); //矩阵行列式
getchar();
//c = a.matrixInverse();
c=a.matrixTranspose();
//cout<<"The Inversed Matrix of (A) is : \n";
cout<<"The Transposed Matrix of (A) is : \n";
c.displayMatrix();
getchar();
}
// 头文件 Matrix.h
#ifndef _MATRIX_H_
#define _MATRIX_H_
#include
#include
#include
#include
#include
using namespace std;
class matrixobj
{
float matrix[3][3];
float cofactor(int i,int j);
public:
void displayMatrix();
void readMatrix();
friend matrixobj operator+(matrixobj &x, matrixobj &y);
friend matrixobj operator-(matrixobj &x, matrixobj &y);
friend matrixobj operator*(matrixobj &x, matrixobj &y);
friend matrixobj operator/(matrixobj &x, float d);
float matrixDeterminant();
matrixobj matrixInverse();
matrixobj matrixTranspose();
};
void matrixobj::displayMatrix()
{
int i,j;
for (i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
cout<
}
void matrixobj::readMatrix()
{
int i,j;
float x=0;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
{
cin>>x;
matrix[i][j] = x;
}
}
float matrixobj::cofactor(int i,int j)
{
float cofactorValue;
int a1,b1,c1,d1,a2,b2,c2,d2;
a1 = (i + 1) % 3;
b1 = (j + 1) % 3;
c1 = (i + 2) % 3;
d1 = (j + 2) % 3;
a2 = (i + 2) % 3;
b2 = (j + 1) % 3;
c2 = (i + 1) % 3;
d2 = (j + 2) % 3;
cofactorValue = matrix[a1][b1] * matrix[c1][d1] - matrix[a2][b2] * matrix[c2][d2];
return cofactorValue;
}
float matrixobj::matrixDeterminant()
{
float det;
det = matrix[0][0] * cofactor(0,0);
det += matrix[0][1] * cofactor(0,1);
det += matrix[0][2] * cofactor(0,2);
return det;
}
matrixobj matrixobj::matrixInverse()
{
float det;
int i,j;
matrixobj z,y;
det = matrixDeterminant();
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = cofactor(i,j);
z = z.matrixTranspose();
if (det == (float) 0)
{
exit(0);
}
y = z / det;
return y;
}
matrixobj matrixobj::matrixTranspose()
{
int i,j;
matrixobj z;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[j][i] = matrix[i][j];
return z;
}
matrixobj operator+(matrixobj &x, matrixobj &y)
{
matrixobj z;
int i,j;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = x.matrix[i][j] + y.matrix[i][j];
return z;
}
matrixobj operator-(matrixobj &x, matrixobj &y)
{
matrixobj z;
int i,j;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = x.matrix[i][j] - y.matrix[i][j];
return z;
}
matrixobj operator*(matrixobj &x, matrixobj &y)
{
matrixobj z;
int i,j,k;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = 0;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
for(k = 0; k < 3; k++)
z.matrix[i][j] += x.matrix[i][k] * y.matrix[k][j];
return z;
}
matrixobj operator/(matrixobj &x, float d)
{
int i,j;
matrixobj z;
for (i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
z.matrix[i][j] = x.matrix[i][j] / d;
cout<<"\n"<
}
#endif
结果:
Enter a 3 by 3 matrix (A):
1 2 3 4 5 6 7 8 9
Enter a 3 by 3 matrix (B):
3 5 7 9 2 4 6 8 10
Matrix (A) is :
1 2 3
4 5 6
7 8 9
Matrix (B) is :
3 5 7
9 2 4
6 8 10
Matrices (A) + (B) is :
4 7 10
13 7 10
13 16 19
Matrices (A) - (B) is :
-2 -3 -4
-5 3 2
1 0 -1
c
Matrices (A) * (B) is :
39 33 45
93 78 108
147 123 171
c
The Transposed Matrix of (A) is :
1 4 7
2 5 8
3 6 9