Monday 15 July 2013

C++ probability calculating program works when bookmarked in Visual Studio but does not work when run without a bookmark -



C++ probability calculating program works when bookmarked in Visual Studio but does not work when run without a bookmark -

before details code allow me explain issue. i'm using microsoft visual studio 2013 compile , run code. code executes when bookmark line function calculates probability. when don't bookmark line or bookmark lines main function programme doesn't work. doesn't work mean gives probability either 100% or 0% no matter how many times run it. reply 50% probability should close 50%. (when simulated 300 times while bookmarked gave 42%) seems issue memory related. i'm cs pupil yet guessing @ point. type of feedback or solution appreciated.

let me explain code suppose do. friend asked me probability question. not able solve using type of logic create easy solve. decided create little programme calculate odds of simulating given number of time. here question.

"there 100 seats on plane. first passenger has lost ticket. doesn't know seat his. sits randomly. after person comes plane 1 one. if seat empty sit down designated seats. if seat occupied sit down randomly. probability of lastly person beingness able sit down on designated seat?"

here of code comments on how works.

#include <stdio.h> #include <stdlib.h> #include <iostream> #include <iomanip> #include <time.h> #include <string.h> #include <string> #define planecapacity 100 //plane capacity changeable. helped me find number irrelevant reply of question using namespace std; int placepeople(int simnumber); int simnumber; int seat; int personno; int successno = 0; int seats[planecapacity]; int latestseat; int main(){ double probability; cout << "please come in number of times want simulation run:" << endl; cin >> simnumber; placepeople(simnumber); // function calculation probability = (successno * 100) / (simnumber); //calculate probability in precentage form cout << "probability: "; cout << probability << endl; system("pause"); homecoming exit_success; //exit success...i wish :( } int placepeople(int simnumber){ (int = 0; < simnumber; a++){ //number of simulations. code doesn't work when bookmark line seats[planecapacity] = { 0 }; //array elements corresponds plane seats. code works when bookmark line point onward. line included. personno = 1; //last person sit down downwards latestseat = 0; //the seat lastly person sit down on srand(time(null)); seat = rand() % planecapacity + 1; //randomizer determines first passanger sit down if (seat == 1){ //if sits in place golden! successno++; } if (1 < seat && seat < planecapacity){ //if doesn't sit down in place or lastly passengers place things gets bit messy seats[seat - 1] = personno; //put him seat e.g 45th seat (int b = 1; b < seat - 1; b++){ //everybody until seat (43 people, 2 44) sits in regular place. 45th person has no sit down :( personno++; seats[personno - 1] = personno; } personno++; latestseat = personno; while (personno < planecapacity){ //the same process first passanger repeated until lastly person seated seat = rand() % (planecapacity - latestseat + 1) + latestseat; //i tried lower random number interval code work little more efficiently if (seat == latestseat){ //the first guys seat might still empty. interval 1 bigger should be. successno++; //i place him when random number says 1. create interval shorter place break; //next guy first seat when random number generator gives latest seat number } //so in illustration if random number 45 place 45th guy 1st seat. 1 time first seat occupied seats[seat - 1] = personno; //we guarenteed have lastly guy sit down in place code can exit after , increment success counter (int b = latestseat; b < seat - 1; b++){ personno++; seats[personno - 1] = personno; } personno++; latestseat = personno; } } } homecoming successno; //return number of succesfull attepmts main function }

thanks time!

to follow on comment, move srand phone call main prior phone call placepeople(). when don't break debugger, think function executing fast plenty iterations of same random number. verify this, seek printing out value of seat , see if distributed randomly on available seats or not.

c++ visual-studio-2013

No comments:

Post a Comment