retrieve mode from data array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sluggoman

    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=&maxLo cator;
    maxLocator++;
    }
    return(maxValue );
    } //end maxval function



    Any input appreciated,

    Thanks

  • Giannis Papadopoulos

    #2
    Re: retrieve mode from data array

    Sluggoman wrote:[color=blue]
    > 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....
    >[/color]
    [color=blue]
    > int maxValue(int *);[/color]

    Yes, this is your prototype... But where's the function? I think you
    meant int maxval(int *);

    [color=blue]
    >
    > main ()[/color]

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

    I hope you use C99.. C89 does not recognize // comments
    [color=blue]
    > 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 ;[/color]

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

    Where's i declared?
    [color=blue]
    >
    > /*************** ******begin main loop ***********/
    > loop = 0;[/color]

    Where's loop declared?
    [color=blue]
    > 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)
    > {[/color]

    ivloop?
    [color=blue]
    > 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[/color]

    Add return 0;
    [color=blue]
    > }//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;[/color]

    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.
    [color=blue]
    > while (i<256)
    > {
    > if (maxValue < &maxLocator)
    > maxValue=&maxLo cator;[/color]

    Same as before...
    [color=blue]
    > maxLocator++;
    > }
    > return(maxValue );
    > } //end maxval function
    >
    >
    >
    > Any input appreciated,
    >
    > Thanks
    >[/color]

    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

    Comment

    Working...