integer arrays return 0 but work when defined as float

Hi Guys, I am changing my code from using floating point values to integer ones.

I have these two lookup arrays declared globally:

int batteryspec[6][56]={{4200,4000,3940,3900,3876,3860,3852,3844,3826,3809,3795,3783,3765,3744,3728,3710,3692,3672,3650,3631,3613,3591,3573,3556,3532,3508,3488,3463,3439,3417,3399,3380,3366,3352,3332,3312,3291,3273,3253,3237,3217,3196,3176,3150,3123,3087,3051,2994,2929,2868,2796,2727,2650,5261,2502},//5A discharge 18650GA SPEC
  {1,2,15,47,85,133,186,244,309,383,447,506,554,612,681,735,793,852,921,1007,1081,1150,1236,1300,1358,1438,1518,1587,1667,1747,1822,1888,1955,2014,2072,2152,2232,2317,2389,2466,2524,2594,2658,2722,2786,2847,2925,2983,3063,3133,3186,3245,3293,3330,3357,3368},
  {4200,4020,3980,3945,3927,3914,3904,3890,3874,3860,3843,3821,3797,3776,3756,3734,3713,3695,3675,3652,3634,3614,3593,3569,3543,3522,3500,3480,3459,3441,3419,3398,3378,3360,3339,3321,3301,3278,3254,3230,3199,3164,3128,3085,3037,2990,2937,2878,2837,2795,2746,2693,2648,2598,2549,2496},
  {0,7,33,84,151,223,294,377,457,521,574,646,718,785,851,931,1011,1091,1171,1251,1323,1397,1469,1547,1632,1701,1778,1853,1928,1994,2088,2175,2258,2333,2413,2487,2562,2634,2706,2775,2842,2908,2697,3023,3074,3116,3159,3205,3234,3261,3287,3314,3333,3352,3365,3379},//3A discharge 18650GA SPEC
  {4200,4131,4094,4073,4054,4038,4028,4015,4003,3985,3965,3945,3921,3894,3870,3850,3830,3811,3795,3775,3759,3739,3716,3692,3674,3648,3623,3601,3578,3561,3542,3522,3502,3486,3467,3445,3423,3401,3374,3350,3324,3291,3253,3210,3168,3123,3075,3026,2964,2897,2838,2781,2715,2652,2585,2502},
  {0,0,36,84,154,223,289,356,420,489,553,620,686,761,838,908,985,1054,1134,1214,1283,1358,1438,1523,1584,1667,1755,1835,1920,2002,2085,2170,2255,2327,2402,2482,2567,2644,2724,2791,2850,2908,2964,3015,3058,3095,3132,3167,3207,3245,3271,3295,3320,3336,3354,3365}}; //1A discharge 18650GA SPEC

int cyclicspec[2][44]={{0,894,7151,16090,26400,37500,50950,61680,74190,85360,96540,107260,118880,130950,142120,154640,168040,182350,194860,208270,219890,234200,246700,262800,276200,291400,303900,317300,3271500,341500,354000,364000,374500,383460,395080,405800,418300,431700,444000,455900,468800,481900,491600,499700},
  {3441,3435,3342,3292,3235,3178,3121,3078,3014,2957,2921,2892,2842,2792,2750,2721,2679,2643,2607,2564,2543,2507,2479,2436,2407,2379,2357,2336,2336,2336,2336,2336,2336,2321,2300,2279,2271,2243,2229,2214,2193,2179,2164,2157}};

However, whenever I call them in the code whether in a function or in loop() I get the value 0. When I define them as float and not integers, they return the proper values upon being called. WHy is that? Thank you for your help!

You may be doing an integer divide that returns zero.

Post your code, using code tags ("</>" button).

I am going to guess that you think they are zero after being used in an integer division. Note that 123/456 will return 0 in integer division.

I’m calling it in this function:

void SOC() {
  int currentref=current/p, compbat=batvoltage/14;
  int j=1,l=1;
  long stateofc1=0, stateofc2=0;

  if (currentref<=1000){
    while (((compbat)>batteryspec[0][j-1])||((compbat)<batteryspec[0][j])){    
           j++;
    }
    capacactual=(batteryspec[1][55]-(batteryspec[1][j-1]+(compbat-batteryspec[0][j-1])*(batteryspec[1][j]-batteryspec[1][j-1])/(batteryspec[0][j]-batteryspec[0][j-1])))*p;
  }
  Serial.print("\n");
        Serial.print(j);
           Serial.print("\n");
        Serial.print(batteryspec[1][5]);
          Serial.print("\n");
          Serial.print("\n");
     
 if ((currentref>1000)&&(currentref<=3000)){
    while (((compbat)>batteryspec[0][j-1])||((compbat)<batteryspec[0][j])){    
           j++;
    }
    stateofc1=(batteryspec[1][55]-(batteryspec[1][j-1]+(compbat-batteryspec[0][j-1])*(batteryspec[1][j]-batteryspec[1][j-1])/(batteryspec[0][j]-batteryspec[0][j-1])))*p;

     while (((compbat)>batteryspec[2][l-1])||((compbat)<batteryspec[2][l])){    
           l++;
    }
    stateofc2=(batteryspec[3][55]-(batteryspec[3][l-1]+(compbat-batteryspec[2][l-1])*(batteryspec[3][l]-batteryspec[3][l-1])/(batteryspec[2][l]-batteryspec[2][l-1])))*p;
    capacactual=stateofc1+(currentref-1)*(stateofc2-stateofc1)/(3000-1000);
  }

   if ((currentref>3000)&&(currentref<=5000)){
    while (((compbat)>batteryspec[2][j-1])||((compbat)<batteryspec[2][j])){    
           j++;
    }
//    stateofc1=(batteryspec[3][55]-(batteryspec[3][j-1]+(compbat-batteryspec[2][j-1])*(batteryspec[3][j]-batteryspec[3][j-1])/(batteryspec[2][j]-batteryspec[2][j-1])))*p;

     while (((compbat)>batteryspec[2][l-1])||((compbat)<batteryspec[2][l])){    
           l++;
    }
//    stateofc2=(batteryspec[3][55]-(batteryspec[3][l-1]+(compbat-batteryspec[2][l-1])*(batteryspec[3][l]-batteryspec[3][l-1])/(batteryspec[2][l]-batteryspec[2][l-1])))*p;
    capacactual=(batteryspec[3][55]-(batteryspec[3][j-1]+(compbat-batteryspec[2][j-1])*(batteryspec[3][j]-batteryspec[3][j-1])/(batteryspec[2][j]-batteryspec[2][j-1])))*p+(currentref-1)*((batteryspec[5][55]-(batteryspec[5][l-1]+(compbat-batteryspec[4][l-1])*(batteryspec[5][l]-batteryspec[5][l-1])/(batteryspec[4][l]-batteryspec[4][l-1])))*p-(batteryspec[3][55]-(batteryspec[3][j-1]+(compbat-batteryspec[2][j-1])*(batteryspec[3][j]-batteryspec[3][j-1])/(batteryspec[2][j]-batteryspec[2][j-1])))*p)/(5000-3000);

  }

Both Serial.print in this function and in the loop one return 0.

Yep. Divide.

trying to set a 16-bit int to a value greater than 32767 is not wise.

use longs for your second array to make sure you get 31 usable bits for non-negative values.