亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

開滿天機 2022-05-31 15:11:21
Question 1Write a function called print_pyramid() which takes a single integer argument heightand 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 asbelow: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 theuser chooses Yes, your program should repeat the above steps and print another pyramid. If theuser chooses No, terminate the program. You may consider using the following sample screen:Do you want to print another pyramid (Y/N)? YHow 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 areeligible for voting. The voters’ election is done at random. The number of votes will be randomlygenerated by the program. Write a program that displays the number of votes each candidate gotand the winner of the election. Note that the output should be different every time the program isexecuted. A sample screen is as below:Candidate Number Number of votes1 1252 123 784 3505 16 877 368 149 7810 219
查看完整描述

3 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

給,已經編譯運行確認:
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;
}



查看完整回答
反對 回復 2022-06-06
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

#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窗口下暫停,可注釋掉


查看完整回答
反對 回復 2022-06-06
?
HUX布斯

TA貢獻1876條經驗 獲得超6個贊

#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;
}


查看完整回答
反對 回復 2022-06-06
  • 3 回答
  • 0 關注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號