K&R2 1.6 Arrays, exercise 1-13

A

arnuld

it works fine, any advice for improvement:

--------- PROGRAMME -------------

/* K&R2: 1.6 Arrays, exercise 1-13

STATEMENT:
write a programme to print a histogram of the lengths of words
in input. it is easy to draw the histogram with bars horizontal;
a vertical orientation i smore challenging.

NOTE: this code uses HORIZONTAL bars.


METHOD:


1.) we will count the length of each word by counting the
characters it has.

2.) we use an array where we will keep track of how many words
of a specific length we have encountered.

3.) for simplicity, we will not take into account any words
having more than 10 characters in it. extra characters will
simply be discarded.

*/


#include<stdio.h>

#define IN 1
#define OUT 0
#define MAXLENGTH 11

int main()
{
int i = 0;
int c = 0;
int nchar = 0;
int inspace = IN;
int wordlen[MAXLENGTH];

for(i = 0; i < MAXLENGTH; ++i)
wordlen = 0;

while((c = getchar()) != EOF)
{
if(c == ' ' || c == '\t' || c == '\n')
{
if (inspace == OUT)
{
if(nchar > MAXLENGTH)
{
nchar = 0;
++wordlen[nchar];
}
else
++wordlen[nchar];
}

inspace = IN;
nchar = 0;
}
else
{
++nchar;
inspace = OUT;
}
}


for(i = 1; i < MAXLENGTH; ++i)
printf("%2d|: %d\n", i, wordlen);


return 0;
}

--------------- OUTPUT --------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-13.c
[arch@voodo kr2]$ ./a.out
like this
and
......................... about
1|: 0
2|: 0
3|: 1
4|: 2
5|: 1
6|: 0
7|: 0
8|: 0
9|: 0
10|: 0
[arch@voodo kr2]$
 
S

santosh

arnuld said:
it works fine, any advice for improvement:

--------- PROGRAMME -------------

/* K&R2: 1.6 Arrays, exercise 1-13

STATEMENT:
write a programme to print a histogram of the lengths of words
in input. it is easy to draw the histogram with bars horizontal;
a vertical orientation i smore challenging.

NOTE: this code uses HORIZONTAL bars.

No. It doesn't use any bars.

3.) for simplicity, we will not take into account any words
having more than 10 characters in it. extra characters will
simply be discarded.

Needless limitation.

--------------- OUTPUT --------------
[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-13.c

Use -O3. Also some installations of gcc do not support the -Wextra
switch, but since it's the same as -W, you might use the latter.
[arch@voodo kr2]$ ./a.out
like this
and
........................ about
1|: 0
2|: 0
3|: 1
4|: 2
5|: 1
6|: 0
7|: 0
8|: 0
9|: 0
10|: 0
[arch@voodo kr2]$

This is not a histogram. Further your program doesn't properly handle
quoted words: the quotes are counted as part of the word's length.
 
A

arnuld

No. It doesn't use any bars.
....[SNIP]...

This is not a histogram. Further your program doesn't properly handle
quoted words: the quotes are counted as part of the word's length.

OK, OK, my mistake. here is the complete version:

-------- PROGRAMME ----------
/* K&R2: 1.6 Arrays, exercise 1-13

STATEMENT:
write a programme to print a histogram of the lengths of words
in input. it is easy to draw the histogram with bars horizontal;
a vertical orientation i smore challenging.

NOTE: this code uses HORIZONTAL bars.


METHOD:


1.) we will count the length of each word by counting the
characters it has.

2.) we use an array where we will keep track of how many words
of a specific length we have encountered.

3.) for simplicity, we will not take into account any words
having more than 10 characters in it. extra characters will
simply be discarded.

*/


#include<stdio.h>

#define IN 1
#define OUT 0
#define MAXLENGTH 11

int main()
{
int i = 0;
int j =0;
int c = 0;
int nchar = 0;
int inspace = IN;
int wordlen[MAXLENGTH];

for(i = 0; i < MAXLENGTH; ++i)
wordlen = 0;

while((c = getchar()) != EOF)
{
if(c == ' ' || c == '\t' || c == '\n')
{
if (inspace == OUT)
{
if(nchar < MAXLENGTH)
++wordlen[nchar];
}

inspace = IN;
nchar = 0;
}
else
{
++nchar;
if(c == '"' || c == '\'')
--nchar;
inspace = OUT;
}
}


/* printing the Horizontal-Histogram */
for(i = 1; i < MAXLENGTH; ++i)
{
printf("%2d| ", i);
for(j = 0; j < wordlen; ++j)
putchar('*');

putchar('\n');
}


return 0;
}

--------- OUTPUT ------------
[arch@voodo kr2]$ ./a.out
like "this" and 'this"
1|
2|
3| *
4| ***
5|
6|
7|
8|
9|
10|
[arch@voodo kr2]$

----- ITSELF as INPUT ---------
1| *********************************************
2| *************************************************
3| ****************************
4| *******************************
5| *******************
6| *******
7| **********
8| **********
9| ****
10| ***********
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top