Hi all... First post here, be gentle. I'm trying to get this to work, and there's no output. This is from the K&R book [v2] exercise 1-14, the prompt is in the header. I added comments for readability, and please comment on anything you believe needs improvement. Thank you for reading.

/*
 * EX114.c
 *      this will create a histogram of the frequency of different characters
 *      in its input.
 *
 *      what I'm trying to do: 2 arrays, 1 with the character and 1 with the #
 *      of time that character shows up...
 */

#include <stdio.h>

#define CHARCOUNT 32
// for the sake of simplicity, CHARCOUNT represents the # of unique characters
main(){
	int c, nc, chcount[CHARCOUNT], lcv;
	// chcount is the # of times a unique character shows up...
	// so eg if I had 'a' show up 5 times then
	// 'a' would be in charray[4], and chcount[4] would show 5.
	// nc is the unique character count
	nc = 0;

	char charray[CHARCOUNT];

	while ((c = getchar()) != EOF){
		// so this loop checks to see if the current character is in the array
		int inarray = 0;
		for(lcv = 0; lcv < CHARCOUNT && inarray == 0; lcv++){
			if(c == charray[lcv]){
				chcount[lcv] = chcount[lcv]++;
				inarray++;
			}
			// if the character is in the array then we increment the count of that character.
			// we also increment inarray so the loop quits.
		}
		// so if this test passes, that means the character was not in the array.
		if(lcv != CHARCOUNT){
			// add the current char to the array.
			charray[nc] = c;
                        chcount[nc] = 1;
			// increment the # of unique characters.
			nc++;
		}
	}
	// this part is for drawing the histogram.
	for(lcv = 0; lcv < nc; lcv++){
		putchar(charray[lcv]);
		int i = chcount[lcv];
		while(i > 0){
			printf("*");
			i--;
		}
		printf("\n");
	}
}

Now looking at this code I realize the part for drawing the histogram should probably be in the loop...

Edited 6 Years Ago by kelevra: n/a

Part of the problem is you want lcv == CHARCOUNT on line 36 -- you've traversed the entire array to the end and the character isn't there. Ignore what I said in my other edit about EOF, but make sure your user knows to hit EOF (ctrl-Z or F6) at the end of the input.

(sorry about all the edits, I had a train of thought but got derailed -- I thought I knew exactly what was wrong with it right off the bat but it needed a closer study)

Edited 6 Years Ago by jonsca: n/a

Line 29 should be chcount[lcv]++; instead of chcount[lcv] = chcount[lcv]++; which can cause weird results.

Ive gotten it so it works after hitting enter then EOF but it counts the enter as a character.

Hi, thanks for the response. Didn't realize I was testing for the wrong thing on line 36, so it works now. And kudos for the chcount[lcv]++, I wasn't sure if this was valid or not.

I added checks so that spaces or the "enter" char would not be added to the array. Below is my final code.

#include <stdio.h>

#define CHARCOUNT 32
// for the sake of simplicity, CHARCOUNT represents the # of unique characters
main(){
	int c, nc, chcount[CHARCOUNT], lcv;
	// chcount is the # of times a unique character shows up...
	// so eg if I had 'a' show up 5 times then
	// 'a' would be in charray[4], and chcount[4] would show 5.
	// nc is the unique character count
	nc = 0;

	char charray[CHARCOUNT];

	while ((c = getchar()) != EOF){
		int inarray = 0;
		if(c == ' ' || c == '\n')
			inarray = 1;
		// so this loop checks to see if the current character is in the array
		for(lcv = 0; lcv < CHARCOUNT && inarray == 0; lcv++){
			if(c == charray[lcv]){
				chcount[lcv]++;
				inarray++;
			}
			// if the character is in the array then we increment the count of that character.
			// we also increment inarray so the loop quits.
		}
		// so if this test passes, that means the character was not in the array.
		if(lcv == CHARCOUNT){
			// add the current char to the array.
			charray[nc] = c;
			chcount[nc] = 1;
			// increment the # of unique characters.
			nc++;
		}
	}
	// this part is for drawing the histogram.
	for(lcv = 0; lcv < nc; lcv++){
		putchar(charray[lcv]);
		printf(" ");
		int i = chcount[lcv];
		while(i > 0){
			printf("*");
			i--;
		}
		printf("\n");
	}
}

Not exactly pretty, but it works. Thanks again.

You're very welcome :) Look forward to strings so you can avoid much of this "stuff."

I should probably put this in my own post (slaps my own wrists with a ruler), but does anyone out there in TV Land know the way to handle the while ((c = getchar()) != EOF) construct (where c is an int) without the special provisions? With the OPs code I always had to hit enter, put in the ^Z and hit enter again. I did search the net high and low but got mainly the information about getchar() returning an int, EOF being negative, etc.

For my penance I will dust off my copy of Kelley/Pohl from the 90's.

Edited 6 Years Ago by jonsca: n/a

This question has already been answered. Start a new discussion instead.