请编写一个函数void fun(int aa[],int n,int x),其中n表示数组aa中元素的个数,函数的功能是:计算前x项的和并放在aa[x]中,aa数组中的元素值和x的值由主函数通过键盘读入。 注意:用循环和数组实现。 部分源程序已

admin2009-01-15  23

问题 请编写一个函数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;
      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,sum=0; for (i=0; i<x; i++) sum=sum+aa [i]; aa [x] =sum; }

解析 根据题意分析可得:本题要求考生统计一个数组中前x项的和。本题主要考查考生对数组作为形参的应用能力。计算数组aa[]中前x项的和可以用循环语句 for来实现。
转载请注明原文地址:https://kaotiyun.com/show/XKID777K
0

最新回复(0)