c语言 任意输入一个3×3的矩阵,用函数实现求上三角矩阵并输出。

必须用指针和函数解答:必须用指针和函数解答:必须用指针和函数解答: 1: 任意输入一个3×3的矩阵,用函数实现求上三角矩阵并输出。2:自己写一个mystrcmp函数,比较两个字符串是否相同。要求函数原型为:int mystrcmp(char *p1,char *p2)设p1指向字符串s1, p2指向字符串s2。逐个字符开始判断。如果所有字符都相等,那么s1=s2.返回值是0。如果当前指向的字符已经不相等了,那么认为s1不等于s2,返回当前字符的ASCII码的值的差值。在main函数中调用这个函数,进行判断,看main函数中输入的两个字符串是相同还是不相同3:写一个函数,形参是成绩数组,要求返回是分析结果的数组(包括平均值,最高分和最低分)。在主函数中输入10个同学的成绩,调用分析函数,然后主函数中输出返回的平均分,最高分和最低分。 先谢谢大家了,帮帮忙啊,作业不会啊。

第一题#include <stdio.h>
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 <stdio.h>
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 <stdio.h>
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]);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-23
你在国外上学吧,这是国外的作业直接在线翻译过来的! 不过我可以帮你。

两个文件,其中一个是头文件,一个是.cpp文件,我把运行结果复制到程序下面了,你可以看一下!在VS2005下编译通过!!

//Matrix.cpp
//两个矩阵的加,减,乘 及 矩阵转置

#include <stdio.h>
#include <conio.h>
#include "matrix.h"
#include <float.h>
#include <iostream>
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 <math.h>
#include <stdlib.h>
#include <float.h>
#include <iostream>
#include <iomanip>

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<<setw(4)<<matrix[i][j];
cout<<"\n"<<endl;
}
}

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"<<endl;
return z;
}

#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
相似回答