问答题

请编写一个函数void fun(int aa[],int n,int x),其中n表示数组aa中元素的个数,函数的功能是:计算前x项的和并放在aa[x]中,aa数组中的元素值和x的值由主函数通过键盘读入。 注意:用循环和数组实现。 部分源程序已存在文件PROC3.cpp中。请勿修改主函数和其他函数中的任何内容, 仅在函数resort()的花括号中填写若干语句。 文件PROC3.cpp中的程序清单如下: //PROC3.CPP #include <iostream> using namespace std; #define MAX 100 int main () { void fun(int aa[],int n, int x); int bb[MAX],i,x,n; cout<<"Please enter the counter of the number:\n"; cin>>n; cout<<"Please enter the number:\n"; for(i=0;i<n;i++) cin>>bb[i]; cout<<"Input the x:\n"; cin>>x; fun(bb, n,x); cout<<"The data after total: "<<bb[x]<<end1; return 0; } void fun(int aa[],int n, int x) { //********* }

【参考答案】

函数fun()的实现如下: void fun(int aa[],int n, int x) { int i=0,su......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
请使用“答题”菜单或从VC6中打开考生文件夹proj1下的工程proj1。此工程包含了类Pets(“宠物”)和主函数main的定义。程序中位于每个 ERRO************found************下的语句行有错,请加以改正。改正后程序的输出结果是:Name: sonny Type: dogName: John Type: dogName: Danny Type: catName: John Type: dog注意:只能修改每个 ERROR************found************下的那一行,不要改动程序中的其他内容。 源程序#include <iostream>using namespace std;enum Pets_type{dog,cat,bird,fish};class Pets{private:char*name:Pets_type type;public:Pets(const char*name= sonny ,Pets_type type=dog);Pets& operator=(const Pets &s);~Pets();void show() const;};Pets::Pets(const char*name,Pets_type type){ 构造函数this->name=new char[strlen(name)+1];strcpy(this->name,name); ERROR************found************type=type;}Pets::~Pets(){ 析构函数,释放name所指向的字符串 ERROR************found************name=’ 0’:}Pets&Pets::operator=(const Pets &s){if(&s==this) return*this; 确保不要向自身赋值delete[]name;name=new char[strlen(s.name)+1]; ERROR************found************strcpy(s.name,this->name);type=S.type;return*this:}void Pets::show() const{couL<< Name: <<name<< Type: ;switch(type){case dog: cout<< dog ;break;case cat: cout<< cat ;break;case bird: cout<< bird ;break;case fish: cout<< fish ;break;}cout<<endl;}int main(){Pets mypet1,mypet2( John ,dog);Pets youpet( Danny ,cat);mypet1.show();mypet2.show();youpet.show();youpet=mypet2;youpet.show();return 0:}