Hello everybody,
without any prior knowledge of programming, I have started to learn C thru a book "C Programming Language" by K&R.
While I have followed it with ease up until now, I am stuck at the 1.13 assignment where I have to print a vertical histogram of word length.
I have found the solution on the web, but have hard time figuring out what it really does. Here is the code.
Code:
#include <stdio.h>
#define MAXWL 20
#define MAXWN 30
int main()
{
int word[MAXWN]; // defines the array of 30 values (max word number)
int i, c, j, nc, nw;
for (i=0; i < MAXWN; ++i)
word[i]=0; // sets all values of array to 0
nw = nc = 0; // sets counters to 0
while ((c=getchar()) != EOF){
++nc; // counts characters
if ( c == ' ' || c == '\t' || c == '\n'){
word[nw] = nc -1; // if white space, subtract 1 from counter value
++nw; // than add that value to array position (first 0, than 1 etc.)
nc = 0; // reset character counter
}
}
for (i=MAXWL; i>=1; --i){ // this prints the histogram
for (j=0; j<= nw; ++j){ // but how it does it
if (i<=word[j]) // is beyond me....
putchar('*');
else
putchar(' ');
}
putchar('\n');
}
return 0;
}
Could someone walk me thru the mechanics of the last part of the code so I can understand the process and continue my studies?
Thank you