Thread: C Coding Help: Unreachable Code

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    13

    C Coding Help: Unreachable Code

    Hey guys, im currently writing code to determine the median for a data set. It is a very basic style of C coding as I have just recently begun playing around with it.
    I am getting this error "warning #2154: Unreachable code." for every MedianIndex1/MedianIndex2 in the code. I dont understand why?
    The code still executes but it is wrong, and i think this error has something to do with it.
    Prior to this the code has been sorted, for rows and columns. Meaning that the middle index is the median.

    thanks in advance


    main.c

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    In the future, please post your code in code tags instead of attaching a file. People here are leery to download a file from somebody they don't know.

    As for your problem, you have many pieces of code that are inside if statements like:
    Code:
    if (( MAX_ROWS % 2) == 0)
    ...
    if ((MAX_COLS % 2) == 0)
    MAX_ROWS and MAX_COLS are a constants (set to 21 and 19 respectively). Their values, modulo 2, are not zero (i.e. they are odd), so you can never get "inside" those if statements. Since they're constants and their value never changes, the code inside those if statements is unreachable.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    Hey thanks, i am new and will do so from now on.
    I am allowing the code to determine for both odd and even columns/rows so i can go back later and do even number of rows/columns.
    I know that in this case i am dealing with odd numbers for both columns and rows. ((MAX_ROWS % 2) == 1) is true for this case, so why is it unreachable?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    This is not going to do what you expect
    Code:
    		MedianIndex1 = ((MAX_ROWS / 2) + 0.5);
    try something like that
    Code:
    		MedianIndex1 = (MAX_ROWS + 1 ) / 2 ;
    Kurt

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That shouldn't be happening, and my compiler only shows it on the two lines I referenced above, so something doesn't jive. Please post, in code tags, the exact code you're compiling. Also post the command you're using to compile and the exact compiler output, with line numbers.

    On a side note:
    Code:
    MedianIndex1 = ((MAX_ROWS / 2) + 0.5);
    That line probably doesn't do what you think. MAX_ROWS is an integer, as is 2. Dividing MAX_ROWS by 2 uses integer division, which throws away the fractional portion, i.e. 21 / 2 is 10, not 10.5. So then you add 10 + 0.5 and get a floating point value of 10.5, but then stuff that value back in an integer variable (MedianIndex1), which again throws away the fractional part. Not sure exactly what you want to do here, but perhaps you want something like:
    Code:
    MedianIndex1 = (MAX_ROWS + 1) / 2;
    That first adds one, making MAX_ROWS, e.g. 22, then divides by 2, giving 11. Since 22 is even, in integer division there is no fractional part to throw away, and you (presumably) get what you want.

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    I am trying to compile the following code (printf), as i know there is an odd number of columns.

    Code:
    if ((MAX_ROWS % 2) == 1)
        {
            /* get the middle index */
            MedianIndex1 = ((MAX_ROWS + 1) / 2);
        }
        /* store median values */
        for (row = 0; row < MAX_ROWS; row++)
        {
            RowMedian[row] = SortedRowData[row][MedianIndex1];
        /* printf("%f\n",RowMedian[row]); */
        }
        
           if ((MAX_COLS % 2) == 1)
        {
            /* get the middle index */
            MedianIndex1 = ((MAX_COLS + 1) / 2);
        }
        /* store median values */
        for (col = 0; col < MAX_COLS; col++)
        {
            ColMedian[col] = SortedColData[MedianIndex1][col];
        
         printf("%f\n",ColMedian[col]);
        }

    when compiled the console program output is as follows:

    0.951580
    0.963490
    0.963080
    0.950930
    0.952360
    0.951630
    0.935300
    0.939930
    0.944860
    0.943570
    0.934280
    0.941670
    0.964340
    0.950390
    0.933500
    0.960410
    0.946780
    0.945750
    0.956680

    the output is in the right form, although the numbers are not correct. I know for a fact that the data, has been sorted in order for both columns and rows as i have compiled it prior to writing this code. so it seems to me that this code, isnt identifying the right column/row to select.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Can you provide the data file that produces incorrect results and tell us what the correct results should be?

  8. #8
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I think you mix up row and column medians.

    Code:
    /* get the middle index */
    MedianIndex1 = ((MAX_ROWS + 1) / 2);
    Here you calculate the median based on the number of rows (in your example MAX_ROWS == 21, so MedianIndex1 == 11).

    Code:
    for (row = 0; row < MAX_ROWS; row++)
    {
        RowMedian[row] = SortedRowData[row][MedianIndex1];
        /* printf("%f\n",RowMedian[row]); */
    }
    Here you go through each row and use MedianIndex1 as the column index. In your example MAX_COLS is 19, thus the median index should be 10 and not 11 (the value of MedianIndex1).
    If you want to get the median value for each row you have to calculate the median index based on the number of columns and vice versa.

    Bye, Andreas

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    thanks Andipersti, so how would i write this?
    do i need to create another index?

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Just change MedianIndex1 to
    Code:
    MedianIndex1 = ((MAX_COLS + 1) / 2);
    and do accordingly with MedianIndex2.

    Bye, Andreas

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    so for both cols and rows. the median index is (MAX_COLS + 1) / 2

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    No.

    Say you have the following (ordered) table:
    Code:
     1  2  3  4  5
     6  7  8  9 10
    11 12 13 14 15
    Then the median for the first row is 3, for the second 8, for the third 13, i.e. the values in the third column (because you have 5 columns, the median column is (5 + 1) / 2 == 3).

    For the median of each column you are interested in the values in the second row because (max_rows + 1) / 2 == 2.

    Do you understand now?

    Bye, Andreas

  13. #13
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    i understand what your saying but i think my code produces what you are saying.

  14. #14
    Registered User
    Join Date
    Aug 2012
    Posts
    13
    So for an odd number of rows/columns as i know i have..
    my code is as follows

    FOR ROWS:

    Code:
    if ((MAX_ROWS % 2) == 1)
        {
            MedianIndex1 = ((MAX_ROWS + 1) / 2);
        }
        for (row = 0; row < MAX_ROWS; row++)
        {
            RowMedian[row] = SortedRowData[row][MedianIndex1];
         
         /*  printf("%f\n",RowMedian[row]); */
        }
    FOR COLS

    Code:
    if ((MAX_COLS % 2) == 1)
        {
            MedianIndex2 = ((MAX_COLS + 1) / 2);
        }
        for (col = 0; col < MAX_COLS; col++)
        {
            ColMedian[col] = SortedColData[MedianIndex2][col];
        
         /* printf("%f\n",ColMedian[col]); */
        }
    When i output these, the ColMedian is perfect!
    However when i output the RowMedian the values are wrong. I have established that it is returning the 12th value of the sorted data, rather than the 10th. I am unsure as to why!

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    OK, I'll try it a last time:

    Quote Originally Posted by sammells View Post
    However when i output the RowMedian the values are wrong. I have established that it is returning the 12th value of the sorted data, rather than the 10th. I am unsure as to why!
    You have defined MAX_ROWS to be 21 and MAX_COLS to be 19. Is this still correct?

    Now put in the numbers in your code:
    Code:
    MedianIndex1 = ((MAX_ROWS + 1) / 2);
    MedianIndex1 will be (21 + 1) / 2 == 11, agreed?
    Index 11 means it's the 12th element in an array, agreed?

    Code:
    for (row = 0; row < MAX_ROWS; row++)
    {
        RowMedian[row] = SortedRowData[row][MedianIndex1];
     }
    Now look at the first iteration. row will be 0, thus you store the value of SortedRowData[0][11] in RowMedian[0], i.e. you store the value in the 12th column of the the first row. You have 19 columns, thus you are calculating the wrong index.
    Now look at what is happening if you calculate MedianIndex1 with the following line:

    Code:
    MedianIndex1 = ((MAX_COLS + 1) / 2);
    MedianIndex1 will be (19 + 1) / 2 == 10.
    Now this looks like it's correct because the 10th element is the median of a 19th column sorted row. But indices start with 0 so you really want MedianIndex to be 9 instead of 10. Otherwise you would get the 11th element of the row. Hence decrement MedianIndex1 or use just (MAX_COLS / 2).

    Code:
    if ((MAX_COLS % 2) == 1)
        {
            MedianIndex2 = ((MAX_COLS + 1) / 2);
        }
        for (col = 0; col < MAX_COLS; col++)
        {
            ColMedian[col] = SortedColData[MedianIndex2][col];
        
         /* printf("%f\n",ColMedian[col]); */
        }
    When i output these, the ColMedian is perfect!
    This is sheer luck. Let's do the math again:
    MedianIndex2 == (19 + 1) / 2 == 10
    SortedColData[10][col] will get you the 11th element of the sorted column and it happens to be the median but only in this case (21 rows, 19 columns). Do the math for any other MAX_COLS value and you will get the wrong index for the reason I've written above.

    Again, that's my last try. I just don't know how to make it clearer.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unreachable code
    By tauqeer ahmed in forum C Programming
    Replies: 1
    Last Post: 03-31-2012, 04:57 AM
  2. Unreachable code in function main()
    By DavidSS in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2010, 07:57 AM
  3. Needing Assistance;Unreachable code in function main()
    By Mehtabyte in forum C++ Programming
    Replies: 5
    Last Post: 11-19-2008, 02:27 PM
  4. Unreachable code?
    By Leojeen in forum C Programming
    Replies: 15
    Last Post: 09-28-2008, 07:11 PM
  5. warning C4702: unreachable code
    By cpjust in forum C++ Programming
    Replies: 8
    Last Post: 05-05-2008, 03:24 PM

Tags for this Thread