473,232 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,232 software developers and data experts.

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 2389
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chung Leong | last post by:
My contribution to the comp.lang.php FAQ: ------------------------------------------------------------- Q: How do I retrieve a page from a web site? A: Pass a URL to file() or...
5
by: David Rasmussen | last post by:
Some sites seem to be session driven in the sense that if I visit the homepage and do a few clicks I can navigate anywhere I want, but if I paste the current location into a new browser window...
2
by: forums_mp | last post by:
I've got an STL class (see below) with two functions to store and retrieve data - msg structs. The "Store" function when called will copy the received message (depending on which message) into...
6
by: Dimitris Mandalidis | last post by:
Hello all, Suppose we have the following struct : struct foo { int a; int b; char *c; } And :
5
by: Roy Gourgi | last post by:
Hi, I am used to working in Visual FoxPro and I would like to be able to create a database and store and retrieve information from it. What is the simplest way to do it and what should I be...
3
by: Dan Avni | last post by:
I am trying to get information posted to my web page through the request.form but the problem is that the machine that is posting me the information does not include the content-type header in the...
1
by: Sluggoman | last post by:
Cannot seem to wrap my head around this, could someone help me with the logic? I need to pull the value which occurs most frequently from array - am writing a mini program to get logic sorted out,...
0
by: Brian Henry | last post by:
Since no one else knew how to do this I sat here all morning experimenting with this and this is what I came up with... Its an example of how to get a list of items back from a virtual mode list...
3
by: Keith Thompson | last post by:
Victor <vhnguyenn@yahoo.comwrites: You're declaring an array of pointers to unsigned long long, but you're initializing the pointers with integer values. This is actually a constraint...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.