Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Exercise 1-13.

Name: Anonymous 2008-12-14 18:01

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.


The horizontal version:


#include <stdio.h>

#define IN      1
#define OUT     0
#define MAX_LEN 10

int main() {
        int c, i, j, state;

        long length = 0, wlength[MAX_LEN];

        for (i = 0; i < MAX_LEN; ++i)
                wlength[i] = 0;

        while ((c = getchar()) != EOF) {
                if (c == ' ' || c == '\t' || c == '\n') {
                        if (length < MAX_LEN && state == IN)
                                ++wlength[length];
                        else if (length >= MAX_LEN && state == IN)
                                ++wlength[MAX_LEN - 1];
                        ++wlength[0];
                        length = 0;
                        state = OUT;
                }
                else {
                        ++length;
                        state = IN;
                }
        }

        if(state == IN)
                ++wlength[length];

        printf("\nno. of:\n");

        for (i = 0; i < MAX_LEN; ++i) {
                if(i == 0)
                        printf("wspace chars: ");
                else
                        printf("%i-char words: ", i);
                for (j = 1; j <= wlength[i]; ++j)
                        printf("#");
                printf("\n");
        }

        return 0;
}


:/ ?

Name: Anonymous 2008-12-14 18:17

Whoops...


        while ((c = getchar()) != EOF) {
                if (c == ' ' || c == '\t' || c == '\n') {
                        if (length < MAX_LEN)
                                ++wlength[length];
                        else
                                ++wlength[MAX_LEN - 1];
                        ++wlength[0];
                        length = 0;
                        state = OUT;
                }
                else {
                        ++length;
                        state = IN;
                }
        }

        if(state == IN) {
                if(length < MAX_LEN)
                        ++wlength[length];
                else
                        ++wlength[MAX_LEN - 1];
        }



root@cake:~/devel/tcpl/c# ./histo-h
I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
no. of:
wspace chars: #############
1-char words: ##
2-char words: ##
3-char words: ####
4-char words: ##
5-char words: #
6-char words:
7-char words: ##
8-char words:
9-char words: #

Name: Anonymous 2008-12-14 22:15

this could done in one line of lisp

Name: Anonymous 2008-12-14 23:06

this could be done in one character of haskell

Name: Anonymous 2008-12-14 23:07

this could be done in one nibble of python

Name: Anonymous 2008-12-14 23:07

this could be done in one bit of ocaml

Name: Anonymous 2008-12-14 23:18

>>3
it could be done in one line of c, too:
#include <limits.h>
#include <stdio.h>
int main(int argc, char **argv){int maxlen = 0, spaces = 0, words[LINE_MAX] = {0}; for(int c = getchar(), len = 0; !feof(stdin); c = getchar()) switch(c){ case ' ': case '\t': case '\n': ++spaces; ++words[len]; maxlen = len > maxlen ? len : maxlen; len = 0; break; default: ++len; } fputs("no. of:\nwspace chars: ", stdout); for(int i = 0; i < spaces; ++i) putchar('#'); puts(""); for(int i = 1; i <= maxlen; ++i){ printf("%d-char words: ", i); for(int j = 0; j < words[i]; ++j) putchar('#'); puts(""); } }

Name: Anonymous 2008-12-14 23:24

>>7
Ugh.

Name: Anonymous 2008-12-14 23:32

>>7
that is 3 lines of c.

Name: Anonymous 2008-12-15 0:09

>>9
no, it's two lines of preprocessor and one line of c.

Name: Anonymous 2008-12-15 0:10

This looks like job for Haskell: The Ironic Hipster Programming Language.

Name: Anonymous 2008-12-15 0:10

>>9
]typedef __signed char __int8_t; typedef unsigned char __uint8_t; typedef short __int16_t; typedef unsigned short __uint16_t; typedef int __int32_t; typedef unsigned int __uint32_t; typedef long long __int64_t; typedef unsigned long long __uint64_t; typedef long __darwin_intptr_t; typedef unsigned int __darwin_natural_t; typedef int __darwin_ct_rune_t; typedef union { char __mbstate8[128]; long long _mbstateL; } __mbstate_t; typedef __mbstate_t __darwin_mbstate_t; typedef int __darwin_ptrdiff_t; typedef long unsigned int __darwin_size_t; typedef __builtin_va_list __darwin_va_list; typedef int __darwin_wchar_t; typedef __darwin_wchar_t __darwin_rune_t; typedef int __darwin_wint_t; typedef unsigned long __darwin_clock_t; typedef __uint32_t __darwin_socklen_t; typedef long __darwin_ssize_t; typedef long __darwin_time_t; struct __darwin_pthread_handler_rec { void (*__routine)(void *); void *__arg; struct __darwin_pthread_handler_rec *__next; }; struct _opaque_pthread_attr_t { long __sig; char __opaque[36]; }; struct _opaque_pthread_cond_t { long __sig; char __opaque[24]; }; struct _opaque_pthread_condattr_t { long __sig; char __opaque[4]; }; struct _opaque_pthread_mutex_t { long __sig; char __opaque[40]; }; struct _opaque_pthread_mutexattr_t { long __sig; char __opaque[8]; }; struct _opaque_pthread_once_t { long __sig; char __opaque[4]; }; struct _opaque_pthread_rwlock_t { long __sig; char __opaque[124]; }; struct _opaque_pthread_rwlockattr_t { long __sig; char __opaque[12]; }; struct _opaque_pthread_t { long __sig; struct __darwin_pthread_handler_rec *__cleanup_stack; char __opaque[596]; }; typedef __int64_t __darwin_blkcnt_t; typedef __int32_t __darwin_blksize_t; typedef __int32_t __darwin_dev_t; typedef unsigned int __darwin_fsblkcnt_t; typedef unsigned int __darwin_fsfilcnt_t; typedef __uint32_t __darwin_gid_t; typedef __uint32_t __darwin_id_t; typedef __uint64_t __darwin_ino64_t; typedef __uint32_t __darwin_ino_t; typedef __darwin_natural_t __darwin_mach_port_name_t; typedef __darwin_mach_port_name_t __darwin_mach_port_t; typedef __uint16_t __darwin_mode_t; typedef __int64_t __darwin_off_t; typedef __int32_t __darwin_pid_t; typedef struct _opaque_pthread_attr_t __darwin_pthread_attr_t; typedef struct _opaque_pthread_cond_t __darwin_pthread_cond_t; typedef struct _opaque_pthread_condattr_t __darwin_pthread_condattr_t; typedef unsigned long __darwin_pthread_key_t; typedef struct _opaque_pthread_mutex_t __darwin_pthread_mutex_t; typedef struct _opaque_pthread_mutexattr_t __darwin_pthread_mutexattr_t; typedef struct _opaque_pthread_once_t __darwin_pthread_once_t; typedef struct _opaque_pthread_rwlock_t __darwin_pthread_rwlock_t; typedef struct _opaque_pthread_rwlockattr_t __darwin_pthread_rwlockattr_t; typedef struct _opaque_pthread_t *__darwin_pthread_t; typedef __uint32_t __darwin_sigset_t; typedef __int32_t __darwin_suseconds_t; typedef __uint32_t __darwin_uid_t; typedef __uint32_t __darwin_useconds_t; typedef unsigned char __darwin_uuid_t[16]; typedef int __darwin_nl_item; typedef int __darwin_wctrans_t; typedef unsigned long __darwin_wctype_t; typedef __darwin_va_list va_list; typedef __darwin_off_t off_t; typedef __darwin_size_t size_t; typedef __darwin_off_t fpos_t; struct __sbuf { unsigned char *_base; int _size; }; struct __sFILEX; typedef struct __sFILE { unsigned char *_p; int _r; int _w; short _flags; short _file; struct __sbuf _bf; int _lbfsize; void *_cookie; int (*_close)(void *); int (*_read) (void *, char *, int); fpos_t (*_seek) (void *, fpos_t, int); int (*_write)(void *, __const char *, int); struct __sbuf _ub; struct __sFILEX *_extra; int _ur; unsigned char _ubuf[3]; unsigned char _nbuf[1]; struct __sbuf _lb; int _blksize; fpos_t _offset; } FILE; extern FILE *__stdinp; extern FILE *__stdoutp; extern FILE *__stderrp; void clearerr(FILE *); int fclose(FILE *); int feof(FILE *); int ferror(FILE *); int fflush(FILE *); int fgetc(FILE *); int fgetpos(FILE * , fpos_t *); char *fgets(char * , int, FILE *); FILE *fopen(__const char * , __const char * ); int fprintf(FILE * , __const char * , ...) ; int fputc(int, FILE *); int fputs(__const char * , FILE * ) __asm("_" "fputs" "$UNIX2003"); size_t fread(void * , size_t, size_t, FILE * ); FILE *freopen(__const char * , __const char * , FILE * ) __asm("_" "freopen" "$UNIX2003"); int fscanf(FILE * , __const char * , ...) ; int fseek(FILE *, long, int); int fsetpos(FILE *, __const fpos_t *); long ftell(FILE *); size_t fwrite(__const void * , size_t, size_t, FILE * ) __asm("_" "fwrite" "$UNIX2003"); int getc(FILE *); int getchar(void); char *gets(char *); extern __const int sys_nerr; extern __const char *__const sys_errlist[]; void perror(__const char *); int printf(__const char * , ...) ; int putc(int, FILE *); int putchar(int); int puts(__const char *); int remove(__const char *); int rename (__const char *, __const char *); void rewind(FILE *); int scanf(__const char * , ...) ; void setbuf(FILE * , char * ); int setvbuf(FILE * , char * , int, size_t); int sprintf(char * , __const char * , ...) ; int sscanf(__const char * , __const char * , ...) ; FILE *tmpfile(void); char *tmpnam(char *); int ungetc(int, FILE *); int vfprintf(FILE * , __const char * , va_list) ; int vprintf(__const char * , va_list) ; int vsprintf(char * , __const char * , va_list) ; int asprintf(char **, __const char *, ...) ; int vasprintf(char **, __const char *, va_list) ; char *ctermid(char *); char *ctermid_r(char *); FILE *fdopen(int, __const char *); char *fgetln(FILE *, size_t *); int fileno(FILE *); void flockfile(FILE *); __const char *fmtcheck(__const char *, __const char *); int fpurge(FILE *); int fseeko(FILE *, off_t, int); off_t ftello(FILE *); int ftrylockfile(FILE *); void funlockfile(FILE *); int getc_unlocked(FILE *); int getchar_unlocked(void); int getw(FILE *); int pclose(FILE *); FILE *popen(__const char *, __const char *); int putc_unlocked(int, FILE *); int putchar_unlocked(int); int putw(int, FILE *); void setbuffer(FILE *, char *, int); int setlinebuf(FILE *); int snprintf(char * , size_t, __const char * , ...) ; char *tempnam(__const char *, __const char *) __asm("_" "tempnam" "$UNIX2003"); int vfscanf(FILE * , __const char * , va_list) ; int vscanf(__const char * , va_list) ; int vsnprintf(char * , size_t, __const char * , va_list) ; int vsscanf(__const char * , __const char * , va_list) ; FILE *zopen(__const char *, __const char *, int); FILE *funopen(__const void *, int (*)(void *, char *, int), int (*)(void *, __const char *, int), fpos_t (*)(void *, fpos_t, int), int (*)(void *)); int __srget(FILE *); int __svfscanf(FILE *, __const char *, va_list) ; int __swbuf(int, FILE *); int main(int argc, char **argv){int maxlen = 0, spaces = 0, words[2048] = {0}; for(int c = getchar(), len = 0; !feof(__stdinp); c = getchar()) switch(c){ case ' ': case '\t': case '\n': ++spaces; ++words[len]; maxlen = len > maxlen ? len : maxlen; len = 0; break; default: ++len; } fputs("no. of:\nwspace chars: ", __stdoutp); for(int i = 0; i < spaces; ++i) putchar('#'); puts(""); for(int i = 1; i <= maxlen; ++i){ printf("%d-char words: ", i); for(int j = 0; j < words[i]; ++j) putchar('#'); puts(""); } }

Name: Anonymous 2008-12-15 0:14

>>11
This looks like a job for Haskell: The Ironic Hipster Programming Language

It's the thought that counts.

Name: Anonymous 2008-12-15 0:14

>>11
This looks like a job for Haskell: The Ironic Hipster Programming Language

It's the thought that counts.

Name: Anonymous 2008-12-15 4:43

>>12VALID PERL CODE

Name: Anonymous 2008-12-15 18:54

>>11,15
-- $_ . q[
import Data.List
import Text.Printf

main :: IO ()
main = interact
    $ unlines
    . map (\xs@(x : _) -> printf "%2d-char words: %s" x $ length xs `replicate` '#')
    . group
    . sort
    . map length
    . words

{- ], 0 if 0;
use warnings;
use strict;

my @h;
while (<>) {
    ++$h[length] for split;
}
for my $i (0 .. $#h) {
    $h[$i] or next;
    printf "%2d-char words: %s\n", $i, '#' x $h[$i];
}
# -}

Name: Anonymous 2008-12-15 22:21

i lol'd

Name: Anonymous 2008-12-15 23:15

>>16
This is beautiful. I cried. Haskell is the shit.

Name: Anonymous 2008-12-16 0:01


import exercise
exercise.1-13()

Name: Anonymous 2008-12-16 1:04

Q>stdio.in!
Z{$word:= [$_wspace,!$_wspace+~,$_wspace]!(n.`=char words:'.(`#'|(** (in Q (search [$word|(:= .length n)])))))|(** 10 (n))}>stdio.out!

Name: Anonymous 2008-12-16 1:08

>>20 VALID LISP CODE

Name: Anonymous 2008-12-16 1:53

>>20

Q>stdio.in!
Z{
$word:= [$_wspace,!$_wspace+~,$_wspace]!
(
n.`=char words:'
.(`#'|(** (in Q (search [$word|(:= .length n)]))))
)|(** 10 (n))
}>stdio.out!

Name: Anonymous 2008-12-16 4:58


"exercise 1-13" < main
{
   main ->
   stef(;)
   stofn
       skrifastreng(exercise 1-13),
   stofnlok
}
*
[b][i][u][o]"GRUNNUR"[/o][/u][/i][/b]
;

Name: Anonymous 2008-12-16 7:43

[b][i][o][u]! [b][i][o][u] is the STANDARD!

Name: Anonymous 2008-12-16 8:57

>>24
"GRUNNUR" is the Icelandic standard.

Name: Anonymous 2011-08-29 10:30

just
testing
my
indentation
skills

please
ignore
me

Don't change these.
Name: Email:
Entire Thread Thread List