主页 > 金融科技 >C语言编程求出4×4矩阵中每行的最大值,输出最大值及位置,并输出他们的和。

C语言编程求出4×4矩阵中每行的最大值,输出最大值及位置,并输出他们的和。

作者: 时间:2020-07-04 627° 金融科技
C语言编程求出4×4矩阵中每行的最大值,输出最大值及位置,并输出他们的和。

#include
intmain()
{
intnum[4][4],*p=&num[0][0],*pSave,max[4]={0,0,0,0},sum=0,i=0;
printf("矩阵16个数字: ");
while(p<=&num[3][3])//输入矩阵数字
{
scanf("%d",p++);
}
p=&num[0][0];
while(1)//总
{
while(p<=&num[i][3])//子循环:统计一
{
if(*p>max[i])
{
max[i]=*p;
pSave=p;
}
p++;
}
sum=sum+max[i];
i++;
printf("%d行最大值是第%d个元素值为%d ",i,pSave-&num[i-1][0]+1,max[i-1]);
if(p>&num[3][3])
break;
}
printf("总和为%d ",sum);
return0;
}

代码如下:#include
#include

#defineN4

intmain(){


//定义一个4*4的数组
intmatrix[N][N]={
45,85,63,12,
52,89,41,32,
11,14,28,24,
36,63,65,49
};

intmax=0; //最大值
intpos=0; //最大值位
intsum=0; //最大值和

for(inti=0;i
max=matrix[i][0];
pos=0;

for(intj=0;jif(matrix[i][j]>max){
max=matrix[i][j];
pos=j;
}
}

sum+=max;

printf("第%d行最大值是%d,位置:%d ",i,max,pos);
}

printf("最大值之和:%d ",sum);

system("pause");
return0;
}

运行结果:

int c[4][4]={5,6,7,8,
8,6,4,3,
6,8,9,2,
4,3,4,7};
int max=0;
int all=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)

{
if (c[i][j]>max) max=c[i][j];

}
printf("hang %d max = %d",i,max);
all=all+max;
max=0;
}

printf("All= %d",all);
在C语言中,编写程序,求下列矩阵各行元素之和及各列元素之和
考虑到这道题的性质,用数组运算更快(原理就不说要想问再留言)
int arry1[5]={1,3,5,7,9};
int arry2[5]={2,4,6,8,10};
int arry3[5]={3,5,8,7,6};
int sum_row[3]={0*3};/*一素之和*/
int sum_column[5]={0*8};/*一列元素之和*/
for(int i=0;i<5;i++)
{

sum_row[0]+=arry1[i];
sum_row[1]+=arry2[i];
sum_row[2]+=arry3[i];
sum_column[i]=arry1[i]+arry2[i]+arry2[i];
printf("第%d列和为%d",i,sum_column[i]);

}
for(int i=0;i<3;i++)
{
printf("第%d行和为%d",i,sum_row[i]);
}#include

void main(void)
{
int a[3][5] = {{1,3,5,7,9}, {2, 4, 6, 8, 10}, {3, 5, 8, 7, 6}};

int i, j, r_sum[3], c_sum[5];

for (i=0; i<3; i++)// 各

{
r_sum[i] = 0;

for (j=0; j<5; j++)

{
r_sum[i] += a[i][j];
}

printf("第%d行的和为%d\n", i, r_sum[i]);
}

for (j=0; j<5; j++)// 各列的和
{
c_sum[j] = 0;
for (i=0; i<3; i++)
{
c_sum[j] += a[i][j];
}
printf("第%d列的和为:%d\n", j, c_sum[j]);
}
}
从键盘输入一个4×4的整数矩阵,编程求: 1) 矩阵元素的最小值, 分别输出矩阵中该值
template
struct Matrix3d
{
T matrix[4][4];

typedef T value_type;
typedef T*reference;
typedef T*iterator;
typedef const T* const_iterator;

Matrix3d(void) { memset(matrix, 0x00, sizeof(matrix)); };
Matrix3d(const Matrix3d& mat) { memcpy(matrix, mat.matrix, sizeof(matrix)); };

Matrix3d(const Vector3d& vt)
{
memset(matrix, 0x00, sizeof(matrix));
matrix[0][0] = vt.x;
matrix[1][1] = vt.y;
matrix[2][2] = vt.z;
matrix[3][3] = 1;
};

identity(void)
{
memset(matrix, 0x00, sizeof(matrix));
matrix[0][0] = 1;
matrix[1][1] = 1;
matrix[2][2] = 1;
matrix[3][3] = 1;
}
//operator
const Matrix3d operator + () const{ return *this;}
Matrix3d operator -(void) const
{
Matrix3d mat;
for(sint i = 0; i < 4; i++){
for(sint j = 0; j < 4; j++){
mat.matrix[i][j] = -matrix[i][j];
}
}

return mat;
};

const Matrix3d& operator =(const Matrix3d& mat)
{
memcpy(matrix, mat.matrix, sizeof(matrix));
return *this;
};

Matrix3d operator +(const Matrix3d& mat) const
{
Matrix3d matTemp;
for(sint i = 0; i < 4; i++){
for(sint j = 0; j < 4; j++){
matTemp.matrix[i][j] = matrix[i][j] + mat.matrix[i][j];
}
}
return matTemp;
};

Matrix3d operator -(const Matrix3d& mat) const
{
Matrix3d matTemp;
for(sint i = 0; i < 4; i++){
for(sint j = 0; j < 4; j++){
matTemp.matrix[i][j] = matrix[i][j] - mat.matrix[i][j];
}
}
return matTemp;
};

Matrix3d operator *(const Matrix3d& mat) const
{
Matrix3d matTemp;
for(sint i = 0; i < 4; i++){
for(sint j = 0; j < 4; j++){
T tCount = 0;
for(sint k = 0; k < 4; k++){
tCount += matrix[i][k] * mat.matrix[k][j];
}

matTemp.matrix[i][j] = tCount;
}
}
return matTemp;
};

const Matrix3d& operator +=(const Matrix3d& mat)
{
for(sint i = 0; i < 4; i++){
for(sin j = 0; j < 4; j++){
matrix[i][j] += mat.matrix[i][j];
}
}
return *this;
};

Matrix3d& operator -=(const Matrix3d& mat)
{
for(sint i = 0; i < 4; i++){
for(sin j = 0; j < 4; j++){
matrix[i][j] -= mat.matrix[i][j];
}
}
return *this;
};

Matrix3d& operator *=(const Matrix3d& mat)
{

Matrix3d matTemp;
for(sint i = 0; i < 4; ++i){
for(sint j = 0; j < 4; ++j){
T tCount = 0;
for(sint k = 0; k < 4; ++k){
tCount += matrix[i][k] * mat.matrix[k][j];
}
matTemp.matrix[i][j] = tCount;
}
}
memcpy(matrix, matTemp.matrix, sizeof(matrix));
return *this;
};

Matrix3d operator *(const T& v) const
{
Matrix3d matTemp;
for(sint i = 0; i < 4; i++){
for(sint j = 0; j < 4; j++){
matTemp.matrix[i][j] = matrix[i][j] * v;
}
}
return matTemp;
};

Matrix3d operator /(const T& v) const
{
Matrix3d matTemp;
for(sint i = 0; i < 4; i++){
for(sint j = 0; j < 4; j++){
matTemp.matrix[i][j] = matrix[i][j] / v;
}
}

return matTemp;
};

const Matrix3d& operator *=(const T& v)
{
for(sint i = 0; i < 4; i++){
for(sint j = 0; j < 4; j++){
matrix[i][j] *= v;
}
}
return *this;
};

const Matrix3d& operator /=(const T& v)
{
for(sint i = 0; i < 4; i++){
for(sint j = 0; j < 4; j++){
matrix[i][j] /=v;
}
}
return *this;
};

//function
void debugOut(void)
{
#ifdef _DEBUG
#include
std::cout<for(sint i = 0; i < 4; i++)
{
for(sint j = 0; j < 4; j ++)
{
std::cout<}
std::cout<}

std::cout<#endif //_DEBUG
};

};

刚才漏了一个vector3d
template
struct Vector3d
{
union{
struct{
T x,y,z;
};
T val[3];
};

Vector3d() : x(0), y(0), z(0) { };
Vector3d(const T& nx, const T& ny, const T& nz) : x(nx), y(ny), z(nz) { };
Vector3d(const Vector3d& vt) : x(vt.x), y(vt.y), z(vt.z) { };

//operator

const Vector3d& operator +() const {return *this;};
Vector3d operator -(void) const { return Vector3d(-x, -y, -z); };
const Vector3d& operator =(const Vector3d& vt) { x = vt.x; y = vt.y; z = vt.z; return *this; };

Vector3d operator +(const Vector3d& vt) const { return Vector3d(x + vt.x, y + vt.y, z + vt.z); };
Vector3d operator -(const Vector3d& vt) const { return Vector3d(x - vt.x, y - vt.y, z - vt.z); };
Vector3d operator *(const Vector3d& vt) const { return Vector3d(x * vt.x, y * vt.y, z * vt.z); };
Vector3d operator /(const Vector3d& vt) const { return Vector3d(x / vt.x, y / vt.y, z / vt.z); };

const Vector3d& operator +=(const Vector3d& vt) { x += vt.x; y += vt.y; z += vt.z; return *this; };
const Vector3d& operator -=(const Vector3d& vt) { x -= vt.x; y -= vt.y; z -= vt.z; return *this; };
const Vector3d& operator *=(const Vector3d& vt) { x *= vt.x; y *= vt.y; z *= vt.z; return *this; };
const Vector3d& operator /=(const Vector3d& vt) { x /= vt.x; y /= vt.y; z /= vt.z; return *this; };

Vector3d operator *(const T& v) const {return Vector3d(x * v, y * v, z * v); };
Vector3d operator /(const T& v) const {return Vector3d(x / v, y / v, z / v); };
const Vector3d& operator *=(const T& v) {x *= v; y *= v; z *= v; return *this; };
const Vector3d& operator /=(const T& v) {x /= v; y /= v; z /= v; return *this; };

bool operator ==(const Vector3d& vt) const { return (x == vt.x && y == vt.y && z == vt.z); };
bool operator !=(const Vector3d& vt) const { return (x != vt.x || y != vt.y || z != vt.z); };

f64 getDistanceToCenter(void) const { return sqrt(x * x + y * y + z * z); };
f64 getDistanceToCenterSQ(void) const { return x * x + y * y + z *z; };

f64 getDistanceFromPoint(const Vector3d& vt) const
{
f64 vx = x - vt.x;
f64 vy = y - vt.y;
f64 vz = z - vt.z;
return sqrt(vx * vx + vy * vy + vz * vz);
};

f32 getDistanceFromSQ(const Vector3d& vt) const
{
f32 vx = x - vt.x;
f32 vy = y - vt.y;
f32 vz = z - vt.z;
return (vx*vx + vy*vy + vz*vz);
};

void setValue(T nx, T ny, T nz) {x = nx; y = ny; z = nz; };
void setValue(const Vector3d& vt) { x = vt.x; y = vt.y; z = vt.z; };

//点乘
T dotProduct(const Vector3d& vt) const { return x * vt.x + y * vt.y + z * vt.z; };

//差乘
Vector3d crossProduct(const Vector3d& vt) const
{
return Vector3d(y * vt.z - z * vt.y, z * vt.x - x * vt.z, x * vt.y - y * vt.x);
};

void invert()
{
x *= -1.0f;
y *= -1.0f;
z *= -1.0f;
};
};
1) 矩阵元素的最小值, 分别输出矩阵中该值所在的行之和、列之和。
2) 矩阵元素的平均值

这两个函数自己补进去就好了
c语言编程 输入一个矩阵,然后输出矩阵,有几种方法呢?
输入可以不知道长宽,但是输出的长宽一定要知道的。

这样输入的时候,可以考虑把数据全都存到一维数组或者二维数组,应该就两种,因为本质上,任何维的数组都可以转化为一维数组。追问

我当时是写错了,一直输不错矩形,后来竟然用上了if((j+1)%3==0),想让每3个数强制换行,结果后来发现,不用这样就直接正常输出了,呵呵,用二维数组循环就很容易实现了,我走了弯路追答

嗯,是的啊,二维数组很简单~

上一篇:
下一篇:

中国食品科技网|广州友邦资讯科技招聘|深圳科技资讯|网站地图