为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 146 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信