iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Occurrences can be done with what's called a frequency table. It's a really cool thing, and it's really easy to implement. But, I'm not going to write your program for you, so I'll give you a frequency table that counts the occurrences of single characters and let you figure out how to do it with words using the excellent links you've already been given (otherwise I would hurt Dave's feelings):
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
int main ( void )
{
int freq[CHAR_MAX + 1] = {0};
int i;
char input;
while ( ( input = getchar() ) != EOF )
++freq[input];
for ( i = 0; i < CHAR_MAX + 1; i++ ) {
if ( freq[i] != 0 ) {
if ( isalpha ( i ) )
printf ( "%c:\t%d\n", i, freq[i] );
else
printf ( "%#X:\t%d\n", i, freq[i] );
}
}
getchar();
return 0;
}
I talk about frequency tables a lot because they're so useful, you might even be able to find an exact solution to your problem by searching the forum. ;)
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>madam thanks for the things
Fixed quote ;)
>but we arent allowed to use them.
How else does your professor expect you to keep track of the number of times a word is present in a sentance, then? I'll let Narue answer this one...
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
>thanks for the things about frequency tables but we arent allowed to use them.
What are you allowed to use if you can't cache the words and counts? I can think of one way (that could be considered a beginner solution) off the top of my head, and the logic is extremely painful even for experienced programmers.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
> thanks for the things about frequency tables but we arent allowed to use them.
Which is why it is pointless to ask people to do your homework.
Not only would we have to vaguely guess at what you really wanted, we also have to guess at how much to dumb it down to the point where you might have plausibly written it. This just isn't going to happen.
> Our lectures are very fast and not beneficial to those with non-C backgrounds
Perhaps you're on the wrong course then. You (as a group of non-C people) need to go talk to the lecturers to voice your concerns.
If the course material said "assumes familiarity with C", then your bluff has been called. Quit now and get onto another course before it's too late to catch up with either of them.
This is going to get a lot harder very quickly, so you need to look at the long term problem as well. Us providing you with one ready made answer is not going to make you an instant C expert. All it will do is keep you on the course for another week until the next (and even more unobtainable) assignment is handed out.
Salem
Posting Sage
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Hello I was thinking about the sorting part. Like I said I don't have my compiler so this is what I rolled out from the top of my head, I'm not sure if it works?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NELEMS 5
//could be done dynamically with malloc perhaps?
struct crap
{
char word[1024];
int wordOccurrence;
};
static int wordOccurrenceSort(const void *, const void *);
int main()
{
size_t i;
static struct crap stuff[NELEMS] =
{
{"card",1},
{"Hello",5},
{"apple",1},
{"country",1},
{"world",2}
};
qsort(stuff, NELEMS, sizeof stuff[0],wordOccurrenceSort);
puts("Sort by word occurence:\n");
for (i = 0; i < NELEMS; ++i )
{
printf("\n%-20s %2d",stuff[i].word, stuff[i].wordOccurrence);
}
getchar();
return 0;
}
static int wordOccurrenceSort(const void *p1, const void *p2)
{
struct crap *sp1 = (struct crap *) p1;
struct crap *sp2 = (struct crap *) p2;
if (sp2->wordOccurrence > sp1->wordOccurrence )
{
return 1; //not sure what value to assign?
}
if (sp2->wordOccurrence < sp1->wordOccurrence )
{
return 0;//not sure what value to assign?
}
if ( sp2->wordOccurrence == sp1->wordOccurrence)
{
int order = strcmp(sp1->word,sp2->word);
return order;
//not sure what value to assign?
}
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
static int wordOccurrenceSort(const void *p1, const void *p2)
{
struct crap *sp1 = (struct crap *) p1;
struct crap *sp2 = (struct crap *) p2;
if (sp2->wordOccurrence > sp1->wordOccurrence )
{
return 1; //not sure what value to assign?
}
if (sp2->wordOccurrence < sp1->wordOccurrence )
{
return 0;//not sure what value to assign?
}
if ( sp2->wordOccurrence == sp1->wordOccurrence)
{
int order = strcmp(sp1->word,sp2->word);
return order;
//not sure what value to assign?
}
}
You could simply have done something like this since this function is required to return how one element is placed wrt another and not any absolute value. Only the sign matters here (+ve, -ve, zero).
static int wordOccurrenceSort(const void *p1, const void *p2)
{
struct crap *sp1 = (struct crap *) p1;
struct crap *sp2 = (struct crap *) p2;
return (sp1->wordOccurrence - sp2->wordOccurrence) ;
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
You could simply have done something like this since this function is required to return how one element is placed wrt another and not any absolute value. Only the sign matters here (+ve, -ve, zero).
static int wordOccurrenceSort(const void *p1, const void *p2)
{
struct crap *sp1 = (struct crap *) p1;
struct crap *sp2 = (struct crap *) p2;
return (sp1->wordOccurrence - sp2->wordOccurrence) ;
}
Maybe, like I said I don't have my compiler to test it. I wrote that on the fly.
How does your version sort the words of same occurrence into alphabetical order though. I can't see it?We were required to write a code for counting frequency of words from stdin and then listing them in descending order with those with equal length being alphabetized
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
How does your version sort the words of same occurrence into alphabetical order though. I can't see it?
Oh, I guess I overlooked that requirement. Here is the fix:
static int wordOccurrenceSort(const void *p1, const void *p2)
{
struct crap *sp1 = (struct crap *) p1;
struct crap *sp2 = (struct crap *) p2;
if (sp1->wordOccurrence == sp2->wordOccurrence)
return strcmp (sp1->word, sp2->word) ;
else
return (sp1->wordOccurrence - sp2->wordOccurrence) ;
}
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Don't forget he/she wants it in descending order?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Well, doing that would be really simple. Posting the entire solution would be like wasting the efforts Narue and the others have put in solving the OP's problem.
I would leave the OP to figure out this one for himself.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
>Well, doing that would be really simple.
No I wasn't asking, I was correcting. :cheesy:
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
No I wasn't asking, I was correcting. :cheesy:
;)
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
>What I meant with the things not being allowed is that the output wanted was to be straightforward.
The output has no relationship to how you produce the answer. You can solve the problem a dozen wildly different ways and get exactly the same desired output that you posted.
>Only the sign matters here (+ve, -ve, zero).
>return (sp1->wordOccurrence - sp2->wordOccurrence) ;
Sure, only the sign matters, but it's also the sign that kills you with this code. You're a smart guy, so I'll give you three words of advice on why this is a poor solution: signed integer overflow.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>You're a smart guy, so I'll give you three words of advice on why this is a poor solution: signed integer overflow.
He he, does that mean my code is safer? To be honest I don't know? This is just guess work - but it would be nice to know why. How long are you on maternity leave for?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439