0

I have no idea what i'm doing wrong here, but I'm trying to make a vertical histogram program based on the length of strings input through the commad line using getchar() (one of The C Programming Language excercises), but something seems to be going wrong when I run it. The function printgraph() is supposed to print the histogram using the for loops shown by printing the graph, graph[][] line by line, where j increments the y axis and i increments the x axis. However, when I run this, the graph doesn't print when it reaches this line of code. I've revised the code and gone over it many times, and still haven't a clue. I know that this may also be a trivial question to some, and I apologize that I lack much experience, but all help is appreciated.

#include <stdio.h>

char graph[11][11];

void printgraph(){
    int i, j;
    char graph[11][11];
    for(j = 0; j<=10; j++){
        for(i = 0; i<=10; i++){
            putchar(graph[i][j]);
        }
        printf("\n");
    }
}
int main(){
    char c, graph[11][11];
    int i, j, onoroff, numchar[10];
    for(i = 10; i>=0; i--)
        graph[0][i] = i;
    for(j=10;j>=0; j--)
        graph[j][0] = j;
    for(j=0;j<=9;i++)
        numchar[j] = 0;
    onoroff = 1;
    i = 0;
    while(graph[1][10] != 'O' || graph[2][10] != 'O' || graph[3][10] != 'O' || graph[4][10] != 'O' || graph[5][10] != 'O' || graph[6][10] != 'O' || graph[7][10] != 'O' || graph[8][10] != 'O' || graph[9][10] != 'O' ||graph[10][10] != 'O'){
        while((c = getchar()) != EOF){
            printgraph();
            if(c == ' '|| c == '\n' || c == '\t'){
                if(onoroff == 1){
                    numchar[i]++;
                    graph[i][numchar[i]+1] = 'O';
                }   
                onoroff = 0;
                i = 0;
            }else if(onoroff == 1){
                i++;
            }else if(onoroff == 0){
                onoroff = 1;
                i++;
            }
        }
    }
    return 0;
}
1
  • 4
    Do not redeclare global variables inside functions. Better yet, avoid global variables altogether. Pass parameters to functions instead. Commented Nov 26, 2015 at 6:51

2 Answers 2

2

It never reaches the printgraph function since you are getting stuck in the third for loop.

for(j=0;j<=9;i++)
    numchar[j] = 0;

You are incrementing i but testing j

Also see the answer from NPToita

Sign up to request clarification or add additional context in comments.

Comments

1

it's because you have 3 variables named graph; the global graph variable is never used because the main function has its own local variable graph which it writes to and printgraph has its own version of graph variable which it reads from.

can you try deleting the graph variables declarations from the main and printgraph functions and see what happens?

1 Comment

or you can use n.m.'s suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.