Question

I'm doing the task 1.14 in "The C Programming Language" book and don't know what's going wrong in my code, especially in printing the diagram.

#include <stdio.h>

#define MAX 300

int main(void) {
    int c,n,k,i,g,p,f;
    int mas[MAX];

    for(i = 0; i < MAX; i++) //array for the string's length
        mas[i] = 0;

    i = 0;
    n = k = f = 0;

    //getting strings to print

    while((c = getchar()) != EOF) {
        n++;        //counter for each string in array
        if(c == '\n'){
            mas[i] = n;
            i++;
            if(n > k){  //the highest number to print
                k = n;
            }
            n = 0;
        }
    }

//printing diagrams. I'm trying to do this:
/*
  |
k |
  |
  |
  |
  |_____________________
              i  
*/

    for(; k > 0; k--){  //diagram's height
        for(g = 0; g < i; g++){ //diagram's length
            if(mas[g] = k){     //if an array have an appropriate height to print
                printf("%c", "#");
            } else
                putchar(' ');   
        }
        putchar('\n');
    }
    return 0;
}

You can get it in the ideone.com — http://ideone.com/1RZ1zU.

Was it helpful?

Solution

if(mas[g] = k){

you probably mean

if(mas[g] == k){

And this is wrong too:

printf("%c", "#");

%c expects a character, not a string:

printf("%c", '#');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top