469,621 Members | 2,244 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

retrieve mode from data array

Hi all,

Am floundering through a course in which C was not a pre-req, but the
assignment is in C - If someone could point out where I am going way
off the rails, I'd apprecciate it. Please be gentle, I know I'm not a C
wizard already, that's why I'm here. The program will generate a guess
based on input from another program I've already written. It won't
always guess the right answer, but it will guess right more often than
any other guess, so what I need to figure out how to do is save the
data into an array, and then count the frequency within the array of
the different guesses made and return the value which occurs most
often (mode). Here's the relevant code of what I have thus far...and
yes, I know it's ugly....

int maxValue(int *);

main ()
{
unsigned char guess3[1000];//array to hold guesses
int tracker3[256] ;//arrays to track frequency of guesses for each
character
int bestguess3;

unsigned char thisguess; //guess made with each iteration of loop -
copied to appropriate guess array, will also increment tracker array
int ivloop3mark; //to mark which spot in the guess array to insert new
guesses

//set loopmark variables to 0
ivloop3mark ;
bestguess3 = 0;

/*initialize and zero guess and tracker arrays*/
for(i=0;i<256;i++)
{
tracker3[i]=0;
guess3[i]=0;
}

/*********************begin main loop ***********/
loop = 0;
while (loop < 600000)
{
//program does a bunch of stuff and will eventually spit out a
variable called "thisguess"
thisguess = 20;

/*put guess in guess* array and increment tracker* array*/

/*create if loop for each guess, increment tracker array */

if(ivloop == 3)
{
guess3[ivloop3mark] = thisguess;// copy guess to guess3 array in
position marked by variable

//update tracker3 for frequency calculation to count frequency - I
THINK this should increment
//guess3 array at location specified by "thisguess" by one -
//hopefully I can then return the value with the highest frequency
later
tracker3[thisguess] = tracker3[thisguess] + 1;

//evaluate tracker 3 for mode - most frequent guess likely the right
one
//I have created a function to evaluate this below, but I am sure I
have done it wrong
bestguess3 = maxval(tracker3);

//increment ivloop3mark for next pass
ivloop3mark ++;
}

//code does a bunch more stuff
loop ++;
}// end endless while loop
}//end main

/*function to determine guess with highest frequency out of tracker
array*/
//function in question, compiles, but with errors
int maxval(int *maxLocator)
{
int i=0, j=0;
int maxValue;
maxValue = &maxLocator;
while (i<256)
{
if (maxValue < &maxLocator)
maxValue=&maxLocator;
maxLocator++;
}
return(maxValue);
} //end maxval function

Any input appreciated,

Thanks

Nov 19 '05 #1
1 2205
Sluggoman wrote:
Hi all,

Am floundering through a course in which C was not a pre-req, but the
assignment is in C - If someone could point out where I am going way
off the rails, I'd apprecciate it. Please be gentle, I know I'm not a C
wizard already, that's why I'm here. The program will generate a guess
based on input from another program I've already written. It won't
always guess the right answer, but it will guess right more often than
any other guess, so what I need to figure out how to do is save the
data into an array, and then count the frequency within the array of
the different guesses made and return the value which occurs most
often (mode). Here's the relevant code of what I have thus far...and
yes, I know it's ugly....
int maxValue(int *);
Yes, this is your prototype... But where's the function? I think you
meant int maxval(int *);


main ()
int main(void)
{
unsigned char guess3[1000];//array to hold guesses
int tracker3[256] ;//arrays to track frequency of guesses for each
character
I hope you use C99.. C89 does not recognize // comments
int bestguess3;

unsigned char thisguess; //guess made with each iteration of loop -
copied to appropriate guess array, will also increment tracker array
int ivloop3mark; //to mark which spot in the guess array to insert new
guesses

//set loopmark variables to 0
ivloop3mark ;
ivloop3mark = 0;
bestguess3 = 0;

/*initialize and zero guess and tracker arrays*/
for(i=0;i<256;i++)
{
tracker3[i]=0;
guess3[i]=0;
}
Where's i declared?

/*********************begin main loop ***********/
loop = 0;
Where's loop declared?
while (loop < 600000)
{
//program does a bunch of stuff and will eventually spit out a
variable called "thisguess"
thisguess = 20;

/*put guess in guess* array and increment tracker* array*/

/*create if loop for each guess, increment tracker array */

if(ivloop == 3)
{
ivloop?
guess3[ivloop3mark] = thisguess;// copy guess to guess3 array in
position marked by variable

//update tracker3 for frequency calculation to count frequency - I
THINK this should increment
//guess3 array at location specified by "thisguess" by one -
//hopefully I can then return the value with the highest frequency
later
tracker3[thisguess] = tracker3[thisguess] + 1;

//evaluate tracker 3 for mode - most frequent guess likely the right
one
//I have created a function to evaluate this below, but I am sure I
have done it wrong
bestguess3 = maxval(tracker3);

//increment ivloop3mark for next pass
ivloop3mark ++;
}

//code does a bunch more stuff
loop ++;
}// end endless while loop
Add return 0;
}//end main

/*function to determine guess with highest frequency out of tracker
array*/
//function in question, compiles, but with errors
int maxval(int *maxLocator)
{
int i=0, j=0;
int maxValue;
maxValue = &maxLocator;
You have completely misunderstood the use of & and * operators...
What you do here is assign the pointer to the pointer to maxLocator to
an int variable, that makes no sense.

You need maxValue = *maxLocator; to assign the VALUE of (and not a
pointer to) maxLocator, which is pointer.
while (i<256)
{
if (maxValue < &maxLocator)
maxValue=&maxLocator;
Same as before...
maxLocator++;
}
return(maxValue);
} //end maxval function

Any input appreciated,

Thanks


Although it is matter of style, you should consider using

for(i=0; i<256; i++)
{
....
....
}

instead of

int i=0;
while(i<256)
{
....
....
i++;
}
When you post, please make sure that your program does not have so many
syntax errors and try to have proper identation.

--
one's freedom stops where others' begin

Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.users.uth.gr
Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Chung Leong | last post: by
5 posts views Thread by David Rasmussen | last post: by
2 posts views Thread by forums_mp | last post: by
6 posts views Thread by Dimitris Mandalidis | last post: by
3 posts views Thread by Dan Avni | last post: by
1 post views Thread by Sluggoman | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.