Count the frequency

R

rccf6178

Hi all,

Do anyone know how to write a C program to count the frequency of a
certain key pattern in a given datafile?

Input:
• Three alpha-numeric keys separated by space
• A data file contains M by 3 matrix of alpha-numeric keys. Where M is
number of rows and 3 is number of columns.

Output:
• Number of occurrences of the input keys

Example:

Input file db.txt and its content:

4 7 9
2 1 G
5 3 4
A 3 E
1 A 3
F 5 6
X 0 4


Sample run #1
Please enter 3 keys (separated by space):7 A 4

The key ‘7 ‘ has 1 occurrence in the database
The key ‘A’ has 2 occurrences in the database
The key ‘4’ has 3 occurrences in the database

Sample run #2
Please enter 3 keys (separated by space): A B C

The key ‘A’ has 1 occurrence in the database
The key ‘B’ has zero occurrence in the database
The key ‘C’ has zero occurrence in the database

The following is the hints that I have. However, I do not know how I can
complete the program. Your help will be appreciated.

***************************

#include <stdio.h>
#include <string.h>


int numeric[10];
int alpha[26];


void ReadData(char* input_file)
{
FILE* fptr;
char buffer[128];
char* token;
int index;

fptr = fopen(input_file, "r");

if (fptr != NULL)
{
while (fgets(buffer, sizeof(buffer), fptr) != NULL)
{
token = strtok(buffer, " ");
while ( token != NULL )
{

if (token[0] >= '0' && token[0] <= '9')
{
/* calculate number of input in numeric[] */
}
else if (token[0] >= 'A' && token[0] <= 'Z')
{
/* calculate number of input in alpha[] */
}

/* Get next token: */
token = strtok( NULL, " " );
}
}
}
}


/* get the occurrence of the specified key */
int GetKeyCount(char key)
{
int index;

if (key >= '0' && key <= '9')
{
….. /* return the occurrence of the numeric input key in numeric[] */
}
else if (key >= 'A' && key <= 'Z')
{
….. /* return the occurrence of the alpha input key in alpha[] */
}
else
return 0; /* return 0, if the key is not defined */

}



void main()
{
char enquiry[32]; /* store the enquiry string input by user */
char* token;

ReadData("db.txt"); /* setup the key occurrence database from the input
file */

printf("Please enter 3 keys (separated by space): ");

while (gets(enquiry) != NULL)
{

token = strtok(enquiry, " "); /* get the key one by one from the line
delimited by a space - e.g. "1 2 A" */

while (token != NULL )
{
/* While there are tokens in "string" */

/* print out the result from the key token[0] and the function
GetKeyCount() */
……..


/* Get next token: */
…….
}

printf("Please enter 3 keys (separated by space): ");
}

}
 
D

dandelion

rccf6178 said:
Hi all,

Do anyone know how to write a C program to count the frequency of a
certain key pattern in a given datafile?

Make your own homework...
 
R

Richard Bos

dandelion said:
When your dutch/german/french/latin is as good as mine, it's time to start
correcting me.

Vind ik best. Het is "_Do_ your own homework", du ungeheurer Idiot,
nicht "Make your own homework". Nunc tace.

Richard
 
J

Joona I Palaste

Vind ik best. Het is "_Do_ your own homework", du ungeheurer Idiot,
nicht "Make your own homework". Nunc tace.

Mais Richard, tu as oublie le Français...
 
M

Mark A. Odell

When your dutch/german/french/latin is as good as mine, it's time to
start correcting me.

When I start writing in any of those languages, I will *welcome* your
corrections. How else will I learn?
 
J

Joona I Palaste

When I start writing in any of those languages, I will *welcome* your
corrections. How else will I learn?

And anyway, how good is either of your (can I say that?) Finnish,
Swedish or Hungarian?
 
M

Mark McIntyre


You practically asked for Richard's comment by being too clever, then you
plonk one of the regulars? Idiot.

For what its worth I was posting a similar response to the Bos's when his
came through. Dang, I hate it when some lowlander is faster than me....
:)
 
M

Mac


IMO, Richard Bos is a reasonably valuable contributor to this newsgroup.
It seems that the above (which I don't fully understand) was just a case
of him taking the bait you put out when you said, higher up in the thread:
When your dutch/german/french/latin is as good as mine, it's time to
start correcting me."

I think you would be better off not plonking him.

For your information, there are a number of people in this newsgroup who
are multilingual, and who are not native speakers of English. Corrections
to grammar and spelling are relatively rare. Typically you would see them
only when the meaning of the English is not clear.

Also, "cute" abbreviations seem to periodically draw fire. For example,
chat room stuff like "UR" instead of "you are" or "2" instead of "to"
or "too" seems to make certain regulars in this group very angry.

Just my $0.02.

--Mac
 
D

dandelion

You're doing well. you;ve plonked one of the regulars, and been plonked by
another. Keep going and soon there'l be an echo-o-o-o-o

Ok.

I don't really care *who* calls me an idiot, (s)he get's plonked. Outside
the soc.culture.* hierarchy that is.
 
C

CBFalconer

dandelion said:
Ok.

I don't really care *who* calls me an idiot, (s)he get's plonked.
Outside the soc.culture.* hierarchy that is.

Built in uncontroversial confirmation of Marks observation is
supplied. You are indubitably idiotic.
 
D

dandelion

CBFalconer said:
Built in uncontroversial confirmation of Marks observation is
supplied. You are indubitably idiotic.

Sure, pumpkin... Now go to mommy and have your nose wiped.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top