- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
大家好,感谢您花时间阅读本文。我正在做一个练习,我得到一个整数数组,每个整数代表图表中一个柱的“高度”。负值表示条形延伸到水平轴以下。因此,如果给我一个包含 4 个整数的数组,图表中将恰好有 4 个柱。
我使用了一个二维字符数组来尝试完成这个练习。
注意:练习的文本说图表中的每个条之间应该有两个空格,所以这就是为什么我将 j 控制变量递增三个每次通过。
我对负值有疑问。无论我做什么,负值都不会打印在屏幕上。
我的尝试如下:我通过将数组大小乘以三来确定图形的宽度。然后,我将二维数组的所有字段设置为空格,中间的水平线除外。
外层 j 循环遍历矩阵的列。我只有一个循环,因为在每次通过 j 循环时,我们都从第 10 行开始。
k 循环实际上进行绘图。循环从零到 number-1
, 在顶部(或底部)为星号留出空间。
我有两个 if 语句来检查数字是 <0 还是 >0,因为在这两种情况下,条形图应该以不同的方式显示。
这是我的代码:
#include <stdio.h>
int main()
{
char mat[30][40];
int i,j, size=0, m=20, n=40;
int arr[7] = {1,2,-3,4,-5,6,-7};
size=7;
n=size*3; // n is the width of the graph
for(i=0; i<m; i++) { // I set all fields of the 2D array to spaces
for(j=0; j<n; j++) { // except for the horizontal line
if(i==10) mat[i][j]='-';
else mat[i][j]=' ';
}
}
int k;
int position=0; // position is the variable I use to iterate through the array
int l;
for(j=0; j<n; j+=3) {
l=10;
for( k = 0; k<arr[position]-1; k++) {
if(arr[position]==0) {
mat[10][j] = '*'; // if the height is zero, put an asterisk
}
else if((arr[position])<0) {
l++; // if the number is negative,
mat[l][j]='|'; // the bar extends below the horizontal line
}
else if(arr[position]>0) {
l--; // if the number is positive, the bar extends above the
mat[l][j] = '|'; // horizontal line
}
}
if(arr[position]>0) mat[l-1][j]='*'; //every bar ends with an asterisk
else if(arr[position]<0) {
mat[l+1][j]='*';
}
position++; // jump to the next element in array
}
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
printf("%c",mat[i][j]);
}
printf("\n");
}
return 0;
}
对于负数,它根本不起作用。此外,我做了一些测试和条件 else if((arr[position])<0)
永远不会实现,我不知道为什么。它的作用是在水平线下方放置一个星号,仅此而已。
感谢阅读!
最佳答案
问题是这样的:
for( k = 0; k<arr[position]-1; k++)
如果 arr[position]
小于 2 则什么都不会发生。试试这个
.
.
.
l=10;
int end = arr[position] < 0 ? -arr[position] : arr[position]; // added code
for( k = 0; k < end - 1; k++) { // changed code
if(arr[position]==0) {
mat[10][j] = '*'; // if the height is zero, put an asterisk
}
.
.
.
输出
*
|
* |
| |
* | |
* | | |
---------------------
| | |
| | |
* | |
| |
* |
|
*
基本上采用 arr[position]
的绝对值并将其用作结束条件。
编辑:当 arr[position]
为 0
时使 astrix 出现。执行以下操作:
.
.
.
int end = arr[position] < 0 ? -arr[position] : arr[position];
if(arr[position]==0) {
mat[10][j] = '*'; // if the height is zero, put an asterisk
}
for( k = 0; k < end - 1; k++) {
.
.
.
所以从 for
循环中移出 if
因为如果 arr[position]
是 0
而不是循环不执行。
关于C - 使用二维数组绘制条形图 - 不会绘制负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60266026/
我学习 SDL 二维编程已有一段时间了,现在我想创建一个结合使用 SDL 和 OpenGL 的程序。我是这样设置的: SDL_Init(SDL_INIT_VIDEO); window = SDL_Cr
尝试查找可在地块中使用的不同类型项目的列表 来自不同样本的投影类型: projection = list(type = "equirectangular") projection = list(typ
我正在尝试使用 Java Graphics API 绘制 GIF,但无法使用下面的代码成功绘制 GIF。仅绘制 GIF 的第一张图像或缩略图,但不播放。 public void paintCompon
我目前正在使用 JFrame 并尝试绘制一个矩形,但我不知道如何执行代码 paint(Graphics g),如何获取 Graphics 对象? package com.raggaer.frame;
这个领域的新手,希望得到一些帮助。 我有一个"Missile.java" 类,我在那里画东西。我想绘制一个 ImageView,我正在使用以下代码: ImageView v = (ImageView)
下面列出了圆形的例子 这是我的 JavaScript 代码。 最佳答案 假设您的 randomColor 是正确的,您只需要: 从 canvas.onclick 中移除 context.clearR
我在绘制和缩放 ImageView 时遇到问题。请帮帮我.. 当我画一些东西然后拖动或缩放图像时 - 绘图保留在原处,如您在屏幕截图中所见。而且我只需要简单地在图片上绘图,并且可以缩放和拖动这张图片。
我们可以在形式之外绘制图像和文本...我的意思是在字面上... 我知道问这个问题很愚蠢但是我们能不能... 最佳答案 您可以通过创建表单并将其 TransparentColor 属性设置为背景色来“作
我在绘制/布局期间收到 3 个对象分配警告 super.onDraw(canvas); canvas.drawColor(Color.WHITE); Paint textPaint = new Pai
我有一个示例时间序列数据框: df = pd.DataFrame({'year':'1990','1991','1992','1993','1994','1995','1996',
我试图想出一种简洁的方法来绘制 R 数据框中所有列的 GridView 。问题是我的数据框中既有离散值又有数值。为简单起见,我们可以使用 R 提供的名为 iris 的示例数据集。我会使用 par(mf
我有一个由 10 列和 50 行组成的 data.frame。我使用 apply 函数逐列计算密度函数。现在我想绘制我一次计算的密度。 换句话说,而不是绘图... plot(den[[1]]) plo
我想知道我们如何才能在第一个和第二个组件之外绘制个人,如下所示: 最佳答案 这可能有效: pc.cr <- princomp(USArrests, cor = TRUE) pairs(pc.cr$lo
我是Pandas和matplotlib的新手,想绘制此DataFrame season won team matches pct_won 0 20
我正在尝试为 distplot 子图编写一个 for 循环。 我有一个包含许多不同长度列的数据框。 (不包括 NaN 值) fig = make_subplots( rows=len(asse
我想创建一个具有密度的 3d 图。 我使用函数 density 首先为特定的 x 值创建一个二维图,然后该函数创建密度并将它们放入 y 变量中。现在我有第二组 x 值并将其再次放入密度函数中,然后我得
全部, 我一直在研究全局所有 MTB 步道的索引。我是 Python 人,所以对于所有涉及的步骤,我都尝试使用 Python 模块。 我能够像这样从 OSM 立交桥 API 中获取关系: from O
我正在使用 e1071 包中的支持向量机对我的数据进行分类,并希望可视化机器实际如何进行分类。但是,在使用 plot.svm 函数时,出现无法解决的错误。 脚本: library("e1071") d
我制作了以下图表,它是使用 xts 对象创建的。 我使用的代码很简单 plot(graphTS1$CCLL, type = "l", las = 2, ylab = "(c)\nCC for I
在绘制状态图时,您如何知道哪些状态放在框中,哪些状态用于转换箭头?我注意到转换也是状态。 我正在查看 this page 上的图 1 : 最佳答案 转换不是状态。转换是将对象从一种状态移动到下一种状态
我是一名优秀的程序员,十分优秀!