By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

Count the frequency

P: n/a
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
Share this Question
Share on Google+
21 Replies


P: n/a

"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

P: n/a
"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

P: n/a

"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

P: n/a
"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

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

<shrug>
Nov 14 '05 #9

P: n/a
"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.