-1

essentially I have a matrix of form (20,20). The numbers within this array range from 0 to 2.00, my aim is to set a display value for specific ranges (i.e. 0-0.4 will be displayed as "1", 0.4-0.8 will be displayed as "2"... 1.6-2.0 will be displayed as "5".) However, I am struggling to make this work, my code for this specific section is as follows:

    //set display values for classification map
        {
            if(rawData[rowIndex][columnIndex]<=0.4)
                printf("1");
            else if(rawData[rowIndex][columnIndex]<=0.8)
                printf("2");
            else if(rawData[rowIndex][columnIndex]<=1.2)
                printf("3");
            else if(rawData[rowIndex][columnIndex]<=1.6)
                printf("4");
            else if(rawData[rowIndex][columnIndex]<=2.0)
                printf("5");
            else printf("ERROR");
        }
        printf("\n");
    }
}

Thanks to any responses :) I am extremely new to programming so any help is much appreciated!!

SM.K
  • 3
  • 1
  • [See this](http://stackoverflow.com/q/588004/2173917) – Sourav Ghosh Sep 02 '16 at 13:03
  • 1
    What issues do you have with your code? – interjay Sep 02 '16 at 13:10
  • What is your input, expected output, and actual output? – dbush Sep 02 '16 at 13:12
  • @SouravGhosh How is that related ? – Nelfeal Sep 02 '16 at 13:13
  • @Nelxiost Sorry? What is not related? It is about FP math, right? – Sourav Ghosh Sep 02 '16 at 13:15
  • @SouravGhosh Is every question about FP math related to "is floating point math broken" ? And even if that was the case, the problem is about mapping ranges of FP numbers to integers. Not much about FP calculations. – Nelfeal Sep 02 '16 at 13:17
  • The issue is when I try to run the program (using code::blocks console application), rather than printing the display number for the range for each specific value, it will add an extra column to the end of the code, displaying integers from 1 to 5, however they are in seemingly random order. – SM.K Sep 02 '16 at 13:20
  • @SM.K If you are looping through your matrix, include the loop statements. Also state how you want the whole thing to be displayed. I would assume you have a row loop containing a column loop, which in turn contains the snippet you are showing. If that's the case, then it's just displaying what you want but in a single column instead of a matrix. – Nelfeal Sep 02 '16 at 13:29
  • Thank you @Nelxiost that was a big help! – SM.K Sep 03 '16 at 06:05

1 Answers1

1

What about:

int value;
if ( rawData[rowIndex][columnIndex] > 2.0 
   || rawData[rowIndex][columnIndex] < 0) {
   printf("ERROR");
} else {
  value = (int)ceil(rawData[rowIndex][columnIndex]/0.4);
  if ( value == 0 )
     value = 1;
  printf("%d", value);
}
Teudimundo
  • 2,610
  • 20
  • 28
  • Something like that, yes, though that particular one prints the wrong thing for the exact boundary values. I suggest using `ceil()` instead of adding one, and creating a special case for zero. – John Bollinger Sep 02 '16 at 13:39