473,397 Members | 1,949 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,397 software developers and data experts.

Look-up tables: finding index of a value below a given number

Hi guys, I'm a C newbie (a couple of months ago I was a virgin!) working with PIC microcontrollers. I need to do a look-up table to change one value to another, probably best to give an example in pseudocode:

Expand|Select|Wrap|Line Numbers
  1. code const integer reading[5] ={12, 96, 147, 245, 747}; // sorted numerically
  2. code const integer result[5] ={5, 10, 15, 20, 25}; // sorted numerically
  3. get look-up value for reading[] (for example 120)
  4. find last value in reading[] below this (for example 96)
  5. return equivalent value from result[] (for example 10)
The last part I can deal with, and may not even be required (as you can see, the values in result[] are at intervals of 5, at least for the moment).

The tables will in reality have up to 100 entries. My plan was to iterate through reading[] and find when the number is bigger, then step back one. Is this OK, or is there a better way? Should I consider hash tables?
Dec 30 '09 #1
4 1941
newb16
687 512MB
If values in reading[] are sorted, use binary search.
Dec 30 '09 #2
Binary search, you mean keep splitting the array? Can you point me to some code to do this?
Dec 30 '09 #3
Expand|Select|Wrap|Line Numbers
  1.          int binarySearch( int [] reading , int target )
  2.          {
  3.                 int found = 0 ;
  4.             int start=0 , mid =-1 , end = 100 ; 
  5.                 while( !found &&  end >= start )
  6.                   {
  7.                     mid = (start+end) / 2 ;
  8.                     if(reading[mid] == target)
  9.                         found = 1 ;
  10.                     else if (reading[mid] > target) 
  11.                          end = mid - 1 ;
  12.                     else
  13.                         start = mid + 1 ;
  14.                  }
  15.                         return mid - 1;
  16.  }
  17.  
The index you are looking is mid-1.
Dec 30 '09 #4
Many thanks, I'll give it a go.

What a helpful forum this is.
Dec 30 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: inhahe | last post by:
Hi i'm a newbie at this and probably always will be, so don't be surprised if I don't know what i'm talking about. but I don't understand why regex look-behinds (and look-aheads) have to be...
4
by: Videos 4 Sale | last post by:
Would you know how to change the look of a mouse pointer on a web page. On the page I have the mouse pointer is exactly as I have at work.... white. But what I want to do is change the look of it...
6
by: MLH | last post by:
You know how the text in the attached label sort of 'greys out' when you click something that sets MyTextbox.Enabled = False? I would like to make a label's text look like that? How do I do it?...
1
by: Shell | last post by:
Hi, I have an application written in ASP.NET 2.0, I would like it's GUI especially the webpart to look like windows xp like transparent look or 3D look. I have looked at option of Borderstyle...
5
by: K. Naga Kiran Kumar | last post by:
I am working on a VB.NET project The main requirement of this project is to get XP look and feel to the application. All controls should look like XP controls. The form control also. I am able...
20
by: Maurice | last post by:
Hi all, I have an application, designed in Visual Studio 2005, which will run mainly on Windows XP computers. If I run the application on a Windows XP computer having the Windows XP Theme...
3
by: Lukas Kurka | last post by:
Hi, i would like to extend some ASP.NET controls but to do that I would like to see what is really happening inside. Is there a tool to take a look inside ASP.NET controls? Thanks Lukas
3
by: Pascal Cloup | last post by:
Hi, all the installers that i create with VS2005 (with deployement projects) have a flat look and feel under windows XP (flat rectangle buttons). Even the installers created with VS2003 (and...
5
by: Neil Cerutti | last post by:
For use in a hand-coded parser I wrote the following simple iterator with look-ahead. I haven't thought too deeply about what peek ought to return when the iterator is exhausted. Suggestions are...
2
by: puzzled | last post by:
I have "Find" buttons throughout my database, each with focus on a different field (they were created with the wizard, then the focus set to the corresponding field with VB). If i press a find button...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.