473,326 Members | 2,023 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,326 software developers and data experts.

mode from array

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, but it just
doesn't get there.

int main(void)
{
int i;
int data[8];
//create static array to hold data
for (i=0;i<8;i++)
{
data[i] = i;
}
//set static value to increase freq of 42.
data[6] = 3;
printf("data = ");

for (i=0;i<8;i++)
printf("%d ", data[i]);
//now get frequency array
int freq[8];
for (i=0;i<8;i++)
{
freq[i] = 0;
}
printf("\n \n freq = ");

for (i=0;i<8;i++)
printf("%d ", freq[i]);

//now count frequency...
int freqcount = 0;//will hold frequencies
int oldfreq=0;
int nownum = 0;
int newoldfreq=0;
for(nownum=0;nownum<8;nownum++)
{
for(i=0;i<8;i++)
{
if(nownum==data[i])
{
freqcount ++;
if(freqcount > oldfreq)
{
nownum = newoldfreq;
}
newoldfreq=oldfreq;
}
}
}


printf("\n mode = %d \n", oldfreq);
return 0;
}

Output;
data = 0 1 2 3 4 5 3 7

freq = 0 0 0 0 0 0 0 0
mode = 0

obviously the mode should 3 - but my logic is horrible - any
suggestions on how to get this value?

Nov 20 '05 #1
1 2185
Sluggoman said:
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, but it just
doesn't get there.


<code snipped>

Take a look at the following code, which does what you want. I've taken the
unusual step of commenting (almost) every line, in the hope that it helps
you clear your head a little. :-)

MINVAL and MAXVAL can be tweaked to taste, within reason, if you wish.
#include <stdio.h> /* prototype for printf, and def of size_t */

#define MINVAL 0 /* the lowest allowable data value */
#define MAXVAL 42 /* the highest allowable data value */

int main(void)
{
int data[] = { 0, 1, 2, 3, 4, 5, 3, 7 }; /* initialise the data array */
int freq[MAXVAL + 1 - MINVAL] = { 0 }; /* clear the frequency
* counts to 0.
*/
size_t i = 0; /* loop counter */
int mode = 0; /* for recording the modal value */

for(i = 0; i < sizeof data / sizeof data[0]; i++) /* for each datum */
{
++freq[data[i]]; /* track the frequency */
if(freq[data[i]] > mode) /* is this now the most common item? */
{
mode = data[i]; /* yes, so log it */
}
}
printf("The modal value is %d which occurs %d time%s.\n",
mode,
freq[mode],
freq[mode] == 1 ? "" : "s"); /* 1 time, 2 time>>s<< */

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dimitris Mandalidis | last post by:
Hello all, Suppose we have the following struct : struct foo { int a; int b; char *c; } And :
3
by: Nadav | last post by:
Hi, I am writing a mixed mode application, I have a mixed mode Assembly manipulating a managed byte array, to access this array in unmanaged code I '__pin' the array, As I understand, pining an...
1
by: Sluggoman | last post by:
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...
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...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
6
by: Megantsu | last post by:
What is the easiest way to program a way to figure mode? I am just having to do it with some simple Int.... Any suggestions?
4
by: tzuchien.chiu | last post by:
I've searched this new group with the keyword: "kernel library array list". I am looking for an open source C++ class library for kernel mode programming, because the C++ standard library and...
2
by: k1ckthem1dget | last post by:
I dont know how to find the mode of an array. I am to do the following. (1) Given the following: · A list of 11 integers; 1,2,3,3,3,2,2,1,3,4,5 (2) The program should:
1
by: shgu0501 | last post by:
hi I'm supposed to do a frequency array and find mode,median and count, but without storing the actual data in an array. I am able to build a frequency array but I can't return the mode since I am...
1
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.