#include<iostream>#include<array>constint Size =10;intmain(){usingnamespace std;
array<double, Size> donations;
cout <<"输入数字(非数字时停止): ";double donation;int i, n;for(i =0;(i < Size)&&(cin >> donation);++i){
donations[i]= donation;
n = i +1;}double sum;for(i =0, sum =0.0; i < n;++i)
sum += donations[i];double avg;
avg = sum /(float)n;
cout.precision(2);
cout <<"平均值为: "<< avg << endl;
cout <<"比平均值大的数有:";for(i =0; i < n;++i){if(donations[i]> avg)
cout << donations[i]<<" ";}
cout << endl;return0;}
3.
#include<iostream>#include<array>constint Size =10;intmain(){usingnamespace std;char choice;
cout <<"Please enter one of the following choices:\n";
cout <<"c) carnivore p) pianist\n";
cout <<"t) tree g) game\n";while(cin >> choice){switch(choice){case'c':cout <<"A tiger is a carnivore";exit(EXIT_FAILURE);case'p':cout <<"Howard is a pianist";exit(EXIT_FAILURE);case't':cout <<"A maple is a tree";exit(EXIT_FAILURE);case'g':cout <<"Pubg is a game";exit(EXIT_FAILURE);default:cout <<"Please enter a c, p, t, or g: ";}}return0;}
4.
#include<iostream>constint strsize =20;constint Num =5;struct bop {char fullname[strsize];char title[strsize];char bopname[strsize];int preference;};intmain(void){usingnamespace std;
bop bops[Num]={{"Wimp Macho","C Programmer","WMPS",0},{"Raki Rhodes","Junior Programmer","RRW",1},{"Celia Laiter","Java Programmer","MIPS",2},{"Hoppy Hipman","Analyst Trainee","HHR",1},{"Pat Hand","Python Programmer","LOOPY",2}};
cout <<"Benevolent Order of Programmers Report\n";
cout <<"a. display by name b.display by title\n""c. display by bopname d.display by perference\n""q. quit\n";
cout <<"Enter your choice: ";char choice;
cin >> choice;while(choice !='q'){switch(choice){case'a':for(int i =0; i < Num; i++)
cout << bops[i].fullname << endl;break;case'b':for(int i =0; i < Num; i++)
cout << bops[i].title << endl;break;case'c':for(int i =0; i < Num; i++)
cout << bops[i].bopname << endl;break;case'd':for(int i =0; i < Num; i++){switch(bops[i].preference){case0: cout << bops[i].fullname << endl;break;case1: cout << bops[i].title << endl;break;case2: cout << bops[i].bopname << endl;break;}}break;}
cout <<"Next choice: ";
cin >> choice;}
cout <<"Bye!";return0;}