JacosteRaider
Joined: 18 Jan 2010 Posts: 479
|
re: c++ Fun
by Jacoste on 2010/10/24 15:24
My proff. loves to use functions, so I use them mainly to keep him happy.
|
|
|
VireneMember
Joined: 21 Jan 2010 Posts: 77
|
re: c++ Fun
by Virene on 2010/10/24 15:35
well, here's a clean version of the same program in 16 lines counting white spaces and looking pretty.
try it, then ask questions for any part of this you dont understand (or why i choose to do it this way instead of some other way)
- Code: Select all
#include <iostream>
using namespace std;
int main()
{
int numorig;
cout << "Welcome to the square/cube program." << endl << "Please enter a number from 1-5 (any other number to quit): ";
cin >> numorig;
while (numorig >= 1 && numorig <= 5) {
cout << "Your number squared: " << numorig*numorig << endl
<< "Your number cubed: " << numorig*numorig*numorig << endl
<< "Please enter a number from 1-5 (any other number to quit): ";
cin >> numorig;
}
}
output looks like
- Code: Select all
r000@ubuntu:~/stuff$ ./a.out
Welcome to the square/cube program.
Please enter a number from 1-5 (any other number to quit): 3
Your number squared: 9
Your number cubed: 27
Please enter a number from 1-5 (any other number to quit): 1
Your number squared: 1
Your number cubed: 1
Please enter a number from 1-5 (any other number to quit): 2
Your number squared: 4
Your number cubed: 8
Please enter a number from 1-5 (any other number to quit): 3
Your number squared: 9
Your number cubed: 27
Please enter a number from 1-5 (any other number to quit): 4
Your number squared: 16
Your number cubed: 64
Please enter a number from 1-5 (any other number to quit): 5
Your number squared: 25
Your number cubed: 125
Please enter a number from 1-5 (any other number to quit): 6
r000@ubuntu:~/stuff$
_________________
|
|
JacosteRaider
Joined: 18 Jan 2010 Posts: 479
|
re: c++ Fun
by Jacoste on 2010/10/24 15:55
While I get what you are doing, and in the end its written better, the way I did my program was exercising the stuff from class that I know guarantees me a solid grade.
|
|
Zeere
Joined: 19 Jul 2008 Posts:
|
re: c++ Fun
by Zeere on 2010/10/24 22:38
man I dunno what you did wrong with entering my code to get a bunch of errors...I just dropped it into my compiler and I got two...screwed up a variable name and had a 't' just sitting around.
_________________ rub a dub dub three nuns in a tub and they're doing it with the garden gnome
|
|
JacosteRaider
Joined: 18 Jan 2010 Posts: 479
|
re: c++ Fun
by Jacoste on 2011/02/20 14:24
Thought someone might enjoy this. Its comparing a simulation of comparing peoples birthdays in the room versus the mathematical probability. So birthday paradox simulation vs birthday paradox formula, and it really shows how weak the RNG of the function is . :
(5000 trials of the simulation btw)
Quote: |
#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;
void compare ( int& , int );
int birthdayboy[50];
int counter;
int main()
{
double tempcount;
double perdif;
double actprob;
int i;
int np;
cout << "Welcome to the birthday paradox program! This program calculates the chance of a person having the same birthdays in the room (m/d) between 2 and 50. \n";
cout << "Number of People | Oberserved Probability | Actual Probability | Percent Difference \n";
for (np=2; np < 51; ++np)
{
for (i=0, counter=0; i<5000; ++i)
{
srand ((unsigned int)time(0)*i);
compare(counter, np);
}
tempcount = (((double)counter)/((double)np*(double)i));
actprob = 1.0 - pow((364.0/365.0), (np*(np-1.0)/2.0));
perdif = abs ( actprob - tempcount) / actprob *100;
cout << np << " | " << tempcount*100 << " | " << actprob*100 << " | " << perdif << endl;
}
return 0;
}
void compare (int& counter, int np)
{
int j;
int h;
int k;
for (k=0; k<np; ++k)
{
birthdayboy[k]=rand() % 365+1;
}
for(j=0; j<np; ++j)
{
for(h=1; h<np; ++h)
{
if(j!=h)
{
if (birthdayboy[j] == birthdayboy[h])
++counter;
}
}
}
}
|
Sample of the output:
Quote: | Welcome to the birthday paradox program! This program calculates the chance of a person having the same birthdays in the room (m/d) between 2 and 50.
Number of People | Oberserved Probability | Actual Probability | Percent Differe
nce
2 | 0.17 | 0.273973 | 37.95
3 | 0.353333 | 0.819668 | 56.8931
4 | 0.61 | 1.63262 | 62.6367
5 | 0.868 | 2.70619 | 67.9254
6 | 1.14333 | 4.0317 | 71.6414
7 | 1.47429 | 5.5985 | 73.6664
8 | 1.6825 | 7.39413 | 77.2454
9 | 1.91333 | 9.40449 | 79.6551
10 | 2.2 | 11.614 | 81.0574
11 | 2.49455 | 14.0059 | 82.1893
12 | 2.79833 | 16.5623 | 83.1042
13 | 3.13077 | 19.2645 | 83.7485
14 | 3.42429 | 22.0932 | 84.5007
15 | 3.656 | 25.0288 | 85.3928
16 | 3.945 | 28.0514 | 85.9365
17 | 4.19294 | 31.1413 | 86.5358
18 | 4.46333 | 34.2791 | 86.9794
19 | 4.65789 | 37.4458 | 87.561
20 | 4.943 | 40.6229 | 87.832
21 | 5.18762 | 43.7932 | 88.1543
22 | 5.45455 | 46.9399 | 88.3797
23 | 5.73043 | 50.0477 | 88.5501
24 | 5.98917 | 53.1023 | 88.7215
25 | 6.2408 | 56.0908 | 88.8738
26 | 6.55538 | 59.0014 | 88.8894
27 | 6.78667 | 61.824 | 89.0226
28 | 7.10071 | 64.5497 | 88.9996
29 | 7.38897 | 67.1709 | 88.9998
30 | 7.68733 | 69.6816 | 88.9679
31 | 7.95677 | 72.077 | 88.9607
32 | 8.22125 | 74.3537 | 88.943
33 | 8.48242 | 76.5092 | 88.9132
34 | 8.77235 | 78.5425 | 88.8311
35 | 9.108 | 80.4535 | 88.6792
36 | 9.39167 | 82.2431 | 88.5806
37 | 9.64216 | 83.913 | 88.5093
38 | 9.90368 | 85.4659 | 88.4121
39 | 10.1605 | 86.9048 | 88.3085
40 | 10.419 | 88.2336 | 88.1916
41 | 10.6776 | 89.4565 | 88.064
42 | 10.9395 | 90.5782 | 87.9226
43 | 11.2135 | 91.6036 | 87.7587
44 | 11.5832 | 92.5379 | 87.4828
45 | 11.8551 | 93.3865 | 87.3053
46 | 12.1083 | 94.1546 | 87.14
47 | 12.383 | 94.8476 | 86.9443
48 | 12.6438 | 95.4709 | 86.7564
49 | 12.9147 | 96.0297 | 86.5514
50 | 13.2024 | 96.5291 | 86.3229
Press any key to continue . . .
|
|
|
|