数组字符串

要求

输入多组身高数据
求取平均值
输出高于平均值的身高数目

使用 do-while 语句
重点在于 height[++count]用于 数目统计使用
代码演示:

#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 << "输入的是身高数为:"<<count<<endl;
        for (int i=1;i<=count;i++)
        {
            sum = sum + height[i];
        }
        ave = sum / count;
        cout << "ave" << ave << endl;

        
        //计数
        for (int i=1;i<=count;i++)
        {
            if (height[i]>ave)
            {
                overavecount++;
            }

        }
      cout << "身高超过平均值的人数:" << overavecount;
        system("pause");
        return;
}

获取位置数组中的元素个数
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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容