更多"请编写一个函数int sum(int n),该函数完成1+2+3+…+"的相关试题:
[简答题]请编写一个函数int sum(int n),该函数完成1+2+3+…+n的运算,并返回运算结果,其中n>0。注意:请使用递归算法实现该函数。
注意:部分源程序已存在文件:test11.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数sum的花括号中填写若干语句。
文件test11_2.cpp的内容如下:
#include<iostream.h>
int sum(int n)
{
}
void main( )
{
int n;
cout<<"输入n:";
cin>>n;
int result;sum(n);
cout<<"结果为:"<<result<<endl;
}
[简答题]请编写一个函数inline long sum(int n),用递归函数完成运算:sum(n)=1*1+2*2+…n*n,递归表达式为 sum(n)=sum(n-1)+n2。
注意:部分源程序已存在文件test10_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数sum的花括号中填写若干语句。
文件test10_2.cpp的内容如下:
#include<iostream.h>
inline long sum(int n)
void main( )
int n;
cout<<"输入n:";
cin>>n;
cout<<"结果为:"<<sum(n)<<endl;
[简答题]请编写一个函数sum(int array[],int len),该函数返回数组array的所有整数元素的和,其中len为数组array的长度。
注意:部分源程序已存在文件test34_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数sum的花括号中填写若干语句。
程序输出结果如下:
sum of array 15
文件test34_2.cpp的内容如下:
#include <iostream.h>
int sum(int array[],int len)
{
}
void main( )
{
static int a[5]-{1,2,3,4,5};
int result=sum(a,5);
cout<<"sum of array "<<result<<end1;
}
[简答题]请编写两个函数int sum_of_powers(int k,int n),powers(int m,int n),求1~6的k次方的和,sum_of_powers中参数k和n分别表示k次方和所求数列中最大的一个自然数,最后返回所求值,powers中参数m和n分别表示m为底数n为指数,最后返回所求值。要求使用for循环和函数嵌套(int sum_of_powers中调用powers)实现算法。输出结果如下:
sum of 4 powers of intergers from 1 to 6=2275
注意:部分源程序已存在文件test25_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数sum_of_powers和powers的花括号中填写若干语句。
文件test25_2.cpp的内容如下:
#include<iostream.h>
const int k(4);
const int n(6);
int sum_of_powers(int k,int n),powers(int m,int n);
void main( )
cout<<"sum of "<<k<<" powers Of intergers from 1 to "<<n<<"=";
cout<<sum_of_powers(k,n)<<endl;
int sum_of_powers(int k,int n)
int powers(int m, int n)
[简答题]请编写一个函数printdate(int year,int month,int day),该函数实现将输入的3个数字转换成英语数字纪年输出的功能,如输入March9,1978,则输出1978 3 9。注意:使用switch结构实现该函数的基本功能并应该能够判断错误的输入。部分源程序已存在文件test40_2.cpp中。请勿修改主函数main和其他函数中的任何内容,仅在函数printdate的花括号中填写若干语句。
源程序文件rest40_2.cpp清单如下:
#include<iostream.h>
void printdate(int year, int month, int day)
{
}
void main( )
{
printdate(1978,3,9);
}
[多项选择]编程题
请编写一个函数void fun(int m, int k, int xx[]),该函数的功能是:将大于整数m且紧靠m的k个非素数存入所指的数组中。
例如,若输入15,5,则应输出16,18,20,21,22。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include
#include
void fun(int m, int k, int xx[])
{
}
main( )
{
int m,n,zz[1000];
clrscr( );
printf("/nPlease enter two integers: ");
scanf("%d%d",&m,&n);
fun(m, n, zz);
for(m=0;m
[简答题]请编写一个函数void fun(int m, int k, int xx[]),该函数的功能是将大于整数m且紧靠m的k个非素数存入所指的数组中。
例如,若输入15,5,则应输出16,18,20,21,22。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
#include <conio.h>
#include <stdio.h>
void fun(int m,int k,int xx[])
main( )
int m,n,zz[1000];
clrscr( );
printf("/nPlease enter two integers:");
scanf("%d%d",&m,&n);
fun(m,n,zz);
for(m=0;m<n;m++)
printf("%d",zz[m]);
printf("/n");
[简答题]请编写一个函数maxofarray(atype*p,int count),该函数从一个数组中找出其中的最大元素,并且数组中可以存放多种数据类型的元素。
注意:部分源程序己存在文件test42_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数maxofarray的花括号中填写若干语句。
文件test42_2.cpp清单如下:
#include<iostream.h>
#include<string.h>
#include<conio.h>
template<class atype>
void maxofarray(atype* p,int count)
{
}
void main ( )
{
int len=5;
char *p1;
cout<<"the char type array and it’s length is 5:/n";
cout<<"the array element is a b c d e/n";
p1=new char[len];
for (int i=0;i<len;i++)
p1[i]=’a’+i;
maxofarray(p1,len);
}
[多项选择]请编写一个函数jS Value(int m, int k, int xx[]),该函数的功能是:将小于整数m且紧靠m的k个素数存入数组xx并传回。调用函数writeDat( )读取10组数据,分别得出结果且把结果输出到文件out.dat中。
注意:部分源程序给出如下。
例如:若输入“17,5”,则应输出“13,11,7,5,3”。
请勿改动主函数main( )和输出数据函数readwriteDat( )的内容。
[试题源程序]
#include<conio.h>
#include<stdio.h>
void readwriteDat( );
void num(int m, int k, int xx[])
main( )
int m, n, xx[1000];
System("cls");
printf("/nPlease enter two integers:");
Scanf("%d%d", &m, &n);
num(m,n,xx);
for(m=0;m<n;m++)
printf("%d",xx[m]);
printf("/n");
readwriteDat( );
void readwriteDat( )
int m,n,xx[1000],i;
FILE *rf,*wf;
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
for(i=0;i<10;i++)
Scanf(rf,"%d%d",&m,&n);
num(m,n,xx);
for(m=0;m<n;m++) fprintf(wf,"%d",xx[m]);
fprintf(wf,"/n");
fclose(rf);
fclose(wf);
[简答题]请编写一个函数comm(int n,int k),该函数将用递归算法计算从n个人中选择k个人组成一个委员会的不同组合数,由n个人里选k个人的组合数=由(n-1)个人里选k个人的组合数+由(n-1)个人里选(k-1)个人的组合数。
注意:部分源程序已存在文件test41_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数comm的花括号中填写若干语句。
源程序文件test41-2.cpp清单如下:
#include<iostream.h>
int comm(int n, int k)
{
}
void main ( )
{
int n=7, k=3;
cout<<"n=7,k=3"<<endl;
cout<<comm(n,k)<<endl;
}
[简答题]请编写一个函数fun(int score [][3],int num),该函数返回有一门成绩以上课程成绩在85分以上,其余课程成绩不低于70分的人数。数组score按行存放num名考生各自的三门期末考试成绩。
注意:部分源程序已存在文件test31_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。
程序输出结果如下:
3
文件test31_2.cpp清单如下:
#include <iostream.h>
int fun(int score[] [3],int num)
{
}
void main ( )
{
int score[4] [3]={{70,89,92},{70,76,93},(80,86,98},{65,73,45});
cout<<fun(score,4)<<end1;
}
[简答题]请编写一个函数is Value(int m,int k,int xx[]),该函数的功能是:将小于整数m且紧靠m的k个素数存入数组xx传回。最后调用函数writeDat( )读取10组数据,分别得出结果且把结果输出到文件out.dat中。
注意:部分源程序给出如下。
例如:若输入17,5,则应输出:13,11,7,5,3。
请勿改动主函数main和输出数据函数readwriteDat( )的内容。
[试题源程序]
#include<conio.h>
#include<stdio.h>
void readwriteDat( );
void num(int m,int k,int xx[])
main( )
int m,n,xx[1000];
system("cls");
printf("/nPlease enter two integers;");
scanf("%d%d",&m,&n);
num(m,n,xx);
for(m=0;m<n;m++)
printf("%d",xx[m]);
printf("/n");
readwriteDat( );
void readwriteDat( )
int m,n,xx[1000],i;
FILE*rf,*wf;
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
for(i=0;i<10;i++)
fscanf(rf,"%d%cr,&m,&n);
num(m,n,xx);
for(m=0;m<n;m++)fprintf(wf,"%d",xx[m]);
fprintf(wf,"/n");
fclose(rf);
fclose(wf);
[简答题]请编写一个函数js Value(int m,int k,int xx[]),该函数的功能是:将小于整数m且紧靠m的k个素数存入数组xx传回。最后调用函数writeDat( )读取10组数据,分别得出结果且把结果输出到文件out.dat中。
注意:部分源程序给出如下。
例如:若输入17,5,则应输出:13,11,7,5,3。
请勿改动主函数main和输出数据函数readwriteDat( )的内容。
[试题源程序]
#include<conio.h>
#include<stdio.h>
void readwriteDat( );
void hum(int m,int k,int xx[])
main( )
int m,n,xx[1000];
system("cls");
printf("/nPlease enter two integers:");
scanf("%d%d",&m,&n);
num(m,n,xx);
for(m=0;m<n;m++)
printf("%d",xx[m]);
printf("/n");
readwriteDat( );
void readwriteDat( )
int m,n,=[1000],i;
FILE*rf, *wf:
rf=fopen("in.dat","r"):
wf=fopen("out.dat","w");
for(i=0;i<10;i++)
fscanf(rf,"%d%d",&m,&n);
num(m,n,xx);
for(m=0;m<n;m++) fprintf(wf,"%d",xx[m]):
fprintf(wf,"/n");
fclose(rf);
fclose(wf);
[简答题]请编写一个函数int stringLen(char*ps),该函数能计算出字符串ps的长度,函数返回值就是字符串的长度(不包括字符串结束标识号’/0’)。本题要求:用指针方式及循环来实现该函数。
注意;部分源程序已存在考生文件夹下的文件PROC6,cpp中。
请勿修改主函数和其他函数中的任何内容,仅在函数stringLen( )的花括号中填写若干语句。
文件PROC6.cpp的内容如下:
//PROC6.cpp
#include<iostream>
using namespace std;
int stringLen(char *);
int main( )
char str[100],*p;
cout<<"Input your string please!/n";
cin>>str;
p=str;
cout<<"The lenth of your string is "<<stringLen(p)<<end1;
return 0;
int stringLen(char *ps)
// * * * * *
[简答题]请编写一个函数int CalcDigital(char *str),该函数可返回字符串str中数字字符(即0~9这10个数字)的个数,如字符串“olympic2008”中数字字符的个数为4。请用if条件判断语句与for循环语句来实现该函数。
注意:部分源程序已存在文件test9_2.cpp中。
请勿修改主函数main和其他函数中的任何内容,仅在函数find的花括号中填写若干语句。
文件test9_2.cpp的内容如下:
#include<iostream.h>
#include<string.h>
int CalcDigital(char*str);
void main( )
char *str;
str=new char[255];
cout<<"输入字符串:";
cin>>str;
int num=CalcDigital(str);
cout<<str<<":"<<num<<endl;
int CalcDigital(char *str)