0

Test inputs and outputs from Xcode (incorrect) and other (web based compilers) are as following:

Input 1: 3 30 0.90 40

Xcode Output 1: Acceptable success rate: 0.90
Average attempted sales: 30.00
Average completed sales: 19.33
Observed success rate: 0.64

Observed success rate not in acceptable range.
Program ended with exit code: 0

Other Compilers' Output 1(correct): Acceptable success rate: 0.90
Average attempted sales: 30.00
Average completed sales: 19.67
Observed success rate: 0.66

Observed success rate not in acceptable range.

Input 2: 10 100 0.5 1

Xcode Output 2: Acceptable success rate: 0.50
Average attempted sales: 100.00
Average completed sales: 29.40
Observed success rate: 0.29

Observed success rate not in acceptable range.
Program ended with exit code: 0

Other Compilers' Output 1(correct): Acceptable success rate: 0.50
Average attempted sales: 100.00
Average completed sales: 28.80
Observed success rate: 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;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    Welcome to StackOverflow! Could you clarify why you think Xcode behaves differently? – Botje Feb 05 '19 at 06:46
  • I say the code behaves differently because the correct or the expected output is what I should get, but I get a slightly off output. No pattern recognized – Yash Patel Feb 06 '19 at 07:14

1 Answers1

1

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).

Xcode uses Clang, which defaults to libc++, while your other compilers might use GCC, which uses libstdc++. The Microsoft Visual Studio compiler has its own implementation as well. 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. That should yield the same results across platforms.

Botje
  • 26,269
  • 3
  • 31
  • 41
  • Agreed. If the user is complaining about floating point rounding errors (which are large in this case) they should at least use a fixed set of input, rather than leaving it to `rand()` to generate. – trojanfoe Feb 05 '19 at 08:19
  • Yes, the "incorrect" output is what Xcode outputs with the same seed. I tried several different web-based and IDE compilers and they all output the expected or "correct" values Also, thank you for the recommendation @Botje I think for this specific application, applying a fix might be an overkill since it will be eventually run in different environment but now I at least know to use more than 1 compiler if my outputs are off after hours of debugging – Yash Patel Feb 06 '19 at 07:21