retrieve mode from data array

Discussion in 'C Programming' started by Sluggoman, Nov 19, 2005.

  1. Sluggoman

    Sluggoman Guest

    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
     
    Sluggoman, Nov 19, 2005
    #1
    1. Advertising

  2. 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=0;
    > guess3=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
     
    Giannis Papadopoulos, Nov 19, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John J Lee
    Replies:
    3
    Views:
    484
    bruno at modulix
    Dec 1, 2005
  2. Edward Loper
    Replies:
    0
    Views:
    469
    Edward Loper
    Aug 7, 2007
  3. John J Lee
    Replies:
    0
    Views:
    522
    John J Lee
    Aug 7, 2007
  4. Edward Loper

    mmm-mode, python-mode and doctest-mode?

    Edward Loper, Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    436
    Edward Loper
    Aug 9, 2007
  5. Neal Becker
    Replies:
    0
    Views:
    381
    Neal Becker
    Aug 9, 2007
Loading...

Share This Page