473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Count the frequency

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): ");
}

}
Nov 14 '05 #1
21 2472

"rccf6178" <rc******@yahoo.com.hk> wrote in message
news:7a******************************@localhost.ta lkaboutprogramming.com...
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...
Nov 14 '05 #2
"dandelion" <da*******@meadow.net> wrote in
news:41***********************@dreader9.news.xs4al l.nl:
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...

^^^^
Do

--
- Mark ->
--
Nov 14 '05 #3

"Mark A. Odell" <od*******@hotmail.com> wrote in message
news:Xn********************************@130.133.1. 4...
"dandelion" <da*******@meadow.net> wrote in
news:41***********************@dreader9.news.xs4al l.nl:
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...

^^^^
Do


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

Nov 14 '05 #4
"dandelion" <da*******@meadow.net> wrote:
"Mark A. Odell" <od*******@hotmail.com> wrote in message
news:Xn********************************@130.133.1. 4...
"dandelion" <da*******@meadow.net> wrote in
news:41***********************@dreader9.news.xs4al l.nl:
Make your own homework...

^^^^
Do


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
Nov 14 '05 #5
Richard Bos <rl*@hoekstra-uitgeverij.nl> scribbled the following:
"dandelion" <da*******@meadow.net> wrote:
"Mark A. Odell" <od*******@hotmail.com> wrote in message
news:Xn********************************@130.133.1. 4...
> "dandelion" <da*******@meadow.net> wrote in
> news:41***********************@dreader9.news.xs4al l.nl:
>
> > Make your own homework...
> ^^^^
> Do
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.


Mais Richard, tu as oublie le Franηais...

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"The question of copying music from the Internet is like a two-barreled sword."
- Finnish rap artist Ezkimo
Nov 14 '05 #6

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
Vind ik best. Het is "_Do_ your own homework", du ungeheurer Idiot,
nicht "Make your own homework". Nunc tace.


<plonk>
Nov 14 '05 #7
dandelion <da*******@meadow.net> scribbled the following:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
Vind ik best. Het is "_Do_ your own homework", du ungeheurer Idiot,
nicht "Make your own homework". Nunc tace.
<plonk>


<plonk>

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A computer program does what you tell it to do, not what you want it to do."
- Anon
Nov 14 '05 #8

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cm**********@oravannahka.helsinki.fi...

<shrug>
Nov 14 '05 #9
"dandelion" <da*******@meadow.net> wrote in
news:41***********************@dreader14.news.xs4a ll.nl:
>> 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...

^^^^
Do


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?

--
- Mark ->
--
Nov 14 '05 #10
Mark A. Odell <od*******@hotmail.com> scribbled the following:
"dandelion" <da*******@meadow.net> wrote in
news:41***********************@dreader14.news.xs4a ll.nl:
>> 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...
^^^^
Do
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?


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
- Dr. Michael Hfuhruhurr
Nov 14 '05 #11

"Mark A. Odell" <od*******@hotmail.com> wrote in message
news:Xn********************************@130.133.1. 4...
"dandelion" <da*******@meadow.net> wrote in
news:41***********************@dreader14.news.xs4a ll.nl:
>> 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...
^^^^
Do


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?


Noted.
Nov 14 '05 #12
On Fri, 5 Nov 2004 12:48:16 +0100, in comp.lang.c , "dandelion"
<da*******@meadow.net> wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
Vind ik best. Het is "_Do_ your own homework", du ungeheurer Idiot,
nicht "Make your own homework". Nunc tace.


<plonk>


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....
:-)

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #13
On Fri, 5 Nov 2004 13:05:38 +0100, in comp.lang.c , "dandelion"
<da*******@meadow.net> wrote:

"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:cm**********@oravannahka.helsinki.fi...

<shrug>


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
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #14
On 5 Nov 2004 13:16:56 GMT, in comp.lang.c , Joona I Palaste
<pa*****@cc.helsinki.fi> wrote:
And anyway, how good is either of your (can I say that?)
Yup, perfect english.
Finnish, Swedish or Hungarian?


Same language aren't they?
(gd&r)

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #15
Mac
On Fri, 05 Nov 2004 13:16:56 +0000, Joona I Palaste wrote:

[snip]
And anyway, how good is either of your (can I say that?) [...]


Yes, you can say that.

--Mac

Nov 14 '05 #16
Mac
On Fri, 05 Nov 2004 12:48:16 +0100, dandelion wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
Vind ik best. Het is "_Do_ your own homework", du ungeheurer Idiot,
nicht "Make your own homework". Nunc tace.
<plonk>


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

Nov 14 '05 #17

"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:65********************************@4ax.com...
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.
Nov 14 '05 #18
dandelion wrote:
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
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.


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

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nov 14 '05 #19

"CBFalconer" <cb********@yahoo.com> wrote in message
news:41***************@yahoo.com...
dandelion wrote:
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
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.


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


Sure, pumpkin... Now go to mommy and have your nose wiped.
Nov 14 '05 #20


dandelion wrote:
"CBFalconer" <cb********@yahoo.com> wrote in message
news:41***************@yahoo.com...
dandelion wrote:
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
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.


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


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


*shrug* ... and that from someone who does not have Ancient Greek,
Russian, Chinese and Arabic in his language list and forgets (?)
to mention C...

<plonk>
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #21
On Mon, 8 Nov 2004 10:42:20 +0100, in comp.lang.c , "dandelion"
<da*******@meadow.net> wrote:

"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:65********************************@4ax.com.. .
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.


Alternatively, you could simply stop behaving like an idiot....

*plonk*
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Johan Hahn | last post by:
Wouldn't it be nice if list.count, called without any arguments, returned a dict with the list's unique items as keys and their frequency of occurance as values? >>> .count() {'a': 1, 1: 2, 2:...
7
by: Dung Ping | last post by:
Such as: <script> //code aaaa zzzz ccc
10
by: jwvai316 | last post by:
I'm having problem in this case. I need to count how many time each word appears in one sentence. and I need it to be case insensitive for example when the input is The quick brown fox jumps...
2
by: Byomokesh | last post by:
Dear All, I have one xml file. I want count cell and divide percentage in between cell (through XSL). Please any one help me. XML File ------------- <table border="0" cellspacing="0"...
1
by: Rahul | last post by:
Hi, I tried many option, but did not found solution. Same Validation Error showing. If I have convert one table, it can easily converted. But If I have paste another table in same file, then...
0
by: mpilman | last post by:
The following oracle query purports to output a word count for all words found in a table column. Is this possible in MYSQL? Mike =================================================== Measuring...
4
by: writeanand | last post by:
How can I count the frequency of words in a ASCII File using STL? I dont know what words will be found in the file ahead of time. I dont want to use any classes, just a simple program wd do. ...
5
by: not_a_commie | last post by:
So I have a motherboard with multiple CPU sockets. It seems that if I create a StopWatch on one thread and then call the Elapsed member from a different thread that sometimes I get a tick count...
3
by: waynejr25 | last post by:
can anyone debug my program and get it to run. #include <fstream> #include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std;
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.