hello guys,
this is a program which is a part od my college work. the follwing the is the question which was given to me.
1. Write a program that simulates a coin flipping game that uses 10 randomly generated flips of a coin. Every time heads comes up, the player wins £2; every time tails comes up, the player loses £1. Write a function calculateTotal that adds up the player’s winnings after 10 flips of the coin. Display the results of each coin flip and the total winnings/losses for each game.
Code:
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
void calculateTotal(int []); // fucntion calculateTotal prototype which takes one integer array and returns nothing
const int FLIPS=10; // Initializing Flips at globle scope
int main()
{
int coin_flip[10],i;
int choice;
for(;;)
{
system("clear");
cout<<"1. Generate Coin Flip"<<endl; //show the menu
cout<<"2. Exit"<<endl;
cout<<endl<<endl<<"Enter your choice [ 1 - 2 ]"<<endl;
cin>>choice; // get the choice
srand(time(0)); // set the srand
switch(choice)
{
case 1:
{
for(i=0;i<FLIPS;i++)
coin_flip[i]=rand()%2; // generating the 10 coin flips and store it in the coin_flip
calculateTotal(coin_flip); // sending the base address of coin_flip array
cout<<"Press any key to continue...";
cin.get();
break;
}
case 2:
{
cout<<endl<<endl<<"GOOD BYE, Have a nice day!!!"<<endl;
cin.get();
exit(0); // exit successfully
}
default: // optional
{
cout<<endl<<endl<<"Invalid Entry"<<endl; //on invalid display choice display error
cout<<"Press any key to continue...";
cin.get();
break;
}
}
}
}
void calculateTotal(int flip_result[])
{
int i;
int total=0;
char nogames[][10]={"One ", // Initializing two dimensional array
"Two ",
"Three ",
"Four ",
"Five ",
"Six ",
"Seven ",
"Eight ",
"Nine ",
"Ten "
};
cout<<endl<<endl;
for(i=0;i<FLIPS;i++)
{
if(flip_result[i]==1) //check it is head
{
cout<<"Game "<<nogames[i]<<setw(5)<<" you got Head "<<setw(6)<<" won 2 pounds"<<endl;
total+=2; // on head add 2 pounds to total
}
else if(flip_result[i]==0) //check for tail
{
cout<<"Game "<<nogames[i]<<setw(5)<<" you got Tail "<<setw(6)<<" lost 1 pounds "<<endl;
total-=1; // on tail subtract 1 pound from total
}
}
if(total>0) // check if total is greater than 0
cout<<endl<<endl<<"You won "<<total<<" pounds !!!"<<endl;
else
cout<<endl<<endl<<"Sorry you dont have any money to take home"<<endl;
}
2. Define a structure for date with the following fields:
day as an integer, month as a string, and year as an integer.
Write a function isequal that will take two date structure parameters and will return true IF the parameters have the same values for day, month and year fields or false IF any field differs. Write a program to test the function, and supply a Test Plan showing test input, reason for test, expected outcome, actual outcome and an indication of whether the test was successful.
Code:
#include <iostream>
#include <iomanip>
#include <cstring>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
struct date // Skeletion of the datatype date ( user defined datatype)
{
int day; // members names & members types
char month[15];
int year;
};
bool isequal(const date *,const date *); // function prototype for isequal which takes two pointer of
// type date a and returns of tupe boolean
bool checkmonth(char []); // function prototype for checkmonth which takes one integer array of type int as
// a parametre
int main()
{
date o1, o2; // declaring variables of type date
system("clear");
cout<<"Enter date in DD MM YY format ( 14 January/Jan 2005) "<<endl;
cout<<"Enter date 1"<<endl;
cin>>o1.day>>o1.month>>o1.year;
while((o1.day<1 || o1.day>31) || checkmonth(o1.month) || (o1.year<=1000 || o1.year>=2050)) // check for valid day,
//year and sending the month to checkmonth as a actual parameter
{
cout<<endl<<endl;
if(o1.day<1 || o1.day>31) // check for day is valid
cout<<"Check out the DAY, Invalid!!!"<<endl;
if(checkmonth(o1.month)) //check for month is correct by sending the o1.month
//to fucniotn check month
cout<<"Check out the MONTH, Invalid!!!"<<endl;
if((o1.year<=1000 || o1.year>=2050)) // check for year is valid
cout<<"Check out the YEAR, Invalid!!!"<<endl;
cout<<endl<<"Press any key to continue..."<<endl;
getachar();
cout<<"Enter date 1"<<endl;
cin>>o1.day>>o1.month>>o1.year; // on invalid date get the valid date
}
cout<<endl<<endl<<"Enter date 2"<<endl;
cin>>o2.day>>o2.month>>o2.year; // get the date two using o2
while((o2.day<1 || o2.day>31) || checkmonth(o2.month) || (o2.year<=1000 || o2.year>=2050)) // check for valid day,
//year and sending the month to checkmonth as a actual parameter
{
cout<<endl<<endl;
if(o2.day<1 || o2.day>31) // check for day is valid
cout<<"Check out the DAY, Invalid!!!"<<endl;
if(checkmonth(o2.month)) // check for month is valid by sending the o1.month to checkmonth fucntion
cout<<"Check out the MONTH, Invalid!!!"<<endl;
if((o2.year<=1000 || o2.year>=2050)) // check for year is valid
cout<<"Check out the YEAR, Invalid!!!"<<endl;
cout<<endl<<"Press any key to continue..."<<endl;
cin.get();
cout<<"Enter date 2"<<endl;
cin>>o2.day>>o2.month>>o2.year;
}
if(isequal(&o1,&o2)) // check both the dates are equal by sending the address of both the variables
// o1 and o2
cout<<endl<<endl<<"Both the dates are equal!!!"<<endl; // on true display equal
else
cout<<endl<<endl<<"Check... || Dates are not equal ||"<<endl; // on false display not equal
cin.get();
return 0;
}
bool isequal(const date *o1, const date *o2)
{
if(((o1)->day==(o2)->day) && (strncmp(o1->month,o2->month,3)==0) && (o1->year==o2->year)) // check for both the dates are equal
return true; // on equal retrue true
else
return false; // on not equal return false
}
bool checkmonth(char month[])
{
char mon[][23]={"January","Febuary","March","April","May","June","July","August","September","October","Novenber","December",
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
// declare and initialze the character array
if((int)month[0] > 90) // check for is the first letters of month is lower case
month[0] -= 32; // on lower changer it to upper case
for(int i=0;i<24;i++)
{
if(strcmp(month,mon[i])==0 || (atoi(month)>=1 && atoi(month)<=12)) // compare if month and mon[i] is equal
{
return 0; // on equal return false
}
}
return 1; // on not equal return true
}
3. Write program that takes a real number and separates it into three parts: a sign (+, - or blank), a whole number magnitude, and a fractional part, and then prints this out. Typical output from the program would be:
Enter a value to analyse: 35.817
Parts of 35.817
sign: +
whole number magnitude: 35
fractional part: 0.8170
The main function will prompt the user and obtain the number. A function separate is to be used to perform the separation, and a function printOut is to be used to display the 3 parts. Both functions have void return types. You will need to pay particular attention to the type of parameters for the functions. Within the separate function you may also wish to use the library functions floor and fabs, where floor returns the largest whole number not greater than the argument passed to it, and fabs calculates the absolute value of a floating point number. Provide a Test Plan for the program.
Code:
#include<iostream>
#include<iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
using std::setprecision;
using std::showpoint;
using std::fixed;
void separate(double , char *, long int *, double *); // prototype of fucniotn seperate which takes 4 parameters including the original floating point number
void printOut(double ,char , long int, double ); // prototype of function pintout which takes 4 parameter
int main()
{
double number_to_seperate;
char sign_part;
long int integer_part;
double decimal_part;
std::string badChars;
system("clear");
cout<<"Enter a floating point number to sepearte its parts\n-->";
while(!(cin>>number_to_seperate)) // get the floating point number and check if it is a valid data
{
cout<<"Invalid data"<<endl<<endl;
cin.clear(); // clear the input buffer
cin >> badChars; // get the invalid data from the input buffer
cout << "You typed \" " << badChars << " \" instead of a floating point number." << endl;
cout << "Please enter floating point number" << endl;
}
separate(number_to_seperate,&sign_part, &integer_part,&decimal_part); // calling seperate function with sending the actual value of number_to_sperate and address
//of all others variables
printOut(number_to_seperate,sign_part, integer_part,decimal_part); // calling printout function to print the result
cin.get();
return 0;
}
void separate(double num, char *sign, long int *integer, double *decimal)
{
*integer= (int)(num); // getting the integer part by type casting the actual number
if(num>=0) // checking if number is greater than 0
*sign='+'; // if number is greater than 0 assign '+' to sign
else if(num<0)
*sign='-'; // else assign '-' to sign
*decimal=num-floor(*integer); // subtracting the original double ing number by the integer value
}
void printOut(double num,char sign, long int integer, double decimal)
{
cout<<endl<<endl<<endl<<"Parts of "<<setprecision(4)<<fixed<<num<<" are as follow: - "<<endl; // output actual double ing number
cout<<"\t\t\t\t"<<"Sign -> "<<sign<<endl; // output sign
cout<<"\t\t\t\t"<<"Whole number Magnitude -> "<<integer<<endl; // output Interger part
cout<<"\t\t\t\t"<<"The decimal part ->"<<showpoint << setprecision(4) << decimal<<endl;
}
the above all compiles fine with no error. can u pleas ehelp me out with giving me some comments on the program so that i can improve with that.
any commenst will be appriciated
thax very much in advance