user3422750
user3422750

Reputation: 3

C programming exercise?

I'm reading a book on C programming and try to do the exercises:

Write a program that prints a horizontal histogram consisting of stars(*). One star for every number there can be in the interval 0 ... 70.

#include <stdio.h>

void draw_stars(int number_of_stars) {
    int counter = 0;

    for(counter = 0; counter < number_of_stars; counter++) {
        printf("*");
    }

    printf("\n");
}

int main(void) {

    int counter1, counter2, ones, tens, zero, one, two, three, four, five, six, seven, eight, nine = 0;

    for(tens = 0; tens < 7; tens++) {
         if(tens == 0)
             zero = zero + 1;
         if(tens == 1)
             one = one + 10;
         if(tens == 2)
             two = two + 10;
         if(tens == 3)
             three = three + 10;
         if(tens == 4)
             four = four + 10;
         if(tens == 5)
             five = five + 10;
         if(tens == 6)
             six = six + 10;
         if(tens == 7)
             seven = seven + 10;

             for(ones = 0; ones < 9; ones++) {
                 if(ones == 0)
                     zero++;
                 if(ones == 1)
                     one++;
                 if(ones == 2)
                     two++;
                 if(ones == 3)
                     three++;
                 if(ones == 4)
                     four++;
                 if(ones == 5)
                     five++;
                 if(ones == 6)
                     six++;
                 if(ones == 7)
                     seven++;
                 if(ones == 8)
                     eight++;
                 if(ones == 9)
                     nine++;
        }
    }

draw_stars(zero);
draw_stars(one);
draw_stars(two);
draw_stars(three);
draw_stars(four);
draw_stars(five);
draw_stars(six);
draw_stars(seven);
draw_stars(eight);
draw_stars(nine);

}

For some reason my program enters a infinite loop printing stars. But I can't find out why?

I haven't been able to come up with any other solution, but I still think it's ugly and bloated. How would a real C programmer solve this?

Edit:

After reading and understanding the chapter about arrays in the book, I was able to write a more clean version of the program. I'm posting it here as it might help other beginners understand the use of arrays. Writing an identical program in terms of output, but using different functionality of the language is a great learning experience.

#include <stdio.h>

#define TENS 7

void draw_stars(int stars) {
    int star_counter = 0;

    for (star_counter = 0; star_counter < stars; star_counter++)
        printf("%c", '*');
}

int main(void) {

    int number_array[10];
    int tens_counter, ones_counter;

    for (ones_counter = 0; ones_counter < 10; ones_counter++)
        number_array[ones_counter] = 0;

    for (tens_counter = 0; tens_counter < TENS; tens_counter++) {
        if (tens_counter != 0)
            number_array[tens_counter] += 10;
        else
            number_array[tens_counter] += 1;

        for (ones_counter = 0; ones_counter < 10; ones_counter++)
            number_array[ones_counter]++;
    }

    for (ones_counter = 0; ones_counter < 10; ones_counter++) {
            draw_stars(number_array[ones_counter]);
            printf("\n");
    }
}

Upvotes: 0

Views: 452

Answers (3)

Rishi Dwivedi
Rishi Dwivedi

Reputation: 928

in main() you have to initialize all variable with zero like

  int counter1=0,counter2=0 and so on

otherwise it take garbage value and perform operation with those values and then output will be like

********
*****************
*****************
*****************
*****************
*****************
*****************
*******
*******

Upvotes: 1

Raging Bull
Raging Bull

Reputation: 18767

Initialize all variables:

int counter1=0, counter2=0, ones=0, tens=0, zero=0, one=0..

BTW, for better performance, replace if with else if except the first one in the block. Why do you want to check all if conditions when you already know only one is true?

FYI, when a condition is true in Else if, all other ifs are skipped.

Upvotes: 1

Spock77
Spock77

Reputation: 3325

If you wanted to initialize all ints to zero, you should write:

int counter1=0, counter2=0, ...

Now only nine is init by 0, other variables contain rubbish - arbitrary values.

Upvotes: 1

Related Questions