猿问

The winner is candidate number 4 with 350 votes!??

Question 1
Write a function called print_pyramid() which takes a single integer argument height
and displays a pyramid of this height made up of "*" characters on the screen in a function.
Limit the user to enter only number from ONE (1) to FIFTEEN (15). A sample screen is as
below:
How high would you like the pyramid to be? 5
*
**
***
****
*****
****
***
**
*
Your program should prompt the user whether he/she would like to print another pyramid. If the
user chooses Yes, your program should repeat the above steps and print another pyramid. If the
user chooses No, terminate the program. You may consider using the following sample screen:
Do you want to print another pyramid (Y/N)? Y
How high would you like the pyramid to be? 2
*
**
*

Question 2 (2.5 marks)
In an election, TEN (10) candidates are contesting and ONE THOUSAND (1000) voters are
eligible for voting. The voters’ election is done at random. The number of votes will be randomly
generated by the program. Write a program that displays the number of votes each candidate got
and the winner of the election. Note that the output should be different every time the program is
executed. A sample screen is as below:
Candidate Number Number of votes
1 125
2 12
3 78
4 350
5 1
6 87
7 36
8 14
9 78
10 219

开满天机
浏览 147回答 3
3回答

慕的地8271018

给,已经编译运行确认:1.#include <iostream>using namespace std;void print_pyramid(int height);int main(){int pyramid_height;char ch;do{cout << "how high would you like the pyramid?: ";cin >> pyramid_height;while (pyramid_height > 30 || pyramid_height < 1){cout << "Pick another height (must be between 1 and 30): ";cin >> pyramid_height;}print_pyramid(pyramid_height);cout << "Do you want to print another pyramid (Y/N)? ";cin>>ch;}while(ch=='Y');return 0;}void print_pyramid(int height){int a,i;cout << "\n";for (a=0;a<height;a++){for (i=0;i<a;i++) cout<<'*';cout<<endl;}for (a=0;a<height;a++){for (i=height-1-a;i>=0;i--) cout<<'*';cout<<endl;}cout << "\n";}2.#include <iostream>#include <stdlib.h>#include <time.h>using namespace std;int main(){int a[10]={NULL},total=1000;int i;int max,maxList;srand((unsigned)time(NULL));for(i=0;i<10;i++){if(i!=9){a[i]=(rand()%1001)%(total+1);total-=a[i];}else a[9]=total;}max=a[0];maxList=0;for(i=1;i<=10;i++){cout<<i<<" "<<a[i-1]<<endl;if(a[i-1]>max) {max=a[i-1];maxList=i;}}cout<<"The winner is candidate number "<<maxList<<" with "<<max<<" votes!";system("pause");return 0;}

DIEA

#include <iostream.h>#include <conio.h>main(){int i,n,sum=0;int vote[10]={0};srand(time(NULL));//设置随机种子while(1)//循环,直到获得合适的票数{sum=0;//将总和归零for(i=0;i<10;i++)//循环10次,生成10个随机数{vote[i]=rand()%1000;//随机数在0-1000之间sum=sum+vote[i];//将生成的随机数累加,存在sum变量中}if(sum==1000)//判断是否等于确定的随机数总和break;}cout<<"candidate number number of votes"<<endl;int max=0;int maxi;for(i=0;i<10;i++)//输出票数,并比大小{if(max<vote[i]){max=vote[i];maxi=i;}cout<<i<<" "<<vote[i]<<endl;}cout<<"The winner is candidate number"<<maxi<<"with"<<max<<"votes!"<<endl;system("pause");//使程序在DOS窗口下暂停,可注释掉

HUX布斯

#include <iostream>using namespace std;int main(){int n,i,j;char ch='y';while(ch=='y'||ch=='Y'){cout<<"How high would you like the pyramid to be(1~15)?";cin>>n;while(n>15){cout<<"How high would you like the pyramid to be(1~15)?";cin>>n;}for(i=0;i<n;i++){for(j=0;j<i+1;j++)cout<<"*";cout<<endl;}for(i=0;i<n-1;i++){for(j=0;j<n-1-i;j++)cout<<"*";cout<<endl;}cout<<"Do you want to print another pyramid (Y/N)?";cin>>ch;}return 0;}
随时随地看视频慕课网APP
我要回答