🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #648

Re: FILE READ PROBLEM

#648
From: Miguel Carrasquer Vidal
Date: Sat, 15 Nov 1997 00:00
79 lines
2758 bytes
On 15 Nov 1997 05:08:09 GMT, "GREG HOOPER" <upgrade@b022.aone.net.au>
wrote:

>/* PURPOSE OF PROGRAM */
>/*
>Read contents of a text file once via re-direction

"Via re-direction" meaning "read from stdin" I suppose?

>character by character, process the characters
>read and display a simple horizontal bar graph
>that depicts the frequency of the first five
>characters found in the text file.

What about case?  Is that the first five (no check for duplicates), or
the first unique five?

>*/
>
>/* LIBRARY */
>#include <stdio.h>
>
>/* FUNCTION CALLS */
>void NumOfChars(char[], int, int[]);
>void CharPrint(char[], int[]);
>void AstPrint(int[], int);
>void pause(void);
>void main()

Mind your language, that's: "int main(void)".

>	{
>	int NumArray[] = {0,0,0,0,0}, get_char, i = 0;
>	char CharArray[] = {0,0,0,0,0};
>
>	do
>		{
>		get_char = getchar();
>
>		if ((get_char > 64 && get_char < 91) || (get_char > 96 && get_char <
>123))

If you had #included <ctype.h> you might have used isalpha().
<ctype.h> also provides the handy toupper(), tolower(), isupper(),
islower() functions (c.q. macros).

>			{
>				CharArray[i] = get_char;
>				i++;

Oops!  CharArray[] only has space for five!

>			}
>		}while((CharArray[i] != EOF));
>
>	 while(get_char != EOF)
 etc.

Think again.  You can do this in a single pass.

Read a character.  Is it already in the array of the five "first"
characters?  [How do you know a place is "taken"?]  If so, increment
the count for that character, if not: do we already have five
characters stored in the "first-five" array?  If so, just ignore this
character, if not, store it in the array and give it a score of 1.
Repeat until EOF.

Now translate that to C.


==
Miguel Carrasquer Vidal                     ~ ~
Amsterdam                   _____________  ~ ~
mcv@pi.net                 |_____________|||

========================== Ce .sig n'est pas une .cig
-- 
comp.lang.c.moderated - clcm@plethora.net
Technical Details
Message-ID: <clcm-19971115-0002@plethora.net>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <clcm-19971114-0010@plethora.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fab7a,75d4be5da465827f
X-Google-Attributes: gidfab7a,public
X-Google-Thread: 114592,75d4be5da465827f
X-Google-Attributes: gid114592,public
X-Google-Thread: 100850,75d4be5da465827f
X-Google-Attributes: gid100850,public
From: mcv@pi.net (Miguel Carrasquer Vidal)
Subject: Re: FILE READ PROBLEM
Date: 1997/11/15
Message-ID: <clcm-19971115-0002@plethora.net>
X-Deja-AN: 289750537
References: <clcm-19971114-0010@plethora.net>
Organization: World Access/Planet Internet
NNTP-Posting-Date: 15 Nov 1997 11:38:59 CST
Reply-To: mcv@pi.net
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c.moderated,ctdl.lang.c
Originator: clcm@plethora.net (comp.lang.c.moderated)