S
Sluggoman
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=0;
guess3=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
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=0;
guess3=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