Order number of array

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Order number of array

1,577 Views
lionking1
Contributor I

Hi all,

 

I am trying to make a program, for example, I have 10 element array, after comparison, it is found that input value is between 6th and 7th. Then, I want assign that array order number (6) to an varible. How it happen?

 

Thanks for the help!

 

David

Labels (1)
Tags (1)
0 Kudos
9 Replies

715 Views
isionous
Contributor III

 

//returns 1 on success, 0 on failure
int func(int val, int arr[], int arrLen, int *desiredElem, int *desiredElemIdx){
    for(int i = 0; i < arrLen - 1; i++){
        if(arr[i] <= val && val < arr[i + 1]){
            *desiredElem = arr[i];
            *desiredElemIdx = i;
            return 1;
        }  
        if(arr[i] > val){
            return 0;
        }  
    }  
    return 0;
}


int main(int argc, char **argv){
    int arr[] = { 4, 6, 8, };
    int elem, idx;

    int result = func(7, arr, 3, &elem, &idx);
    printf("result=%d, elem=%d, idx=%d\n", result, elem, idx);

    return 0;
}



 

0 Kudos

715 Views
lionking1
Contributor I

Thank you, ISIONOUS!

 

I made my own program finally. It can define the order numbers, however, some simple calculation can't go through, like dt, du. I am pulling my hair. Can you help to check?

 

David

#include "math.h"
#include "MC9S08DZ60.h"


void main(void) {

  int i=0,j=0;
  float temp_in=16.8;
  float pressure_in=5.8;
  float temp[]={10.2, 14.7, 17.4, 21.2, 26.7};
  float *pa;
  float temp1, temp2;
  float pressure[]={1.5, 2.7, 3.9, 5.4, 6.9};
  float *pb;
  float pressure1, pressure2;
  float bipt[5][5]={
      {4.2, 7.5, 10.9, 14.1, 17.6},
      {5.1, 8.1, 12.4, 14.9, 18.7},
      {6.7, 9.1, 13.8, 16.7, 20.5},
      {7.8, 10.4, 15.4, 18.0, 21.7},
      {9.2, 11.2, 16.7, 19.1, 23.2}
  };
  float *pab;
  float t, u, z11, z12, z21, z22,dt, du;
  float z_pt;
 
  pa=&temp[0];
  pb=&pressure[0];
  pab=&bipt[0][0];
 
  if (temp_in>*pa&temp_in<*(pa+4)) {
    while (*(pa+i)<temp_in) {
         temp1=*(pa+i);
         temp2=*(pa+i+1);
         i++;
    }
    while (*(pb+j)<pressure_in){
         pressure1=*(pb+j);
         pressure2=*(pb+j+1);
         j++;
    }
    t=(temp_in-temp1)/(temp2-temp1);
    u=(pressure_in-pressure1)/(pressure2-pressure1);
    dt=1-t;
    du=1-u;
    z22=*(pab+i*5+j);
    z11=*(pab+(i-1)*5+(j-1));
    z12=*(pab+(i-1)*5+j);
    z21=*(pab+i*5+(j-1));
   
    //z_pt=(1-t)*(1-u)*z11+t*(1-u)*z21+t*u*z22+(1-t)*u*z12;
    
  }
        
  for(;;);   /* please make sure that you never leave main */
}

 

 

0 Kudos

715 Views
kef
Specialist I

I don't know what your code is supposed to do, but this is suspicious:

 

  if (temp_in>*pa&temp_in<*(pa+4)) {

 

 

& is a bitwise AND, and I think you want logical AND (&&) here.

 

 

0 Kudos

715 Views
lionking1
Contributor I

Kef,

 

This program is for bilinear interpolation. There are a 2d array and 2 1d arrays to be row and column (temperature and pressure). For any pair of temperature and pressure within the range, an estimating value can be available by equation.

 

I tried & to && before. It doesn't do anything about my problem.  In my code, the t and u are calculated, but 1-t or 1-u. For the step of 1-t, the program runs into blackhole, and get nothing. it is really annoying me.

 

David

0 Kudos

715 Views
kef
Specialist I

& with float arguments doesn't make sense

 

How much stack is specified in your prm file? I'm asking because all your variables are allocated on stack and you need over 200 bytes of stack just for variables. By default stack size if 128 bytes...

To reduce stack requirements you may move at least bipt array of constants to flash:

 

static const float bipt[5][5]={...};

const float *pab;

 

0 Kudos

715 Views
lionking1
Contributor I

Well, how to do it? Any reading available? Thanks!

 

David

0 Kudos

715 Views
bigmac
Specialist III

Hello,

 

Defining so many local variables withing main() can be problematic to the extent that they will occupy stack space that is never recovered.  Far better to define them as static variables outside of the main function, where they will occupy RAM space in other than the stack.

 

Your constant table(s) also reside in RAM, rather than flash.  In addition to consuming limited RAM resources, you are relying on the initialised values remaining uncorrupted for an indefinite period - not a good idea for an embedded project.

 

In addition to bipt[ ][ ] array, it would appear that the temp[ ] and pressure[ ] arrays are also constant?  If so, these could also be defined outside of main(), as const, to place in flash memory.

 

The number of intermediate float variables can possibly be reduced somewhat by direct use of pointer values or array indices within the expresions for variables t and u.  More stack may be used for the calculations, but this is recoverable.  Variables i and j may be char size.

 

There may also be a (lack of) parenthesis issue within the if ( ) statement, additional to the logical and requirement.  Personally, I think that the use of array indices, rather than pointers, makes this code more understandable, especially with the two dimensional array requirement.

 

 

if ((temp_in > temp[0]) && (temp_in < temp[4])) {
  i = 0;
  while (temp[i] < temp_in)
    i++;
  j = 0;
  while (pressure[j] < pressure_in)
    j++;

  t = (temp_in - temp[i]) / (temp[i+1] - temp[i]);
  u = (pressure_in - pressure[j]) / (pressure[j+1] - pressure[j]);

  z22 = bipt[i][j];
  z11 = bipt[i-1][j-1];
  z12 = bipt[i-1][j];
  z21 = bipt[i][j-1];

  ...

}

 

Regards,

Mac

 

0 Kudos

715 Views
lionking1
Contributor I

Mac,

 

Thank you for your owesome explaination. Because the chip (DZ60CLF) has both Flash (60K) and EEPROM (2K), I am curious which is better to put the data. What is the difference between EEPROM and Flash on performance.

 

 

0 Kudos

715 Views
bigmac
Specialist III

Hello,

 

Your constant arrays and tables should reside in flash unless they will need to be altered from within your program, i.e. as non-volatile data.  The primary difference between flash memory and EEPROM is the erase sector size, 768 bytes for flash, and 4 bytes for EEPROM.

 

Regards,

Mac

 

0 Kudos