C ++排序"百分比"两个成对整数数组 [英] C++ Sorting the "percentage" of two paired integer arrays

查看:198
本文介绍了C ++排序"百分比"两个成对整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2方案配对整型数组newNumerator []和newDenominator []这两者中都有9的整数。我写了他们排序按升序功能,但我不知道它的作品,因为我还没有得到它来编译成功呢。我有太多类型转换的一些问题。下面是函数定义 -

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 order, however I'm not sure it works as I have not gotten it to successfully compile yet. I am having some issues with typecasting too. Here is the function definition-

void sortData(int *newNumerator[], int *newDenominator[], int newSize)
{
    int temp1;
    int temp2;
    bool swap;
    int count = 0;
    double percentageLeft = 100.0 * static_cast<double>(newNumerator[count]) / newDenominator[count];
    double percentageRight = 100.0 * static_cast<double>(newNumerator[count + 1]) / newDenominator[count + 1];

    do
    {  swap = false;
        for(count = 0; count < (newSize - 1); count++)
        {
            if(percentageLeft > percentageRight)
            {
                temp1 = *newNumerator[count];
                *newNumerator[count] = *newNumerator[count + 1];
                *newNumerator[count + 1] = temp1;

                temp2 = *newDenominator[count];
                *newDenominator[count] = *newDenominator[count + 1];
                *newDenominator[count + 1] = temp2;

                swap = true;
            }
        }
    } while (swap);
}

我遇到的类型转换问题是percentageLeft和percentageRight因为newNumerator []和newDenominator []是整数的指针,但要获得百分比他们中的我需要它是双。不知道如何去这个问题。基本上我只需要弄清楚如何解决这个问题,也知道如果我的功能,甚至提供它的目的。任何帮助是AP preciated,并请让我知道如果有什么我可以进一步明确

The typecasting problem I am having is with percentageLeft and percentageRight because newNumerator[ ] and newDenominator[ ] are integer pointers, but to get the "percentage" of them i need it to be a double. Not sure how to go about this. Basically i just need to figure out how to solve this issue, and also know if my function even serves its purpose. Any help is appreciated, and please let me know if there is anything I can further clarify

推荐答案

您的主要错误是,你到的static_cast&LT给人错误的价值观;&GT; 。后来你正确地使用它们提领,但中投,您缺少。

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.

您需要的是:

double percentageLeft = 100.0 * static_cast<double>((*newNumerator)[count]) / *newDenominator[count];
double percentageRight = 100.0 * static_cast<double>((*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.

这篇关于C ++排序&QUOT;百分比&QUOT;两个成对整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆