c++实验答案

内容发布更新时间 : 2024/5/3 7:23:07星期一 下面是文章的全部内容请认真阅读。

cout<<\}

解法二

#include #define N 30 void main(void)

{ char str1[N],str2[N],str[2*N]; int i,j;

cout<<\ first String\ cin>>str1;

cout<<\ second String\ cin>>str2; i=0;

while(str1[i]!=0) { str[i]=str1[i]; i++; } j=0;

while(str2[j]!=0) { str[i]=str2[j]; i++; j++; }

str[i]=0;

cout<<\}

运行结果:

Input first String abcde

Input second String fghij

str=abcdefghij (3)

#include #include #define N 11 void main(void)

{ int c[N][N],m,n,k,m1,n1,nm1; for(n=1;n

for(n=3; n

for(m=2; m<=n-1;m++)

c[n][m]=c[n-1][m-1]+c[n-1][m]; //数组元素赋值 for(n=1; n

{ if(n%2==0)cout<<” ” ; //调整数字对齐 for (m=0;m<(N-n)/2;m++) //输出空格 cout<

cout<

#include #include #define M 7 #define N 5 void main(void)

{ float s[M][N],sum,ave,temp,max,min; int i,j,k;

cout<<\ for (i=0;i>s[i][j]; }

for (i=0;i

for (j=1;j

s[i][N-1]=sum; }

for (j=1;j

for (i=0;is[i][j])

min=s[i][j]; }

s[M-1][j]=min; s[M-2][j]=max; }

for (i=0;i

//输入5个学生的学号与3门课成绩 //处理数据 //计算每个学生的总成绩 //计算每个学生的总分 //处理数据 //处理计算每门课程 //计算每门课程的最低分 //计算每门课程的最高分 //按总成绩排序

{ k=i;

for(j=i+1;j

if (s[k][N-1]

for (j=0;j

{ temp=s[i][j];s[i][j]=s[k][j];s[k][j]=temp;} }

cout<

{ for (j=0;j

cout<<\}

运行结果: Input data: 1001 90 80 85 1002 70 75 80 1003 65 70 75 1004 85 50 60 1005 80 90 70

Num. Math. Chin. Engl. Sum. ------------------------------

1001 90 80 85 255 1005 80 90 70 240 1002 70 75 80 225 1003 65 70 75 210 1004 85 50 60 195 最高分 90 90 85 255 最低分 65 50 60 195 ------------------------------

实验六

1.实验目的 通过本次实验

(1)初步掌握函数的定义方法,及函数的三种调用方法; (2)理解参数传送过程中,值传送与传地址的过程与区别; (3)初步学会用递归编写程序方法;

(4)学会用数组作为函数参数的编程方法。 2.实验要求

(1)编写实验程序

(2)在VC++运行环境中,输入源程序; (3)编译运行源程序;

(4)输入测试数据进行程序测试; (5)写出运行结果。 3.实验内容

(1)分别用冒泡法(升序)、选择法(降序)、擂台法(升序)编写三个对一维数组进行排序的函数,函数名为sort1()、sort2()、sort3()。再定义一个输出数组元素值的函数print()。在主函数中定义一维整型数组a[N](N=10),用键盘输入10个整数给a[N]数组。依次调用sort1()、print()、sort2()、print()、sort3()、print(),进行升序、降序、升序的操作,并输出每次排序后的结果。

输入十个实验数据:10,25,90,80,70,35,65,40,55,5

(2)编写一个函数px(float x,int n)用递归的方法求下列级数前n项的和s。

在主函数中定义变量x与n,用键盘输入x与n的值,调用px()函数计算并返回级数前n项和s。最后输出s的值。 输入实验数据:x=1.2 n=10

(3)编写一个字符串连接函数str_cat(char s[],char s1[],char

s2[]),完成s=s1+s2的字符串连接工作。具体要求为,先将字符串s1复制到s中,然后再将字符串s2连接到s后面。在主函数中定义三个字符串数组str[80]、str1[40]、str2[40],将两个字符串输入到str1与str2中,调用字符串连接函数str_cat(),将str1与str2连接到str中,最后输出连接后的字符串str。要求用两种方法编写str_cat()函数。方法一,用字符串复制与连接函数。方法二,用while语句编程实现。

输入实验数据:str1=”I am student” str2=”And You are student too” (4)编写一个计算sin(x)的函数,在主函数中输入x,调用sin(x)函数计算并输出y值。 y= 要求:在sin(x)函数内,将级数中各项值累加到和变量s中去,直到最后一项绝对值小于0.00001为止。

输入实验数据:x=3.14159

(5)编写计算组合数的函数cmn(int n,int m),实现如下杨辉三角形的输出。 1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

1 6 15 20 15 6 1

4.解答参考 (1)

#include #include #define N 10 void print(int a[]) { int i;

for(i=0;i

void sort1( int a[] ) { int i,j,temp;

for(i=0;i

for(j=0;j<=N-1-i;j++) if (a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } }

void sort2( int a[] ) { int i,j,temp;

for(i=0;i

{ temp=a[i]; a[i]=a[j]; a[j]=temp; } }

void sort3( int a[] ) {

int i,j,k,temp;

for(i=0;i

for(j=i+1; j

if (a[k]>a[j]) k=j; if (k>i)

{ temp=a[i]; a[i]=a[k]; a[k]=temp;} } }

void main(void) { int i; int b[10];

cout<<\请输入10个数:\for(i=0;i<10;i++) cin>>b[i]; sort1(b);

cout<<\输出排好序的10个数:\

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi