This is a challenge i got from a book when i was learning Python, i had fun with it so i thought i would share
| Welcome | |
|---|---|
|
Welcome to the forums of AntiRTFM's Absolute N00b Spoonfeed C++ Tutorials! You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. Registration is fast, simple, and absolutely free, so please, <a href="/profile.php?mode=register">join our community today</a>! |
|
#include "stdafx.h"
#include <time.h>
#include <iostream>
#include <stdlib.h>
#include <sstream>
using namespace std;
void main()
{
srand(time(0));
char input;
while (1>0)
{
int heads = 0, tails = 0;
cout<<"\nBegin? y or n";
cin>>input;
if( input == 'n' || input == 'N')
{
break;
}
if(input == 'y' || input == 'Y')
{
for(int i=0; i<=49; i++)
{
int x = rand() % 2 +1;
if(x == 1)
{
heads++;
cout<<" heads \n";
}
else{
tails++;
cout<<" tails \n";
}
} // end for loop
}
cout<<"\nTotal number of Heads: "<<heads;
cout<<"\nTotal number of Tails: "<<tails;
} //end of while
}

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int heads = 0;
int tails = 0;
for (int i = 0;i < 100;i++)
{
int randNum = rand()%2;
if (randNum == 1)
{
heads++;
}
else if (randNum == 0)
{
tails++;
}
}
cout << heads << endl;
cout << tails << endl;
}
#include "stdafx.h"
#include <iostream>
#include <time.h>
using namespace std;
void coin(){
cout<<"Lets flip a coin 100 times... ";
int head;
head = 0;
int tale;
tale = 0;
for(int x = 0;x <= 100;x++){
int random;
random = rand()% +2;
if (random == 1)
{
head++;
}
else
{
tale++;
}
}
{
cout<<"Done!\n\n";
cout<<"Head came: "<<head<<endl;
cout<<"Tale came: "<<tale<<endl;
if (head >tale)
{
cout<<"I guess head von with "<<head<<" times\n\n";
}
else
{
cout<<"I guess tale von with "<<tale<<" times\n\n";
}
}
cout<<"Again? (Y)es/(N)o \n";
char nextTry;
cin>>nextTry;
if (nextTry == 'y' || nextTry == 'Y')
{
coin();
}
else
{
cout<<"Thanks for playing!";
}
}
int main(){
srand(time(NULL));
coin();
return 0;
}
#include <iostream>
using namespace std;
int main(){
int counter;
bool flip;
srand(time(NULL));
for(counter = 0; counter < 100; counter++){
flip = rand() % 2;
if(flip == 0){
cout << "Heads\n";
}
else if(flip == 1){
cout << "Tails\n";
}
}
system("pause");
return 0;
}#include<iostream>
using namespace std;
int main()
{
int head,tail,x,c;
head=tail=0;
srand(time(0));
for(int i=0;i<100;i++)
{
x=rand()%2+1;
switch(x){
case 1:
head++;
cout<<"Head!\n";
break;
case 2:
tail++;
cout<<"Tail!\n";
break;
}
}
cout<<"\nHeads: "<<head;
cout<<"\nTails: "<<tail<<endl;
cout<<"Flip again?1-Yes,2-No ";
cin>>c;
switch(c){
case 1:
main();
break;
case 2:
return 0;
default:
return 0;
}
}#include <iostream>
#include <cmath>
using namespace std;
int main(){
char resp;
bool redo = true;
int coinheads=0,cointales=0;
while(redo){
for(int i=0; i<100; i++){
int time = rand()%2;
while(time == 0){
cointales++;
break;
}
while(time == 1 ){
coinheads++;
break;
}
}
cout << "\ncoin landed on tales " << cointales << " times ";
cout << "\ncoin landed on heads " << coinheads << " times \n";
cointales = 0 , coinheads = 0;
cout << "\n Do it again? \t (y) Yes or (n) No\n" << endl;
cin >> resp;
redo = (resp == 'y') ? true : false;
} //while(redo)
system("pause");
} //mainUsers browsing this forum: No registered users and 0 guests