C++ Sorting the “percentage” of two paired integer arrays

后端 未结 3 488
鱼传尺愫
鱼传尺愫 2021-01-27 11:54

I have a program with 2 \"paired\" integer arrays newNumerator[ ], and newDenominator[ ] which both have 9 integers in them. I wrote a function that sorts them in ascending ord

3条回答
  •  感动是毒
    2021-01-27 12:32

    The main error you have is that you are giving wrong values to static_cast<>. Later you use them correctly by dereferencing, but in the cast you are missing that.

    What you need is:

    double percentageLeft = 100.0 * static_cast((*newNumerator)[count]) / *newDenominator[count];
    double percentageRight = 100.0 * static_cast((*newNumerator)[count + 1]) / *newDenominator[count + 1];
    

    Added parentheses to make it explicitly clear what the dereferencing is.

    Also if you are not changing the actual array pointers, but only the contents of the arrays, you can remove the dereferencing hassle altogether. If you define the function as

    void sortData(int newNumerator[], int newDenominator[], int newSize)
    

    you can just use the normal indexing without dereferencing every time you use it.

提交回复
热议问题