Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] EEPROM equality function

Status
Not open for further replies.

kunal5959

Junior Member level 3
Joined
Jul 26, 2011
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,644
I have a situation where i have to write a code in C for ATMEGA128 in AVR studio, so as to compare 5 integers that are being saved in the EEPROM and then to see which ones are equal and choose atleast 3 equal numbers of the 5 nrs. :-

e.g.

a=0,b=0,c=0,d=2,c=3
then Valid_nr=0,
a=6,b=0,c=6,d=6,c=6
then Valid_nr=6,
a=8,b=8,c=8,d=8,c=8
then Valid_nr=8
Please suggest me some logic because this seems to be very confusing to write a short and efficient code to achieve this..
 

Shouldn't be something like that ?

Code:
int Smalest ( int a, int b , int c , int d  )
{
int Valid_nr = a ;
if ( a >= b )  { Valid_nr= b } ;
if ( b >= c )  { Valid_nr= c } ;
if ( c >= d )  { Valid_nr= d } ;
return = Valid_nr ;
}

( not optimized, not simulated )
 

Two nested for loops will do it in O(n^2) but can work in place, which for 5 values should be easy, but will scale badly if you have 5,000,000 values to search.

With only 5 inputs almost anything will do, but if you had a much bigger search space I would be thinking in terms of sorting the array (in ram) then doing a linear scan, result would be O(n log n) in time and O(n) in space.

Regards, Dan.
 

I do not understand your question. Should the examples be a, b, c, d and 'e'. You show 'c' twice.

In general, the shortest code will be to bin the values in an array. The size of the array has to be at least as big as the largest value in your list. For example if the numbers are 0 - 9, define an array[10] and initialize all it's values to zero. Then for each value you want to check, use array[value]++; When the 5 values have be added to the array it will hold the number of ocurrences of each value. You can then see which has a value of 3 or greater.

Brian.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top