编写程序 分别使用输入 输出 求平均值函数



#include<iostream>


#include<cctype>


using namespace std;
const int SIZE = 10;
struct GOLF
{
char name[SIZE];
float grade;
};


void init_array(GOLF *str);
void print_array_name(GOLF str);
void print_array_grade(GOLF str);
void average(GOLF *str, int number);




int main(void)
{
GOLF *p = new GOLF[SIZE];
char ch;
int i = 0;
int m = 0;
cout << "please a char Y/y continue and now you can input  " << SIZE-i << "  data:";
while(cin>>ch && ('Y' == ch || ch =='y')&&i < SIZE)
{
cout << "Enter a name and the grade:";
init_array(&p[i]);
++i;
cout <<"You can input  " << SIZE - i << "  data:" << "Continue: (Y or n)" ;
}


m = i;
average(p, m);
m = 0;
while (m < i)
{
print_array_name(p[m]);
++m;
}
cout << endl;
m = 0;
while (m < i)
{
print_array_grade(p[m]);
++m;
}


delete[] p;
cin.get();
cin.get();
return 0;
}


void init_array(GOLF *str)
{
cin >> str->name >> str->grade;
}
void print_array_name(GOLF str)
{
cout << str.name << "\t";
}
void print_array_grade(GOLF str)
{
cout << str.grade << "\t";
}


void average(GOLF *str, int number)
{
float num = 0;
for (int i = 0;i < number;++i)
num += str[i].grade;
cout << number << " number" << endl;
cout << "avarage = " << num / number << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值