C++ 垂直直方图不起作用
我的直方图程序有一个问题,我正在让它打印出我想要的东西,但是它有一些不寻常的间距问题,我想知道是否有人可以帮助我如何避免它。 这就是我的输出看起来的样子,因为你可以看到它在90-100括号之前的所有空格中都有错误的间距 我希望它看起来像什么,因此在数字和星号之间的底部没有空格:C++ 垂直直方图不起作用,c++,C++,我的直方图程序有一个问题,我正在让它打印出我想要的东西,但是它有一些不寻常的间距问题,我想知道是否有人可以帮助我如何避免它。 这就是我的输出看起来的样子,因为你可以看到它在90-100括号之前的所有空格中都有错误的间距 我希望它看起来像什么,因此在数字和星号之间的底部没有空格: * * * * * * * * * * * * * * * * * * * * * * * * * 0 1 2 3 还有谁能推荐一些东西来代替系统(“清除”)或系统(“暂停”) 代码: #
*
*
* *
* *
* *
* *
* *
* *
* * *
* * * *
* * * *
0 1 2 3
还有谁能推荐一些东西来代替系统(“清除”)
或系统(“暂停”)
代码:
#包括
#包括
使用名称空间std;
无效读取examMarks(int examMarks[],int sizeOfArray,int&counter1,int&counter2,int&counter3,int&counter4,int&counter5,int&counter6,int&counter7,int&counter8,int&counter9,int&counter10){
cout x;
如果((x>=0)&(x每个范围的变量计数器
不是您真正想要做的,那么您只需要一个数组:
#include <iostream>
using namespace std;
int main() {
// count array for brackets 0 to 10
int *count = new int[11];
// variable to store each mark read
int mark;
// Read in marks
while (cin >> mark) {
// Increment the count for the bracket the mark falls in
count[mark/10]++;
}
for (int i=0;i<11;i++) {
cout << i << "\t| " << string(count[i],'*') << endl;
}
}
#包括
使用名称空间std;
int main(){
//括号0到10的计数数组
整数*计数=新整数[11];
//变量来存储读取的每个标记
整数标记;
//读入分数
而(cin>>标记){
//增加标记所在括号的计数
计数[标记/10]++;
}
对于(int i=0;我请包括实际输出和您期望的内容。我们不是心灵感应。输出在imugur链接中,我将在编辑中包括我希望它看起来像什么的图片。我希望,您了解数组。但是,告诉我,为什么您使用10个计数器而不是计数器数组?另外,在10种情况下,您增加conter9
,而不是计数器10
@因为我还没有学会如何使用计数器数组,所以我不知道如何使用to@Cheersandhth.-如果我对它进行了编辑,以显示我希望它看起来像是什么,那么没有操作数匹配这些@Thomas应该是双引号“\t |”
。我不知道你的问题是什么,因为代码没有问题。重点不是把我的代码作为你的课程提交,我的重点是你在做错误的事情,使用单独的计数器变量和每个括号的case statemeter,使用数组!
#include <iostream>
using namespace std;
int main() {
// count array for brackets 0 to 10
int *count = new int[11];
// variable to store each mark read
int mark;
// Read in marks
while (cin >> mark) {
// Increment the count for the bracket the mark falls in
count[mark/10]++;
}
for (int i=0;i<11;i++) {
cout << i << "\t| " << string(count[i],'*') << endl;
}
}
$ ./histogram.out < marks
0 | ******
1 | *********
2 | **********
3 | ************
4 | *********
5 | *****************
6 | ********
7 | *********
8 | ***
9 | ****
10 | *