C
Ceriousmall
This is one of the programs from the K&R Ansi C Book that you have to
write I've had a go at it but got kinda stumped here. So here's my
effort so far...... And I'm aware there is a new ISO standard for C
C99 as of March 2000 and I'm getting around to learning and adopting
that as well but my main concern is actually learning the
language....
/* copyright 2011 Ceriousmall. . . .
Program prints a histogram of the length of words in its input */
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
main()
{
char x;
int c, i, n, nchar, state;
int wordlength[13];
int horscale[13], verscale[51];
x = '*';
state = OUT;
n = nchar = 0;
for (i = 0; i < 13; ++i) {
wordlength = 0;
horscale = i;
}
for (i = 0; i < 51; ++i)
verscale = i;
while ((c=getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) /* condition controlls the start */
state = IN; /* of a word */
if (state == IN)
++nchar;
else if (nchar == 1)
++wordlength[1];
else if (nchar == 2)
++wordlength[2];
else if (nchar == 3)
++wordlength[3];
else if (nchar == 4)
++wordlength[4];
else if (nchar == 5)
++wordlength[5];
else if (nchar == 6)
++wordlength[6];
if (state == OUT)
nchar = 0;
}
for (i = 50; i >= 0; --i)
if (wordlength[1] == verscale)
for (i = verscale; i >= 0; --i)
printf("%2d| %c\n", verscale, x);
printf(" +------------------------------------\n");
for (i = 0; i < 13; ++i)
printf("%3d", horscale);
printf("\n");
return 0;
}
write I've had a go at it but got kinda stumped here. So here's my
effort so far...... And I'm aware there is a new ISO standard for C
C99 as of March 2000 and I'm getting around to learning and adopting
that as well but my main concern is actually learning the
language....
/* copyright 2011 Ceriousmall. . . .
Program prints a histogram of the length of words in its input */
#include <stdio.h>
#define IN 1 /* inside a word */
#define OUT 0 /* outside a word */
main()
{
char x;
int c, i, n, nchar, state;
int wordlength[13];
int horscale[13], verscale[51];
x = '*';
state = OUT;
n = nchar = 0;
for (i = 0; i < 13; ++i) {
wordlength = 0;
horscale = i;
}
for (i = 0; i < 51; ++i)
verscale = i;
while ((c=getchar()) != EOF) {
if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT) /* condition controlls the start */
state = IN; /* of a word */
if (state == IN)
++nchar;
else if (nchar == 1)
++wordlength[1];
else if (nchar == 2)
++wordlength[2];
else if (nchar == 3)
++wordlength[3];
else if (nchar == 4)
++wordlength[4];
else if (nchar == 5)
++wordlength[5];
else if (nchar == 6)
++wordlength[6];
if (state == OUT)
nchar = 0;
}
for (i = 50; i >= 0; --i)
if (wordlength[1] == verscale)
for (i = verscale; i >= 0; --i)
printf("%2d| %c\n", verscale, x);
printf(" +------------------------------------\n");
for (i = 0; i < 13; ++i)
printf("%3d", horscale);
printf("\n");
return 0;
}