Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jun 2nd, 2006, 4:35 AM   #1
darthsabbath
Newbie
 
Join Date: Dec 2005
Posts: 15
Rep Power: 0 darthsabbath is on a distinguished road
Code Critique

Hi, all. I don't so much have a problem. My code works (or at least, it does in all tests I've ran so far), but as I'm still working on some of the examples in K&R, I'm just looking for some comments. Criticisms especially. If I've done something dumb, I want to know about it. Any feedback is helpful .

Please note that I'm not using pointers, functions, or anything that has not been introduced yet at this time in the book... trying to play fair.

Anywhoo... here's the assignment, from the K&R text.

Quote:
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.
And here's my program.

/* K&R
 * Chapter 1
 * Exercise 1-13
 * Prints a histogram of word sizes
 */
#include <stdio.h>

#define		MAXSIZE		10		/* Maximum word size */

int main(void)
{
	int arr[MAXSIZE+1];				/* 10 sizes of words, plus a >MAXSIZE field */
	int i, j, nc, c;			/* Loop counters, number of characters, character */
	int highest = 0;					/* Used to get the highest value in arr[] */
	
	/* initialize arr[] to 0*/
	for (i = 0; i <= MAXSIZE; ++i)
		arr[i] = 0;
	
	nc = 0;

	while ((c = getchar()) != EOF) {
		++nc; /* Here we increment with each character until we hit a blank... */
		if (c == ' ' || c == '\n' || c == '\t' || c == EOF){
			--nc; /* ...then drop the blank */
			if (nc <= MAXSIZE) { 
				arr[nc-1]++;
				nc = 0;
			} else if (nc > MAXSIZE) {
				arr[MAXSIZE]++;
				nc = 0;
			}
		}
	}
	
	/* Let's see what the highest number in arr[] is */
	for (i = 0; i <= MAXSIZE; ++i) {
		if (arr[i] > highest)
			highest = arr[i];
	}

	/*Now let's do a vertical histogram */
	while (highest > 0) {
		printf("\t%3d: ", highest);
		for (i = 0; i <= MAXSIZE; ++i) {
			if (highest > arr[i])
				printf("     ");
			else 
				printf("  |  ");
		}
		printf("\n");
		highest--;
	}

	/* And now, the footer */
	printf("\t  --------------------------------------------------------\n\t     ");
	for(i = 0; i <= MAXSIZE; ++i) {
		if (i < MAXSIZE)
			printf("  %d  ", i+1);
		else
			printf(">%d ", MAXSIZE);
	}
	return 0;
}

Thanks,
Phil
darthsabbath is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:26 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC