/* Koen Noens * september 2003 * * multopler choice tool; probability calculation, * * what is the probability to achieve a given score by randomly answering * a given number of questions, each with a given number of multiple choice * answers. * */ /* TO DO check casting and/or find a way to use larger numbers */ #include //functies void showLogo(); void showIntro(); void doBlankLines(unsigned char n); void getInitData(unsigned int &n, unsigned int &a, unsigned int &p, unsigned int &t, float &f); unsigned int NumberCorrectAnswersNeeded(unsigned int n, unsigned int p, unsigned int t, float f); float FairPunishment(unsigned int t, unsigned int a); long double power(float a, unsigned int b); long frac(unsigned int a); long frac(unsigned int a, unsigned int b); long combinations(unsigned int set, unsigned int subset); long double cumulprob(unsigned int n, unsigned int k, unsigned int a); long double binomialprob(unsigned int n, unsigned int k, unsigned int a); void getDecision(unsigned char &c); void main() { //variables unsigned int n=0; //number of questions unsigned int a=0; //number of options per question (multiple choice) unsigned int p=0; //points needed to pass te test unsigned int t=0; //points to be earned with a correct answer float f=0; //points to subtract for a wrong answer unsigned int c=0; //number of correct answers given by student unsigned int w=0; //number of wrong answers given by student unsigned int k=0; //number of questions that need to be answered correctly in order to pass this test unsigned char decision = 'Y' ; //decision to continue or not while(decision == 'Y') { cout << "decision : "<< decision << endl; showLogo(); getInitData(n,a,p,t,f); k=NumberCorrectAnswersNeeded(n, p, t, f); cout << "gambling student needs to guess at least "<< k <<" questions correctly\n"; cout <<"\n\nThere is a probability of " << (float) cumulprob(n,k,a) << " % " << endl << " to pass this test on pure guess work" << endl << endl << " wanna risk it ... ?" << endl << endl; //continue? doBlankLines(4); getDecision(decision); }; //wait to exit cin.get(); }; long combinations(unsigned int set, unsigned int subset) {/* number of combinations of a subset in a set */ //return ( frac(set) / ( frac(subset) * frac(set - subset) ) ) ; return frac(set, subset); } long double binomprob(unsigned int n, unsigned int k, unsigned int a) {//binomial probabolity, result in % long double result; long double bpcoeff; long double p; long double q; bpcoeff = frac(n) / ( frac(k) * frac(n-k) ) ; p = float(1)/a; q = (float)(a-1)/a; p = power(p,k); q = power(q,(n-k)); result = bpcoeff * p * q * 100; // resultaat in % return result; } long double cumulprob(unsigned int n, unsigned int k, unsigned int a) { /* calculate of combinations of a subset in a set * given that for set choose subset, * set choose subset+1, subset+2 etc need to be considered as well, * until subset+n = set */ long double result = 0.0; while (k <= n) { result = result + binomprob (n, k, a ) ; k++; }; return result; } unsigned int NumberCorrectAnswersNeeded(unsigned int n, unsigned int p, unsigned int t, float f) {//calculate how many correct answers you need in order to pass this test, float temp; unsigned int result; temp = (p + (n*f)) / (t + f); result = int(temp); //round up, but only if there are decimals if((temp - result)!=0) { result += 1; } return result; } float FairPunishment(unsigned int t, unsigned int a) { return ( (float)t /(a-1) ); } void getInitData(unsigned int &n, unsigned int &a, unsigned int &p, unsigned int &t, float &f) { do { cout <<"number of questions : "; cin >> n; if(n>15) //frac functie overflow when > 15 { cout << "this number may be too large for the program to function correctly.\n Try a number smaller than 16\n"; } } while(n>15); cout << "multiple choice : how many answers to a question ? : "; cin >> a; cout <<"points earned by correct answer : "; cin >> t; cout << "total points for this test : " << n*t << endl; cout <<"points needed to pass : "; cin >> p; cout <<"points subtracted per wrong answer (suggestion : " <> f; //random solutions cout << "\nThis test has " << (long)power((float)a,n) << " possible solutions.\n\n"; } long double power(float a, unsigned int b) {//returns a ^ b. no negative exponents allowed long double result=1; //unsigned power for(unsigned int i=0; i> c; //uppercase A-Z only : ascii 65-90 if(c>90) { c -=32; //from a = 97 to A = 65 etc. }; if(c<65 || c>90) { cout << "invalid choice. Choose Y / N \n"; }; }while(c<65 || c>90); //screen layout doBlankLines(65); } void showIntro() { cout << "Multiple Choice Tool\n\nCalculate the probability of passing a test by answering randomly to a given number of multiple choice questions\n\n" ; } void showLogo() { cout <<"\n\n\t\t\tSilly Software Productions\n\n\t\t\t\t-=oOo=-\n\n"; showIntro(); } void doBlankLines(unsigned char n) { // do n blank lines for(int i = 0;i<=n;i++) {cout << "\n"; } }