C++:使用数组、指针和 for 循环制作直方图。我就在那里,我可能想得太辛苦了

c++: Making a histogram with arrays, pointers, for loops. I'm right there, I'm probably thinking too hard

本文关键字:在那里 直方图 数组 指针 循环 for C++      更新时间:2023-10-16

这是我到目前为止所拥有的:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int* histogram (int arr[], int size);
double deviation (int arr[], int size);
double mean (int arr[], int size);
int main ()  {
int scores[100];
int count = 0;
int temp;
double sd;
cout << "***===***===Lab 5 - Stats===***===***n";
cout << "Welcome to the Historgam Generator and Stardard Deviation Caluclator!" << endl;
cout << "Enter some scores, 0 to 109, limit 100, enter -1 to quit." << endl;
while (count < 109) {//puts limit on array
cout << "Enter value " << count + 1 << ":   ";
cin >> temp; //initialize data
if (temp == -1) {
break;//end program
}
else if (temp < 0) {
cout << "An invalid score has been entered." << endl;
break;//end program
}
else {
scores[count] = temp;
count++;//valid number, continue counting when entered corretly
}
}
//help from here
//histogram (scores, count);
int* bin = histogram (scores, count);
for (int i = 0; i < count; i++) {
int displayNo = 0;
//bin[scores[count]]++;
//count is the number of numbers entered.
//scores[] is the array of scores entered by user
//bin[9] is the address of the element in the array
cout << displayNo << "| ";
for (int k = 0; k < scores[i]; k++) {
cout << '*';
}
cout << endl;
}
//to here
sd = deviation (scores, count);
cout << "SD: " << sd << endl;
//hold window open
system ("pause");
return 0;
}
int* histogram (int scores[], int size) {
int* bin = new int[10];
for (int i = 0; i < size; i++) {
if (scores[i] >= 90) {
bin[9]++;
}
else if (scores[i] >= 80 && scores[i] < 90) {
bin[8]++;
}
else if (scores[i] >= 70 && scores[i] < 80) {
bin[7]++;
}
else if (scores[i] >= 60 && scores[i] < 70) {
bin[6]++;
}
else if (scores[i] >= 50 && scores[i] < 60) {
bin[5]++;
}
else if (scores[i] >= 40 && scores[i] < 50) {
bin[4]++;
}
else if (scores[i] >= 30 && scores[i] < 40) {
bin[3]++;
}
else if (scores[i] >= 20 && scores[i] < 30) {
bin[2]++;
}
else if (scores[i] >= 10 && scores[i] < 20) {
bin[1]++;
}
else if (scores[i] >= 0 && scores[i] < 10) {
bin[0]++;
}
}
return bin;
}
//calculates standard deviation
double deviation (int scores[], int size) {
double avg = 0;
double sd = 0;
avg = mean (scores, size);
for (int i = 0; i < size; i++) {
sd += pow ((scores[i] - avg), 2);
}
sd = sqrt (sd / size);
return sd;
}
//calculates the mean/average
double mean (int scores[], int size) {
double sum = 0;
double mean = 0;
for (int i = 0; i < size; i++){
sum += scores[i];
}
mean = sum / size;
return mean;
}

我所要做的就是让我的程序中的直方图显示如下:

9| ***

8| **

7| *

6| ***

5| ******

4| *******

3| ****

2|

1|

0|

标清: 15.2579


那是如果我输入这个测试用例中的数字:

案例 2:30、40、45、102、35、42、65、89、55、48、56、46、42、54、56、51、47、50、51、50、50、47、52、53、47、44、69、35、40、45、35、42、65、55、48、100、56、46、46、54、56、51、47、50、51、50、50、47、52、53、47、78、80 和 95

我有SD部分,但我只是不知道与for循环一起使用什么逻辑来使其看起来像这样。我可以得到上面制作的零件的帮助吗?

****更新**** 以下是我的教练给我的指示:

计划要求

命名程序文件统计信息.cpp 不要使用任何全局变量 仅使用 iostream 和 iomanip 函数进行 I/O 和格式化(无 stdio) 从控制台读取一个整数列表,一次读取一个整数列表 将每个整数放入数组中 允许最多(不是总数)100 分(请参阅数组和循环(指向外部站点的链接)。指向外部站点的链接。- 页面底部的示例) 输入的分数都不会<0 计算输入的整数数 停止数据输入并在用户输入 -1 时开始计算(不要在数据中包含 -1) 回想一下,我使用测试台来帮助对您的实验室进行评分,这意味着它将是一个为您的程序提供输入的简单程序 - 如果您的程序偏离上述要求,测试台将失败。 按如下方式对直方图中的分数进行分组:

Bin 9: score ≥ 90
Bin 8: score ≥ 80 but < 90
Bin 7: score ≥ 70 but < 80
.
.
.
Bin 1: score ≥ 10 but < 20
Bin 0: score < 10

请注意,分数包括一些获得额外学分的分数;您的程序必须适用于最高 109 的分数。我使用了一个名为 bin 的整数数组,并在直方图函数中填充了数组:int bins[10];bins 数组中的每个元素都是一个计数器,用于计算特定范围内的分数数。打印直方图类似于打印 X 金字塔或绘制松树:使用两个 for 循环,一个嵌套在另一个循环中。外部循环遍历 bins 数组,内部循环在给定行上打印每个"*"。

每当您使用变量作为累加器或计数器时(即,您使用它类似于这样:sum += ...或 count++),它必须初始化为 0。自动变量和动态变量都不会自动初始化,因此程序员必须记住将其作为程序的一部分进行初始化。数组只是使用一个名称访问的变量列表,这意味着当数组用作一组累加器或计数器时,数组的每个元素都必须初始化为 0。请参阅图 2(指向外部站点的链接)。指向外部站点的链接..

您的程序将有四个函数(统计函数必须遵循 main):

在程序顶部为三个统计函数添加函数原型 主要 在此处定义阵列 在此处阅读数据 调用直方图 打印直方图(有关所需输出的示例,请参阅下面的"测试用例") 呼叫偏差 打印标准偏差(如果需要,可以组合步骤 v 和 vi) 直方图(传入您需要的任何参数) 计算直方图 返回直方图数组,作为函数返回值(使用 "return" 关键字)或通过参数列表返回。选择数组和函数(指向外部站点的链接)中说明的三种技术之一。指向外部站点的链接.. 偏差(传入您需要的任何参数,返回双精度) 呼叫平均值(即所有分数的平均值) 计算标准偏差 返回标准偏差 mean(传入您需要的任何参数,返回双精度) - 请参阅平均值.cpp (链接到外部站点。指向外部站点的链接。 额外积分 如果您提前完成作业,您可以通过尝试以下一项或两项额外学分作业来挑战自己:

(5 分)仅使用单个循环打印直方图。有关提示,请参阅我发布的实验 3 解决方案之一。 (5 分)仅使用计算完成要求 5(制作直方图 您只能使用一 (1) 个循环 您的单个循环可能只包含一个语句(使用逗号运算符不会作弊) 您不能使用 if、switch 或条件语句 不得使用函数调用 您不得将任何逻辑移动到打印函数中 您可以使用多个循环来打印直方图(除非您正在执行额外的信用 1),但不能填充数组 或者,对于 5 分的额外学分,请按上述方式完成要求 5,但您可以使用条件语句。

不确定你想要做什么,但就是这样吗?

void histogram(int scores[], int size, int *bin)
{
for (int i = 0; i < size; i++)
{
int binId = scores[i] / 10;
if (binId > 9) binId = 9;
bin[binId]++; 
}
}
// called from main:
int bin[10]; // declared here as global var, so it is well destroyed
histogram(scores, size, bin); // pass bin pointer, avoid returning pointer with assigned memory from within a fonction, this will trap you.

如果箱始终具有相同的"排序标准",则x/10实际上将下限值,因此您无需进行所有这些比较。


为了显示这样的东西?

for (int i = 0; i < 10; i++) 
{
cout << i << "| ";
for (int k = 0; k < bin[i]; k++) 
{
cout << '*';
}
cout << endl;
}

如果您迷失在循环中,那么将循环放入循环实际上会变得棘手。有些代码有 3-4 级循环,成为一场噩梦。重构为子函数将使代码更具可读性且更易于调试。

for (int i = 0; i < count; i++) 
{
cout << i << "| ";
print_stars(bin[i])
cout << endl;
}
void print_stars(int amount)
{
for (int k = 0; k < amount; k++) 
{
cout << '*';
}
}

在不得不自己完成这项任务之后,我想出了一种方法。目前这可能对您没有帮助,但对于任何新手来说,它都可以。我没有尝试返回动态数组,而是通过参数/参数返回它。

直方图原型

int* histogram(int arr[], int size, int* bin);

直方图函数

int* histogram (int arr[], int size, int* bin) {
for (int i = 0; i < size; i++) {
if (scores[i] >= 90) {
bin[9]++;
}
else if (scores[i] >= 80 && scores[i] < 90) {
bin[8]++;
}
else if (scores[i] >= 70 && scores[i] < 80) {
bin[7]++;
}
else if (scores[i] >= 60 && scores[i] < 70) {
bin[6]++;
}
else if (scores[i] >= 50 && scores[i] < 60) {
bin[5]++;
}
else if (scores[i] >= 40 && scores[i] < 50) {
bin[4]++;
}
else if (scores[i] >= 30 && scores[i] < 40) {
bin[3]++;
}
else if (scores[i] >= 20 && scores[i] < 30) {
bin[2]++;
}
else if (scores[i] >= 10 && scores[i] < 20) {
bin[1]++;
}
else if (scores[i] >= 0 && scores[i] < 10) {
bin[0]++;
}
}
return bin;
}

调用函数并在主函数中打印直方图

int bin[10];
histogram(scores, count, bin); 
for (int i = 9; i >= 0; i--) {
cout << i << "| ";
for (int j = 0; j < bin[i]; j++)
{
cout << '*';
}
cout << endl;
}