C++ 代码统计比 Jay 身高高的人数
以下是使用 C++ 编写的程序,计算比 Jay 身高高的人数:
#include <iostream>
using namespace std;
int countTaller(int jayHeight, int heights[], int size) {
int count = 0;
for (int i = 0; i < size; i++) {
if (heights[i] > jayHeight) {
count++;
}
}
return count;
}
int main() {
int jayHeight = 150;
int heights[] = {170, 165, 175, 175, 160, 175};
int size = sizeof(heights) / sizeof(heights[0]);
int tallerCount = countTaller(jayHeight, heights, size);
cout << '比 Jay 身高高的人数为:' << tallerCount << endl;
return 0;
}
这段程序定义了一个名为countTaller的函数,该函数接受 Jay 的身高、一个身高数组以及数组的大小作为参数,计算比 Jay 身高高的人数,并将结果返回。在main函数中,首先定义了 Jay 的身高为 150,然后定义了一个身高数组heights,并初始化了数组中每个人的身高。接下来,计算身高数组中比 Jay 身高高的人数,将结果存储在tallerCount变量中。最后,使用cout语句将比 Jay 身高高的人数输出到显示器上。
原文地址: https://www.cveoy.top/t/topic/SNj 著作权归作者所有。请勿转载和采集!