简体   繁体   English

与使用相同输入的其他几个编译器相比,用Xcode编译的代码给出了不正确的(?)输入

[英]Code compiled in Xcode gives an incorrect(?) input vs several other compilers with same inputs

Test inputs and outputs from Xcode (incorrect) and other (web based compilers) are as following: Xcode(不正确)和其他(基于Web的编译器)的测试输入和输出如下:

Input 1: 3 30 0.90 40 输入1:3 30 0.90 40

Xcode Output 1: Acceptable success rate: 0.90 Xcode输出1:可接受的成功率:0.90
Average attempted sales: 30.00 平均尝试销售:30.00
Average completed sales: 19.33 平均完成销售额:19.33
Observed success rate: 0.64 观察成功率:0.64
Observed success rate not in acceptable range. 观察到的成功率不在可接受的范围内。
Program ended with exit code: 0 程序以退出代码结束:0

Other Compilers' Output 1(correct): Acceptable success rate: 0.90 其他编译器的输出1(正确):可接受的成功率:0.90
Average attempted sales: 30.00 平均尝试销售:30.00
Average completed sales: 19.67 平均完成销售额:19.67
Observed success rate: 0.66 观察成功率:0.66
Observed success rate not in acceptable range. 观察到的成功率不在可接受的范围内。

Input 2: 10 100 0.5 1 输入2:10100 0.5 1

Xcode Output 2: Acceptable success rate: 0.50 Xcode输出2:可接受的成功率:0.50
Average attempted sales: 100.00 平均尝试销售:100.00
Average completed sales: 29.40 平均完成销售额:29.40
Observed success rate: 0.29 观察成功率:0.29
Observed success rate not in acceptable range. 观察到的成功率不在可接受的范围内。
Program ended with exit code: 0 程序以退出代码结束:0

Other Compilers' Output 1(correct): Acceptable success rate: 0.50 其他编译器的输出1(正确):可接受的成功率:0.50
Average attempted sales: 100.00 平均尝试销售:100.00
Average completed sales: 28.80 平均完成销售额:28.80
Observed success rate: 0.29 观察成功率:0.29
Observed success rate not in acceptable range. 观察到的成功率不在可接受的范围内。

#include <iostream>
#include  <iomanip>
#include <stdlib.h>
using namespace std;

int main()
{
    int iterations, seed;
    double successRate;

    float completedSales = 0, numAttempted;
    int  i, t;
    int seatChoice;

    //Add other variables necessary for your program

    //cout << "Please enter number of iterations to be evaluated" <<endl;
    cin >> iterations; //number of iterations to be evaluated

    //cout << "Please enter number of attempted sales" <<endl;
    cin >> numAttempted; //number of attempted sales

    //cout << "Please enter expected success rate" <<endl;
    cin >> successRate;

    //cout << "Please enter seed for random number generation" <<endl;
    cin >> seed; //seed for random number generation


    //Validate input
    if(iterations <= 0 || numAttempted < 0 || seed < 0 ) {
        cout << "Error. Invalid parameters.";

    }
    else if(successRate < 0 || successRate > 1) {
        cout << "Error. Invalid parameters.";
    }
    else {
        //Set seed and start iterations
        srand(seed);

        //Every iteration of the simulation starts by setting all seats to empty

        for(i = 0; i < iterations; ++i) {

            int availableSeats[30] = {0};

            for(t = 0; t < numAttempted; ++t) {
                seatChoice = (rand() % 30);

                if(availableSeats[seatChoice] == 0) {
                    availableSeats[seatChoice] = 1.0;
                    completedSales++;
                }
            }
        }
        float averageSales = (float) completedSales/iterations;
        float observedSuccessRate = (float) averageSales/numAttempted;

        //Complete final output:
        cout << setprecision(2) <<fixed;
        cout << "Acceptable success rate: "  <<successRate <<endl;
        cout << "Average attempted sales: " <<(double)numAttempted <<endl;
        cout << "Average completed sales: " <<averageSales <<endl;
        cout << "Observed success rate: " <<(float)observedSuccessRate <<endl;

        //Complete if statement to test success rate
        if(observedSuccessRate < successRate) {
            cout << "Observed success rate not in acceptable range." << endl;
        }
        else {
            cout << "Observed success rate is acceptable." << endl;
        }
    }
    return 0;
}

Upon re-reading your code, is the "incorrect" behavior simply the different outcome given the same seed? 重新读取您的代码后,“错误”行为是否就是给定相同种子的不同结果? This could be due to a different implementation of srand and rand between C++ compilers (and standard libraries). 这可能是由于C ++编译器(和标准库)之间对srandrand实现不同。

Xcode uses Clang, which defaults to libc++, while your other compilers might use GCC, which uses lib std c++. Xcode使用Clang(默认为libc ++),而其他编译器可能使用GCC(使用lib std c ++)。 The Microsoft Visual Studio compiler has its own implementation as well. Microsoft Visual Studio编译器也有自己的实现。 In general, you cannot rely on these implementations being equal. 通常,您不能依靠这些实现方式相等。

Your options are to either create your own Pseudo-random number generator (PRNG) or use the built-in Mersenne twister generator std::mt19937 . 您可以选择创建自己的伪随机数生成器(PRNG)或使用内置的梅森扭曲器生成器 std::mt19937 That should yield the same results across platforms. 跨平台应该会产生相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 VS 代码在 c++ 代码中显示错误,该代码在在线编译器和 Xcode 上运行良好 - VS code showing error in c++ code which is working absolutely fine on online compilers and Xcode 与其他编译器一起编译QT代码 - Compiling QT code with other compilers 当相同的源代码在两个不同的编译器下给出不同的答案时,它意味着什么? - What does it mean when the same source code gives different answers under two different compilers? VS 2017 .cpp在其他编译器中无法正确打开 - VS 2017 .cpp opens incorrectly in other compilers 显式模板实例化在XLC上提供编译错误,但适用于其他编译器 - explicit template instantiation gives compile error on XLC, but works on other compilers waitpid()提供错误的退出代码 - waitpid() gives incorrect exit code 编译时自动从 input.txt 在 VS 代码终端中输入 - Auto input in VS code terminal from input.txt whenever compiled 竞争性编程-代码在在线编译器上给出了不同的答案 - Competitive Programming - Code gives different answers on online compilers ##__VA_ARGS__ 与其他编译器的 MSVC 处理 - MSVC treatment of ##__VA_ARGS__ vs. other compilers 为什么我的代码不允许我将值输入到字符串变量中,而其他用户输入其他变量? C++ - Why is my code not allowing me to input values into a string variable with other user inputs for other variables? C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM