454,507 Members | 1,758 Online Need help? Post your question and get tips & solutions from a community of 454,507 IT Pros & Developers. It's quick & easy.

# mode from array

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