C - 使用二维数组绘制条形图 - 不会绘制负值

标签 c arrays

大家好,感谢您花时间阅读本文。我正在做一个练习,我得到一个整数数组,每个整数代表图表中一个柱的“高度”。负值表示条形延伸到水平轴以下。因此,如果给我一个包含 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/

相关文章:

c++ - 以任何方式多次打开共享内存是否不好?

python - 在 Python 中查找 2 个列表中相同元素的数量

java - 如何将数组转换为字符数组?

PHP 数组数字

C 编程 fprintf 问题

c - int32_t 到 int64_t(或 float )

c - 改进递归哈达玛变换

c - 浮点二进制可移植性

c# - 查找一个字节数组是否包含另一个字节数组的最快方法是什么?

python - 如何将不同大小的数组列表从 python 传递到 C?