A
arnuld
this programme runs without any error but it does not do what i want
it to do:
------------- PROGRAMME --------------
/* K&R2, section 1.6 Arrays; Exercise 1-13.
STATEMENT:
Write a program to print a histogram of the lengths of words in its
input.
It is easy to draw the histogram with the bars horizontal; a vertical
orientation is more challenging.
Method:
1.) we will store lenth of each wordn an array.
2.) for keeping simplicity, array size is 1000, i.e.
it can hold only 1000 words.
3.) after EOF is encountered then the "a Hostogram of starts ( * )
"will be
printed on the screen.
4.) each line of histogram will contain number of "stars" == length
of that word.
*/
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXWORDS 1000
int main()
{
int c;
int i = 0;
int aindex = 0;
int j = 0; /* "i,j,count" are general index counters */
int nc = 0; /* length of word or number of characters in a word */
int lwords[MAXWORDS + 1];
int nw = 0; /* number of words */
/* length of each word is stored in this array
and will be printed in the end */
int state = IN;
while( ((c = getchar()) != EOF) && (nw <= MAXWORDS) )
{
++nc;
if(c == ' ' || c == '\t' || c == '\n')
{
state = OUT;
--nc;
}
else if(state == OUT)
{
lwords[aindex++] = nc;
++nw;
state = IN;
}
}
printf("---------- printing HISTOGRAM -----------\n");
for(i = 0; i < aindex; ++i)
{
for(j = 0; j < lwords; ++j)
putchar('*');
putchar('\n');
}
return 0;
}
------------- OUTPUT ---------------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-13.c
[arch@voodo kr2]$ ./a.out
like
---------- printing HISTOGRAM -----------
[arch@voodo kr2]$
it to do:
------------- PROGRAMME --------------
/* K&R2, section 1.6 Arrays; Exercise 1-13.
STATEMENT:
Write a program to print a histogram of the lengths of words in its
input.
It is easy to draw the histogram with the bars horizontal; a vertical
orientation is more challenging.
Method:
1.) we will store lenth of each wordn an array.
2.) for keeping simplicity, array size is 1000, i.e.
it can hold only 1000 words.
3.) after EOF is encountered then the "a Hostogram of starts ( * )
"will be
printed on the screen.
4.) each line of histogram will contain number of "stars" == length
of that word.
*/
#include <stdio.h>
#define IN 1
#define OUT 0
#define MAXWORDS 1000
int main()
{
int c;
int i = 0;
int aindex = 0;
int j = 0; /* "i,j,count" are general index counters */
int nc = 0; /* length of word or number of characters in a word */
int lwords[MAXWORDS + 1];
int nw = 0; /* number of words */
/* length of each word is stored in this array
and will be printed in the end */
int state = IN;
while( ((c = getchar()) != EOF) && (nw <= MAXWORDS) )
{
++nc;
if(c == ' ' || c == '\t' || c == '\n')
{
state = OUT;
--nc;
}
else if(state == OUT)
{
lwords[aindex++] = nc;
++nw;
state = IN;
}
}
printf("---------- printing HISTOGRAM -----------\n");
for(i = 0; i < aindex; ++i)
{
for(j = 0; j < lwords; ++j)
putchar('*');
putchar('\n');
}
return 0;
}
------------- OUTPUT ---------------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-13.c
[arch@voodo kr2]$ ./a.out
like
---------- printing HISTOGRAM -----------
[arch@voodo kr2]$