Well how i would just keep a count of the data points?
Well how i would just keep a count of the data points?
Every time you see a number, you add one in the appropriate place.
So have it only store integers into an array if it has entered before? And then after they stop the loop(enter -number), have it check the array somehow for duplicates then determine which one has the most?
So you're going to start with a big pile of numbers, all of which are 0. When a number, call it number, is read in, you're going to add one to that particular number in your big pile: frequency[number]++. At the end of the day, whichever number(s) have frequency[number] the largest, win.
Example:
You can just use that function to build a list of arbitrary numbers.Code:struct node { int value; int frequency; struct node *next; }; struct node *insert(struct node *p, int value) { struct node *tmp = p, *head = p; if(!p) { p = malloc(sizeof(*p)); if(p) { p->value = value; p->frequency = 1; p->next = 0; } return p; } /* From here on out, p represents the previous node in the tree */ for(p = 0; tmp; tmp = tmp->next) { if(tmp->value < value) { tmp->next = p; p = malloc(sizeof(*p)); if(p) { p->value = value; p->frequency = 1; p->next = tmp; return head; } } p = tmp; } return 0; }
i appreciate your help but i dont get what that code does, i cant compile it either.
Well its not stand-alone. It is a linked list.
Example:
Code:#include <FPT.h> struct node { double value; int frequency; struct node *next; }; struct node *insert(struct node *p, double value) { struct node *tmp = p, *head = p; if(!p) { p = malloc(sizeof(*p)); if(p) { p->value = value; p->frequency = 1; p->next = 0; } return p; } /* From here on out, p represents the previous node in the tree */ for(p = 0; tmp; tmp = tmp->next) { if(tmp->value == value) { ++tmp->frequency; return head; } else if(tmp->value < value) { tmp->next = p; p = malloc(sizeof(*p)); if(p) { p->value = value; p->frequency = 1; p->next = tmp; return head; } } p = tmp; } return 0; } void clean(struct node *p) { if(p) { clean(p->next); free(p); } } void outN(struct node *p) { if(p) { printf("%g\t%d", p->value, p->frequency); outN(p->next); } } int main() { double i; struct node *p = 0; outS("Please input a postive integer..."); while((i=inD())>=0){ p = insert(p, i); } outN(p); clean(p); }
Last edited by master5001; 10-06-2008 at 07:49 PM.
Simple arrays would do. Unless the assignment asked for linked lists.
*mopes* I just figured since he was using floating points it would make more sense to do it with linked lists. But yeah, I suppose you could use arrays... if this is some sort of contest to use the least amount of lines.
This ain't COBOL (where you're paid by the line. LOL).
haha I will keep that in mind next time. Since I have come to know Lucid's programming level reasonably well, I think its safe to say he would never ethically use code he doesn't comprehend and this is above where he is at in his learning.
So, although i wont be turning in the code listed, i had to try it out. Seems to work good except for when there are two values that appear the same number of times.
Is there anyway to tweak this program to accept two "modes" im gonna try to learn from this program and write my own.
Did you get my code before or after I corrected a bug in it? It should be working ok now.
The most recent one, if i enter 3,3,2,2 it shows 2...2. When it should show 3,2. Meaning the mode is 3 and 2