Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 3rd, 2004, 9:30 AM   #1
s9155773
Newbie
 
Join Date: Nov 2004
Posts: 1
Rep Power: 0 s9155773 is on a distinguished road
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): ");
}

}
s9155773 is offline   Reply With Quote
Old Nov 3rd, 2004, 9:53 AM   #2
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
You should really use the code tags.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Old Nov 3rd, 2004, 10:03 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
T'would be handy. And also, you didn't need to mail me this - I check here every day. Sorry I can't help.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 3rd, 2004, 11:17 PM   #4
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
Why won't it compile?
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Nov 4th, 2004, 9:46 AM   #5
Pizentios
Programming Guru
 
Pizentios's Avatar
 
Join Date: May 2004
Location: Brandon, Manitoba, Canada
Posts: 2,023
Rep Power: 7 Pizentios is on a distinguished road
Send a message via ICQ to Pizentios Send a message via MSN to Pizentios
I have a feeling that this is a homework assignment.
__________________
Profanity is the one language that all programmers understand.

Check out my Blog <---updated Nov 30 2007!
Pizentios is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC