要求
輸入多組身高數(shù)據(jù)
求取平均值
輸出高于平均值的身高數(shù)目
使用 do-while 語句
重點在于 height[++count]用于 數(shù)目統(tǒng)計使用
代碼演示:
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
int height[10] ;
char reply = 0;
int count = 0;
double ave = 0.0;
double sum = 0.0;
char ch = 0;
int overavecount = 0;
do
{
cout << "身高:";
cin >> height[++count];
cout << "是否輸入height:" << endl;
cin >> reply;
} while (count<10&& reply =='y');
cout << "輸入的是身高數(shù)為:"<<count<<endl;
for (int i=1;i<=count;i++)
{
sum = sum + height[i];
}
ave = sum / count;
cout << "ave" << ave << endl;
//計數(shù)
for (int i=1;i<=count;i++)
{
if (height[i]>ave)
{
overavecount++;
}
}
cout << "身高超過平均值的人數(shù):" << overavecount;
system("pause");
return;
}
獲取位置數(shù)組中的元素個數(shù)
sizeof
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
int a = 0;
int height[] = { 1,2,3,4,5,6,7,8 };
a =( sizeof(height))/(sizeof(height[0]));
cout << a;
system("pause");
return;
}