CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Array Accessing - [SOLVED]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

Array Accessing - [SOLVED]
PostPosted: Tue Aug 14, 2012 7:51 am     Reply with quote

Sorry to bother you all again,
but I have a question regarding arrays and look-up tables.

If I had an array [46] [2]

And to access it you would say:
I want what is in row [35] and column [2].
so X = [35] [2] returns what is in that location from the array. In this case = 6.
This is the normal way of doing it.

But is there any way to 'match' the information in the array to a variable rather than accessing the row/column??

so my array is below
Code:
unsigned int32 max_val[46][2] = {
{0,0},          //row0
{64,33},                       
{100,16},
{128,11},
{152,8},
{180,6},         //row5
{256,6},
{360,5},
{512,4},
{600,4},
{720,3},       //row 10
{800,3},
{1000,3},
{1024,3},
{1440,3},
{1500,2},      //row 15
{2000,2},
{2048,2},
{2880,2},
{3600,2},
{4096,2},      //row 20
{0X3FF,33},                       
{0X7FF,16},
{0XFFF,11},
{0X1FFF,8},
{0X3FFF,6},     //row25
{0X7FFF,6},
{0XFFFF,5},
{0X1FFFF,4},
{0X3FFFF,4},
{0X7FFFF,3},    //row 30
{0XFFFFF,33},                       
{0X1FFFFF,16},
{0X3FFFFF,11},
{0X7FFFFF,8},
{0XFFFFFF,6},   //row35
{0X11DF,6},
{0X23BF,5},
{0X477F,4},
{0X8EFF,4},
{0X11DFF,3},    //row 40
{0X14F27,3},
{0X23BFF,3},
{0X3245F,3},
{0X477FF,3},
{0X8E,2},      //row 45
};


Now if I read a value (which will always be incorporated in the array above) from somewhere else (eeprom or other hardware), and lets say the value is val = 0X477FF.

Is it possible to search the array, to find when 'val' = 'the value in the first array' - so that I can then return the value from the second array - in this case '3' would be returned.

Any help appreciated
Carl


Last edited by carl on Wed Aug 15, 2012 3:12 am; edited 1 time in total
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Tue Aug 14, 2012 9:07 am     Reply with quote

Tried using switch case as below but got this error message:
Quote:
Constant out of the valid range 131071 is not less than 65536


so I presume that a switch case can only work on 16bit variables.
I need 32 bit - so this method is not going to work.
Code:
extern unsigned int32 load_max_result;

unsigned int32 get_max(void){
switch (load_max_result){

case 64:    return 0;                   
case 100:   return 76;   
case 128:    return 0;                   
case 152:   return 76;   
case 180:    return 0;                   
case 256:   return 76;   
case 360:    return 0;                   
case 512:   return 76; 
case 600:    return 0;                   
case 720:   return 76;   
case 800:    return 0;                   
case 1000:   return 76; 
case 1024:    return 0;                   
case 1440:   return 76;   
case 1500:    return 0;                   
case 2000:   return 76; 
case 2048:    return 0;                   
case 2880:   return 76;   
case 3600:    return 0;                   
case 4096:   return 76; 
case 0x3FF:    return 0;                   
case 0x7FF:   return 76;   
case 0xFFF:    return 0;                   
case 0x1FFF:   return 76; 
case 0x3FFF:    return 0;                   
case 0X7FFF:   return 76;   
case 0XFFFF:    return 0;                   
case 0X1FFFF:   return 76; 
case 0X3FFFF:    return 0;                   
case 0X7FFFF:   return 76;   
case 0XFFFFF:    return 0;                   
case 0X1FFFFF:   return 76; 
case 0X3FFFFF:    return 0;                   
case 0X7FFFFF:   return 76;   
case 0XFFFFFF:    return 0;                   
case 0X11DF:   return 76;   
case 0X23BF:   return 76; 
case 0X477F:    return 0;                   
case 0X8EFF:   return 76;   
case 0X11DFF:    return 0;                   
case 0X14F27:   return 76; 
case 0X23BFF:    return 0;                   
case 0X3245F:   return 76;   
case 0X477FF:    return 0;                   
case 0X8E:   return 76;
      }
}


Any THoughts?
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Tue Aug 14, 2012 1:57 pm     Reply with quote

Anyone?

The only other way I can think of if loads of nested IF's - but I dont like this plan....
Ttelmah



Joined: 11 Mar 2010
Posts: 19347

View user's profile Send private message

PostPosted: Tue Aug 14, 2012 2:22 pm     Reply with quote

Just search the array. Not exactly 'rocket science'.....
Code:

int8 find(int32 valtofind) {
   int8 count;
   for (count=0;count<46;count++) {
      if (max_val[count][0]==valtofind) return(max_val[count][1]);
   }
   return(0); //put what you want here - error did not find the value;
}

This is really a 'C 101' question, nothing to do with CCS as such.

Best Wishes
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Aug 14, 2012 2:23 pm     Reply with quote

your exposition is not totally clear to me.

there are ways to index - as you seem to desire with TWO arrays

where each is loaded with constants

as in
int32 a[48]
int 8 b[48]

then searching with comparisons ( especially if the arrays are SORTED from hi to lo or vice versa ) funny but, i just wrote some code like that this morning to add to a project.

here is an excerpt


Code:

int16 omult=0;
const unsigned int16 fcor[8]={2700,3143,3883,5191,5333,5491,5571,5659,};
const unsigned int32 gfreq[8]={250000,200000,150000,100000,96000,92000,90000,80000,};
.... later

for (j=0; j<8; j++) {  if (infreq>=gfreq[j]) { omult=fcor[j];  break;  }}
// the version i actually wrote has 72 elements but this is the idea
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Wed Aug 15, 2012 3:12 am     Reply with quote

Thanks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group