0
#include <stdio.h>

int order = 3;
int wt[10] = {2,1,3};
int price [10] = {100,50,150};
int maxW = 5;

void fracK() {
    int curr_weight, i, max_i;  // <<<<
    float tot_price;            // <<<<
    int used[10];               // <<<<

    //inititialising all used elements to 0
    for (i = 0; i < order; ++i) {
        used[i] = 0;
    }

    curr_weight = maxW;

    while (curr_weight > 0) {
        max_i = -1;

        for (i = 0; i < order; ++i) {
            if ((used[i] == 0) && ((max_i == -1) || ((float)price[i]/wt[i] > (float)price[max_i]/wt[max_i]))){
                max_i = i;
            }
        }
        used[max_i] = 1;
        curr_weight -= wt[max_i];
        tot_price += price[max_i];

        if (curr_weight >= 0) {
            continue;
        }else {
            tot_price -= price[max_i];
            tot_price += (1 + (float)curr_weight/wt[max_i]) * price[max_i];
        }
    }
    printf("%f", tot_price);
}

//driver function
int main(int argc, char *argv[]) {
    fracK();
    return 0;
}

in lines 9 to 11, if I declare float in the second or third line i.e. line 10 or 11, the final value returned is 197040072659526240000000000000000.000000, which is not my expected value. However, when I declare the float variable in the first line, i.e. line 9, the final value returned is 250.000000 which is my expected value.

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
K___V
  • 32
  • 4
  • 2
    Such things happen when you have an undefined behavior bug somewhere. Like array out of bounds access. Have you tried single stepping through the code using your favourite debugger? – Lundin Nov 10 '22 at 12:24
  • 1
    You can use`max_i` as an index while it's still equal to `-1`. Which will be an out-of-bounds index. – Some programmer dude Nov 10 '22 at 12:26
  • 1
    I also recommend you learn how to use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code line by line while monitoring variables and their values. That will tell you *exactly* when things start to go wrong, and when and where you have indexes out of bounds. – Some programmer dude Nov 10 '22 at 12:28
  • @MarkBenningfield May I know what error it is showing? It compiles in my machine. I thought the markup would automatically make the code under the ***'s bold, but it didn't seem to work. My apologies, I am not so well versed with SO. – K___V Nov 10 '22 at 12:45
  • 1
    @K___V [What compiler options are recommended for beginners learning C?](https://software.codidact.com/posts/282565) The error displayed after compiling with those options is: "error: 'tot_price' may be used uninitialized", which is very self-explanatory. – Lundin Nov 10 '22 at 13:34

1 Answers1

3

It should be:

float tot_price = 0;

then the placement probably will not matter. As is now, the code is adding numbers to uninitialized variable, which will not have predictable results.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    So yet again the problem turns out to be someone disabling/ignoring compiler warnings... – Lundin Nov 10 '22 at 13:33
  • @Lundin Can you tell me how to enable compiler warnings? I use gcc on a windows machine – K___V Nov 10 '22 at 15:15
  • 1
    @K___V As per the link posted in my comment below your question. How to set compiler options for gcc in a specific Windows IDE might vary a bit depending on IDE. – Lundin Nov 10 '22 at 15:21