C++高手请进,写代码(在线等,急)

Question1Writeafunctioncalledprint_pyramid()whichtakesasingleintegerargumentheightand... 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
The winner is candidate number 4 with 350 votes!
展开
 我来答
匿名用户
2009-03-09
展开全部
给,已经编译运行确认:
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;
}
perijo
2009-03-09 · 超过21用户采纳过TA的回答
知道答主
回答量:51
采纳率:0%
帮助的人:0
展开全部
#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窗口下暂停,可注释掉
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
chenhui1990212
2009-03-09
知道答主
回答量:2
采纳率:0%
帮助的人:0
展开全部
#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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
lihaosky123
2009-03-09 · TA获得超过134个赞
知道答主
回答量:143
采纳率:0%
帮助的人:0
展开全部
作业吧,这么简单都不做,唉!!!!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式