[ create a new paste ] login | about

Link: http://codepad.org/vmwyc6On    [ raw code | fork ]

ninwa - C, pasted on Sep 8:
/* The C Programming Language,
    my soultion to exercise 1-13:

    write a program to print a histogram of the lengths of words in its inputs....

 WELL I TOTALLY READ THAT WRONG. This prints a histogram of the count of each
digit that you entered. */

#include <stdio.h>

main()
{
	int c, i, nwhite, nother, bars, max, maxIndex;
	int ndigit[10];

	nwhite = nother = bars = max = maxIndex = 0;
	for( i = 0; i < 10; ++i )
		ndigit[i] = 0;

	while ((c = getchar()) != '\n')
		if (c >= '0' && c <= '9')
			++ndigit[c-'0'];
		else if (c == ' ' || c == '\n' || c == '\t')
			++nwhite;
		else
			++nother;
	
	/* draw digit-count vertical histogram */
	/* find max value */
	for (i = 0; i < 9; ++i){
		if (ndigit[i] > max){
			maxIndex = i;
			max = ndigit[i];
		}
	}

	for (i = max; i >= 0; i--){
		for (bars=0; bars <= 9; ++bars){
			if (ndigit[bars] >= i)
				printf("#");
			else
				printf(" ");
		}
		printf("\n");
	}
	printf("0123456789");
}


Create a new paste based on this one


Comments: