-4

An integer division caused by the elements of an array returns 0, I'm supposed to store the % in the same array....

array[6][i]=array[5][i]/total;

This stores a 0... I thought it had something to do with the array being an integer array... so I did a cast...

array[6][i]=(int)(array[5][i]/total); 

Still stored 0... I read I had to convert them to floating points but the casting doesn't work... I tried this

array[6][i]=(int)((float)array[5][i]/(float)total); 

the array declaration

int arreglo[7][5]={{1,194,48,206,45},{2,180,20,320,16},{3,221,90,140,20},{4,432,50,821,14},{5,820,61,946,18},{0,0,0,0,0},{0,0,0,0,0}};

and the last one will store each percentaje

OHHH
  • 1,011
  • 3
  • 16
  • 34

2 Answers2

3

If you want a percentage, then what you're looking for is something like

array[6][i] = (int) (100 * ((float)array[5][i] / (float)total));
prprcupofcoffee
  • 2,950
  • 16
  • 20
2

This will always return 0.

If you are working with ints, and you divide by the total, the result will be <1 and truncated to 0 (as the result must be an integer).

You have to either use doubles (or floats) arrays, or scale the integers by a factor of eg 100 (not the total)

thedayofcondor
  • 3,860
  • 1
  • 19
  • 28