S
Sluggoman
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;
}
//set static value to increase freq of 42.
data[6] = 3;
printf("data = ");
for (i=0;i<8;i++)
printf("%d ", data);
//now get frequency array
int freq[8];
for (i=0;i<8;i++)
{
freq = 0;
}
printf("\n \n freq = ");
for (i=0;i<8;i++)
printf("%d ", freq);
//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)
{
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?
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;
}
//set static value to increase freq of 42.
data[6] = 3;
printf("data = ");
for (i=0;i<8;i++)
printf("%d ", data);
//now get frequency array
int freq[8];
for (i=0;i<8;i++)
{
freq = 0;
}
printf("\n \n freq = ");
for (i=0;i<8;i++)
printf("%d ", freq);
//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)
{
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?