首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
请打开考生文件夹下的解决方案文件proj3,其中定义的Matrix是一个用于表示矩阵的类。成员函数max_value的功能是求出所有矩阵元素中的最大值。例如,若有3×3矩阵 则调用max_value函数,返回值为3。请编写成员函数max_value。 要
请打开考生文件夹下的解决方案文件proj3,其中定义的Matrix是一个用于表示矩阵的类。成员函数max_value的功能是求出所有矩阵元素中的最大值。例如,若有3×3矩阵 则调用max_value函数,返回值为3。请编写成员函数max_value。 要
admin
2018-07-06
27
问题
请打开考生文件夹下的解决方案文件proj3,其中定义的Matrix是一个用于表示矩阵的类。成员函数max_value的功能是求出所有矩阵元素中的最大值。例如,若有3×3矩阵
则调用max_value函数,返回值为3。请编写成员函数max_value。
要求:
补充编制的内容写在“//*******333*******”与“//*******666*******”之间,不得修改程序的其他部分。
注意:程序最后将结果输出到文件out.dat中。输出函数writeToFile已经编译为Obj文件,并且在本程序中调用。
//Matfix.h
#include<iostream>
#include<iomanip>
using namespace std;
const int M=18;
conSt int N=18;
Class Matrix{
int array[M][N];
public:
Matrix( ){}
int getElement(int i,int j)const{return array
[j];}
VOid setElement(int i,int j,int value){array
[j]=value;}
int max value( )const;
void show(const char*s)const
{
cout<<endl<<s;
for(int i=0;i<M;i++)(
cout<<endl;
for(int j=0;j<N;j++)
cout<<setw(4)<<array
[j];
}
}
};
void readFromFile(const char*,Matrix&);
void writeToFile(char*,const Matrix&);
//main.cpp
#include"Matrix.h"
#include<fstream>
void readFromFile(const char*f,Matrix&m){
ifstream infile(f);
if(infile.fail( )){cerr<<"打开输入文件失败!";return;}
int k;
for(int i=0;i<M;i++)
for(int j=0;j<N;j++){
infile>>k;
m.setElement(i,j,k);
}
}
int Matrix::max_value( )const
{
//*******333*******
//*******666*******
}
int main( )
{
Matrix m;
readFromFile(" ",m);
m.show("Matrix:");
cout<<endl<<"最大元素:"<<m.maxvalue( )<<endl;
writeToFile(" ",m);
return0;
}
选项
答案
int temp=0;//定义整数变量temp,并赋值为零 for(int i=0;i<M;i++)//遍历矩阵的行 for(int j=0;j<N;j++)//遍历短阵的列 if(temp<array[i][j])//如果temp小于array[i][j] temp=array[i][j];//把array[i][j]赋值给temp return temp;//返回temp
解析
主要考查考生对二维数组的掌握,题目要求成员函数max_value的功能是求出所有矩阵元素中的最大值。因此只要逐个元素比较即可,下标i和j作为矩阵行和列的标记,使用双层for循环来遍历数组中的所有元素。
转载请注明原文地址:https://kaotiyun.com/show/fAAp777K
本试题收录于:
二级C题库NCRE全国计算机二级分类
0
二级C
NCRE全国计算机二级
相关试题推荐
对建立良好的程序设计风格,下列描述中正确的是()。
派生类中的成员不能直接访问基类中的()成员。
根据输出结果填空完成下面程序。#include<iostream.h>classTest{private:staticintval;inta;public:staticintfunc()
有如下程序:#include<iostream>usingnamespacestd;ClassTestClass{inta;public:TestClass(intx){a=x;}void
数据流图有两种典型的结构形式,它们分别是变换型和【】。
假定要对类AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为()。
如果不使用多态机制,那么通过基类的指针虽然可以指向派生类对象,但是只能访问从基类继承的成员。下列程序没有使用多态机制,其输出结果是______。#include<iostream>usingnamespacestd;c
类time可以输出12小时或24小时制的时间,现需要设计一个缺省构造函数,缺省值为0时0分0秒。该缺省构造函数为______。#include<iostream.h>classTime{inth,m,s;
已知二叉树后序遍历序列是dabec,中序遍历序列是debac,它的前序遍历序列是
函数inttest(inta,intb=1,intc=0),下列调用不合法的个数是test(0);test(0,0);test();test(0,0,0);
随机试题
肉、禽及水产类罐头需将罐头加热,使内容物熔化后开罐。
A.CVP下降,BP上升,CO下降 B.CVP上升,BP下降,CO下降 C.CVP不变,BP下降,CO上升 D.CVP下降,BP上升,CO上升 E.CVP正常,BP上升,CO正常失血性休克早期时可出现
下列哪种通道是持续开放的
患者,女,25岁。发现颈部肿块2天。体格检查:气管右侧可扪及一结节,质硬,表面不光滑,可随吞咽上下移动,同侧胸锁乳突肌前缘可扪及2个肿大淋巴结。应首先考虑的诊断为
业主使用物业时,应当遵守的规则主要包括()。
股份有限公司的董事应当持有公司股份。()
设4阶行列式D=4,且D的每列元素之和均为2,则A21+A22+A23+A24=()。
下列函数原型声明中,错误的是()。
【B1】【B4】
Coffee,ahotbeveragefavoredbypeopleindifferentregions,issaidtohavebeen【1】______【1】______inEthiopia.Itwasfound
最新回复
(
0
)