实验6数组的应用 【实验目的】 1掌握数组定义和初始化的方法。 2熟悉使用字符数组处理字符串处理的方法。 【实验内容】 1.输入10个学生的成绩,求平均成绩,并将低于平均成绩的分数打印出来 # include≤ iostream. h> #include <iomanip. h void readdata(float scoreloD cout<<"Please input 10 students score: <<endl; for(int F0; i<10; i++) cin>>score; 1 float aver(float scoreloD f float sum=0; int i; for(i=0;<10;i+) sum+=score i; return(sum/10); void print(float score 10, float ave) Rint 1; cout<< the scores which are below the average for(i=0;<10;i+) if(s core山<ave cout<<score<<; out<<endl: return; void main( i void readdata(float scorel1oD float aver(float score 10 void print(float score[ 10, float ave); float ave, score 10 readdata(score) cout<<average="<<ave<<endl; print(score, ave) 2.编写一程序,从键盘任意输入两个字符串sl和s2,然后比较字符串的大 小,若s1>s2,输出1:若sl=s2,输出0:若sl<s2,输出-1 lude <string. h #include <stdio h> #includesiostream h3 void maino i int k
实验 6 数组的应用 【实验目的】 1 掌握数组定义和初始化的方法。 2 熟悉使用字符数组处理字符串处理的方法。 【实验内容】 ⒈输入 10 个学生的成绩,求平均成绩,并将低于平均成绩的分数打印出来。 #include <iostream.h> #include <iomanip.h> void readdata(float score[10]) {cout<<"Please input 10 student's score:"<<endl; for(int i=0;i<10;i++) cin>>score[i];} float aver(float score[10]) { float sum=0; int i; for(i=0;i<10;i++) sum+=score[i]; return(sum/10); } void print(float score[10],float ave) {int i; cout<<"the scores which are below the average:"; for(i=0;i<10;i++) if(score[i]<ave) cout<<score[i]<<" "; cout<<endl; return; } void main( ) { void readdata(float score[10]); float aver(float score[10]); void print(float score[10],float ave); float ave,score[10]; readdata(score); ave=aver(score); cout<<"average="<<ave<<endl; print(score,ave); } ⒉编写一程序,从键盘任意输入两个字符串 s1 和 s2,然后比较字符串的大 小,若 s1>s2,输出 1;若 s1=s2,输出 0;若 s1<s2,输出-1; #include <string.h> #include <stdio.h> #include<iostream.h> void main() { int k;
har sl301, s2 30 1 cout<< input a string: <<end; gets(sD); gets(s2); k=strcmp(sl, s2); if(k=0)cout<"0Ⅶ"; if(>0) if(k<0) cout<<"-In3 3应用一维数组,对10个从键盘输入的数进行冒泡排序,使其按照从大到 小的顺序输出 #include <iostream.h> Rint a[101 int 1, ], t: cout<< Input ten data: <<endl; for(i=0;i<10;i++) i cout<<"al<<i+1<<I=; cin>>a i; j for(i=0;i<=8;i++) if(a i<aljD) it=ail; t<<"sorted d for(i=0;i←=9;i++) 4应用二维数组打印如图所示杨辉三角形。 #include <iostream.h void mainO int i,j, n=5; for(i=0;<n;i++) yG=l: i0=1;}
char s1[30],s2[30 ]; cout<<"input a string: "<<endl; gets(s1); gets(s2); k=strcmp(s1,s2); if(k==0) cout<<"0 \n"; if(k>0) cout<<"1\n"; if(k<0) cout<<"-1\n"; } ⒊应用一维数组,对 10 个从键盘输入的数进行冒泡排序,使其按照从大到 小的顺序输出。 #include <iostream.h> void main() {int a[10]; int i,j,t; cout<<"Input ten data:"<<endl; for(i=0;i<10;i++) { cout<<"a["<<i+1<<"]="; cin>>a[i]; } for(i=0;i<=8;i++) { for(j=i+1;j<=9;j++){ if(a[i]<a[j]) { t=a[i]; a[i]=a[j]; a[j]=t;} } } cout<<"sorted data:"<<endl; for(i=0;i<=9;i++) cout<<"a["<<i+1<<"]="<<a[i]<<" "<<endl; } ⒋应用二维数组打印如图所示杨辉三角形。 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 #include <iostream.h> void main() { int y[5][5]; int i,j,n=5; for(i=0;i<n;i++) { y[i][i]=1; y[i][0]=1;}
f forg=l;j<i;j++) y=yi-1lj-1+yi-1[: ou<"杨辉三角形:"<<endl for(FO; i<n; i++) forG=0;j=i;j+ cout<<yliik<; 5编程序将输入的字符串删去空格输出。 #includesiostream. h> #include<stdio. h> void maino i char str50]: int F0.j; gets(str); while(str=NULL if(str[=”") while(strIl:=NULL) i strHlFstr[+1; cout<<str<<endl; j
for(i=2;i<n;i++) { for(j=1;j<i;j++) y[i][j]=y[i-1][j-1]+y[i-1][j];} cout<<"杨辉三角形:"<<endl; for(i=0;i<n;i++) { cout<<" "; for(j=0;j<=i;j++) cout<<y[i][j]<<" "; cout<<endl;} } ⒌编程序将输入的字符串删去空格输出。 #include<iostream.h> #include<stdio.h> void main() { char str[50]; int i=0,j; gets(str); while(str[i]!=NULL) { if(str[i]==' ') { j=i; while(str[j]!=NULL) { str[j]=str[j+1]; j++;} } i++;} cout<<str<<endl; }