Thread: Vertical histogram, finishing touches

  1. #1
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877

    Vertical histogram, finishing touches

    Hello all, I just finished up this vertical histogram (finally lol).

    My question is this, is it possible to change out the non incremented elements (basically any number in the array that is a 0), with either a blank character or a null?

    I've tried a lot of the same things that work with char arrays..

    Code:
    arrxy[y][x]=(arrxy[y][x]=='0') ? '\0' : arrxy[y][x];
    Also tried the same thing with if statements. Is it even possible to blot out the 0's?

    Here's the code, not well notated because most of it planned out beforehand in notepad. I wrote where the ternary statements and if's were, in the bottom for loop:

    Code:
    #include<stdio.h>
    main()
    {
        int arrxy[12][10];
        char lookgood;
        int c, scan;
        int y, x;
        int num[9]={0};
     
        for(y=0; y<=11; ++y){
            for(x=0; x<=9; ++x){
            arrxy[y][x]=0;
            }
        }
    
        while ((c=getchar())!=EOF){
            switch (c) {
                case '0':{
                    num[0]=(c=='0')? --num[0] : num[0];
                    arrxy[num[0]+12][c-'0']++;
                    break;
                }
                case '1':{
                    num[1]=(c=='1')? --num[1] : num[1];
                    arrxy[num[1]+12][c-'0']++;
                    break;
                }
                case '2':{
                    num[2]=(c=='2')? --num[2] : num[2];
                    arrxy[num[2]+12][c-'0']++;
                    break;
                }
                case '3':{
                    num[3]=(c=='3')? --num[3] : num[3];
                    arrxy[num[3]+12][c-'0']++;
                    break;
                }
                case '4':{
                    num[4]=(c=='4')? --num[4] : num[4];
                    arrxy[num[4]+12][c-'0']++;
                    break;
                }
                case '5':{
                    num[5]=(c=='5')? --num[5] : num[5];
                    arrxy[num[5]+12][c-'0']++;
                    break;
                }
                case '6':{
                    num[6]=(c=='6')? --num[6] : num[6];
                    arrxy[num[6]+12][c-'0']++;
                    break;
                }
                case '7':{
                    num[7]=(c=='7')? --num[7] : num[7];
                    arrxy[num[7]+12][c-'0']++;
                    break;
                }
                case '8':{
                    num[8]=(c=='8')? --num[8]: num[8];
                    arrxy[num[8]+12][c-'0']++;
                    break;
                }
                case '9':{
                    num[9]=(c=='9')? --num[9] : num[9];
                    arrxy[num[9]+12][c-'0']++;
                    break;
                }
                break;
            }
        }
        for (y=0; y<=11; ++y){
            for(x=0; x<=9; ++x){ 
    
                
                /** arrxy[y][x]=(arrxy[y][x]=='0') ? '\0' : arrxy[y][x] ;
                Also tried if statements, both with and without constant
                character indicators ' ' **/
                
    
    
                printf("%d%c", arrxy[y][x], lookgood=(x==9) ? '\n': ' ');
            }
        }
        printf("====================\n0 1 2 3 4 5 6 7 8 9");
    }
    Last edited by Alpo; 04-15-2014 at 10:51 PM. Reason: spelling is hard

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    This might be easier to accomplish in the printing loop itself. If the array value is zero, then you want to print "nothing" (blank space). Else, you want to print the vertical line as you're doing now. Many times, how data is represented internally, and how it is displayed on the output, can and should be distinct.

    Overall, this code looks pretty good. A few suggestions:

    - Use #defined named constants instead of magic numbers for your array indices; e.g.:

    Code:
    #define SIZE_Y 12
    #define SIZE_X 10
    - You can initialize the two-dimensional array to all zeroes the same way as you do the one dimensional array - since it's two dimensions, just use two sets of braces:

    Code:
    int arrxy[SIZE_Y][SIZE_X] = {{0}};
    Your "for()" loops appear to be working correctly; however, standard idiom is to use "<12" instead of "<=11". This will be especially important if you use the #defined named constants as mentioned in the first point above.

    Code:
    for(y=0; y<SIZE_Y; y++)
        // ...
    - I've mentioned to you before to be careful about using the ternary conditional operator so much. Apart from the fact that frequent use might make the code more difficult to reason at a glance, you also have some potential for undefined behavior - see here: Question 3.3

    ...most of it planned out beforehand in notepad.
    This makes me happy. Good work!

    [edit]

    It might be a good idea to be explicit with your form of "main()":

    Code:
    int main(void)
    {
        // code
    
        return 0;
    }
    Last edited by Matticus; 04-16-2014 at 08:30 AM.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    @Matticus - Thanks, I just got it, I didn't think to have the array display as %c in the printf. #Defines are in now. Will use the 2d array initializer in the future, I had it originally with a single bracket around the 0 and couldn't figure out why I was getting junk lol. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program, Need help with finishing touches.
    By bgarner151 in forum C Programming
    Replies: 5
    Last Post: 12-10-2012, 06:30 PM
  2. help finishing this program
    By j.ward91 in forum C Programming
    Replies: 16
    Last Post: 04-26-2012, 10:44 AM
  3. Need help finishing a few functions.
    By Mo777 in forum C Programming
    Replies: 14
    Last Post: 03-09-2011, 06:04 PM
  4. some final touches... pls help out
    By zattara13 in forum C Programming
    Replies: 2
    Last Post: 04-02-2008, 02:26 PM
  5. Finishing up
    By Gnoober in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2002, 06:18 AM

Tags for this Thread