🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: FILE READ PROBLEM

#651
From: Michael Smith
Date: Sun, 16 Nov 1997 00:00
78 lines
2782 bytes
GREG HOOPER wrote:
> 
> Below is a program that achieves the correct end result that I'm after. The
> problem is
> I'm only allowed to read the text file once.
> Can anyone show me how to achieve this to give the same output that I have.
> 
> Any help would be appreciated Thanks  GREG   :-(
> 
> /* PURPOSE OF PROGRAM */
> /*
> Read contents of a text file once via re-direction
> 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.
> */

Here is some code to count the chars.  You can use your own bar graph
code.

This is not optimised (i.e. it's slow) but it is fairly simple.  You can
optimise it yourself if speed is important.

#include <stdio.h>

#define MAXCHAR 5

int get_index( char ch, char *arr, int narr );
#define in_array(ch,arr,narr) (get_index(ch,arr,narr)>=0)

int main( void )
{
    char chars[MAXCHAR];
    int  count[MAXCHAR];
    int  nchar, i, ch, ndx;

    nchar = 0;
    for (i=0; i<MAXCHAR; i++) count[i] = 0;

    while ( (ch=getchar()) != EOF ) {
        if (nchar<MAXCHAR) {
            if (!in_array((char)ch,chars,nchar)) {
                chars[nchar++] = (char)ch;
            }
        }
        ndx = get_index( (char)ch, chars, nchar );
        if (ndx>=0) count[ndx]++;
    }

    printf( "\n\n" );
    for (i=0; i<nchar; i++) {
        printf( "%3d    %c%12d\n", i+1, chars[i], count[i] );
    }
    return 0;
}

int get_index( char ch, char *arr, int narr )
{
    int i;
    for (i=0; i<narr; i++) {
        if (arr[i]==ch) return i;
    }
    return -1;
}




-- 
########################################################################
Michael Smith                                        emmenjay@zip.com.au
Emmenjay Consulting Pty Ltd             http://www.zip.com.au/~emmenjay/

Computers *ARE* user-friendly.  You just need to be properly introduced.
-- 
comp.lang.c.moderated - clcm@plethora.net
Technical Details
Message-ID: <clcm-19971116-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: Michael Smith <emmenjay@zip.com.au>
Subject: Re: FILE READ PROBLEM
Date: 1997/11/16
Message-ID: <clcm-19971116-0002@plethora.net>
X-Deja-AN: 289941332
References: <clcm-19971114-0010@plethora.net>
Organization: Emmenjay Consulting
NNTP-Posting-Date: 16 Nov 1997 12:04:49 CST
Reply-To: emmenjay@zip.com.au
Newsgroups: alt.comp.lang.learn.c-c++,comp.lang.c.moderated,ctdl.lang.c
Originator: clcm@plethora.net (comp.lang.c.moderated)