Platypus
Lifer
Hey:
My teacher is being such a hardass about this assignment, and he wants us to use arrays. I have NO idea what an array is or how it works other than it uses [] operators and it can handle alot of data. I have included a cpp that I wrote last week in about 40 min for a program scheme called "BASIC". Its a stupid program, but I have to replace the reference parameters "aka cp0,cp1" and "b_part, a_part" with arrays by tommorow at this time. I really really really could use some help with this, any help at all would be appreciated greatly. I apoligize for my lack of programming skill, and since im new to this language, please dont make fun of me 🙂
Sorry for the length...
Here is the file:
**************
#define NORANDOM_CHECK
#define NOSEE_RANDOM
#define NOBOTTLE_CHECK
#define NODEBUG_INPUT
#define NOCAP_CHECK
#define NOCHECK_CAP
#define NOREPORT_DEBUG
#define NOREAD_CHECK
//**************************************************************************
//
// Program: B A S I C Relief for Programmers
// Contest Giveaway Simulation
//
// Description: The DEBUG company which markets BASIC, Relief for
Programmers
// have decided on a promotional campaign. Bottles of
BASIC
// will have a letter under the cap. Programmers who
collect
// all five letters, 'B', 'A', 'S', 'I', and 'C' thereby
// spelling out the product name will win either a
million Dollars
// or a date with their fantasy sex symbol. To avoid
giving away
// too much in prizes, the relative frequency of the
letters
// are being regulated by the following percentages:
//
// The name of the product as well as the relative
distribution
// of the cap letters will be read from an external data
file
//
// This program will simulate the process of buying
bottles
// through the use of pseudo-random numbers generated by
the
// computer.
//
// Mailbox: debug
//
//**************************************************************************
#include <iostream.h> // want cout/cerr
#include <iomanip.h> // need setw(), setiosflags
#include <assert.h> // functions need protection
#include <ctype.h> // need toupper function
#include <stdlib.h> // random number stuff
#include <time.h> // to seed the random number generator
#include <apstring.h> // words
#include <fstream.h> // to read the percenages
const int MAX_RAND = 10000; // Could be considered the number of
bottles
// in the promotional.
const int COL_SIZE = 8; // Columns of the output table
const int PRODUCT_NAME_LENGTH = 5; // Only allowing for 5 letters in
a name
typedef int Counter_Array[PRODUCT_NAME_LENGTH]
typedef double Percent_Array[PRODUCT_NAME_LENGTH-1]
//**************************************************************************
//
// Function seed_random
//
// Purpose: "seeds" the pseudorandom number generator with a value
// from the system clock
//
// Parameters: none
//
// Examples: Not Applicable, the "results" of this function are
internal
//
//**************************************************************************
void seed_random();
//**************************************************************************
//
// Function: get_random
//
// Purpose: returns a pseudorandom integer value through and
including
// the two parameter values. This function assumes that
the
// pseudo-generator has been previously "seeded".
//
// Parameters: int low -> smallest or lowest value desired
// int high -> largest or highest value desired
//
// Examples: get_random(0,1) returns either 0 or 1
// get_random(1,6) returns 1,2,3,4,5, or 6
// get_random(1,100) returns values between 1 and 100
// get_random(6,1) -> assert failed (low < high)
//
//**************************************************************************
int get_random(int low, int high);
//**************************************************************************
//
// Function: report_header
//
// Purpose: Displays the Report's Title and Column Headings
//
// Parameters: apstring name -> name of the product
//
// Examples:
// Results for "BASIC"
//
// Letter Count Percent
// --------------------------------------
//
//**************************************************************************
void report_header(apstring name);
//**************************************************************************
//
// Function: report_line
//
// Purpose: Outputs a single line of the results
//
// Parameters: apstring label -> a letter of the product's name
// int count -> number of "caps" of the letter
accumulated
// int allcount-> total number of bottles "bought"
//
// Examples: report_line('B',40,200) -> B 40 20
// report_line('A',40,200) -> A 40 20
// report_line('S',80,200) -> S 80 40
// report_line('I',39,200) -> I 39 19
// report_line('C', 1,200) -> C 1 1
//
//**************************************************************************
void report_line( apstring label, int count, int allcount);
//***************************************************************************
//
// Function : centered
//
// Purpose: positions a text string in the "middle" of an
apstring
// of spaces. if the text string is longer than the
field
// size, no centering takes place and only the text is
// returned.
//
// Parameter: int field -> Size of desired apstring with the
centered text
// apstring text -> message to be centered
//
1234567890123456789012345678901234567890
// Example: 10,"HELLO" => " HELLO "
// 5,"A" => " A "
// 11,"HELLO" => " HELLO "
// 10,"HELP" => " HELP "
// 3,"HELLO => "HELLO"
//
//***************************************************************************
apstring centered(int field, apstring text);
//**************************************************************************
//
// Function: buy_bottles
//
// Purpose: Updates the appropriate "cap" counter depending on the
value
// of cap_let parameter.
//
//
// Parameters: char cap_let -> one of the five letters spelling out
the
// apstring product-> product's name
// int cp0 -> counter of the first letter in
product name
// int cp1 -> counter of the second letter in
product name
// int cp2 -> counter of the third letter in
product name
// int cp3 -> counter of the fourth letter in
product name
// int cp4 -> counter of the fifth letter in
product name
// All five counters are assumed to be properly initialized
//
// Examples: Results
// buy_bottles('B', "BASIC", 0, 0, 0, 0,0) -> cp0++
// buy_bottles('A', "BASIC", 0, 0, 0, 0,0) -> cp1++
// buy_bottles('S', "BASIC", 0, 0, 0, 0,0) -> cp2++
// buy_bottles('I', "BASIC", 0, 0, 0, 0,0) -> cp3++
// buy_bottles('C', "BASIC", 0, 0, 0, 0,0) -> cp4++
//
//**************************************************************************
void buy_bottles(char cap_let, apstring product,
int& cp0, int& cp1,int& cp2,int& cp3,int& cp4);
//**************************************************************************
//
// Function: cap_letter
//
// Purpose: "converts" an integer into an appropriate letter
//
// Parameters: int num -> any positive integer between 1 and
MAX_RAND
// apstring product -> set of letters to accumulate
// double b_part -> relative number of
// double a_part -> each letter in the product name
// double s_part -> as planned by the "contest"
// doulbe i_part -> promoters.
//
// Examples: cap_letter( 1, "BASIC", 20,20,40,19) -> 'B'
// cap_letter( 21, "BASIC", 20,20,40,49) -> 'A'
// cap_letter( 41, "BASIC", 20,20,40,19) -> 'S'
// cap_letter( 81, "BASIC", 20,20,40,19) -> 'I'
// cap_letter(100, "BASIC", 20,20,40,19) -> 'C'
//
//**************************************************************************
char cap_letter(int num, apstring product,
double b_part,double a_part,double s_part,double
i_part);
//***************************************************************************
//
// Function : learn_percents
//
// Purpose: reads the name of the product as well as information
about
// the relativc distribution of the cap letters
//
// Parameters: apstring in_filename -> name of file with data to
learn
// apstring product-> Letters to accumulate
//
+-----------------------+
// double& b_part -> First letter | Relative
|
// double& a_part -> Second letter | planned
distribution |
// double& s_part -> Third letter | of the various
|
// double& i_part -> Fourth letter | letters on the
caps |
//
+-----------------------+
// Example: producnt -> BASIC
// b_part -> 20.0%
// a_part -> 20.0%
// s_part -> 40.0%
// i_part -> 19.0%
//
//***************************************************************************
void learn_percents(apstring in_filename, apstring &product,
double& b_part,double& a_part,double&
s_part,double&i_part);
//***************************************************************************
//
// Function : open_input_file
//
// Purpose: Opens an ifsream for reading from a file,
// Prompts user for a filename if none supplied
//
// Parameter: ifstream &inf - The file stream opened
// apstring fname -> name of file. If this is a NULL
string,
// the name of the file will be
prompted for
//
// Example: N/A, The function will repeat until a file can be
// successfully opened
//
//***************************************************************************
void open_input_file(ifstream& inf, apstring& fname);
void open_input_file(ifstream& inf, apstring& fname)
{
#ifdef OFILE
cerr << "open_input_file function entered." << endl;
#endif
if (fname.length() < 1)
{
cerr << "Enter the name of the file: ";
cin >> fname;
} // asking for the filename
inf.open(fname.c_str() );
if ( inf.fail() )
{
cerr << "WARNING! " << fname <<" could not be opened."<< endl;
assert (!inf.fail() );
} // file could not be opened
} // open_input_file
int main()
{
ifstream in_file; // file access buffer
apstring in_filename =""; // name of data file's name
int cap0 = 0; // counters for the number
int cap1 = 0; // of each cap "letter"
int cap2 = 0; // accumulated as the simulation
int cap3 = 0; // of the "purchase" of bottles
int cap4 = 0; // "Hardest" cap because lowest
availability
apstring product; // set of letters that must be collected
double b_part; // The relative distribution
double a_part; // of the letters in the product
double s_part; // that will come from an external
double i_part; // data file.
char letter; // a letter of the product's name
learn_percents(in_filename, product,b_part,a_part,s_part,i_part);
assert(product.length() == PRODUCT_NAME_LENGTH);
assert( b_part+a_part+s_part+i_part < 100.0 );
int newcap; // a simulated cap
seed_random();
while (cap0*cap1*cap2*cap3*cap4 == 0)
{
newcap = get_random(1,MAX_RAND);
letter = cap_letter(newcap, product,
b_part,a_part,s_part,i_part);
#ifdef SEE_RANDOM
cerr << "Random value (" << setw(COL_SIZE)
<<(cap0+cap1+cap2+cap3+cap4+1)
<<"😉 is " << setw(COL_SIZE) << newcap << "😉.\n";
#endif
buy_bottles( letter, product, cap0, cap1,cap2,cap3,cap4);
} // wasting money on bottles
report_header(product);
report_line(product.substr(0,1), cap0,cap0+cap1+cap2+cap3+cap4);
report_line(product.substr(1,1), cap1,cap0+cap1+cap2+cap3+cap4);
report_line(product.substr(2,1), cap2,cap0+cap1+cap2+cap3+cap4);
report_line(product.substr(3,1), cap3,cap0+cap1+cap2+cap3+cap4);
report_line(product.substr(4,1), cap4,cap0+cap1+cap2+cap3+cap4);
report_line("Total",cap0+cap1+cap2+cap3+cap4,cap0+cap1+cap2+cap3+cap4);
// Plop,Plop,Fizz,Fizz, Oh! What a relief it is?
return 0;
} // main
void seed_random()
{
#ifdef RANDOM_CHECK
cerr << "seed_random entered." << endl;
#endif
// no assert needed here
time_t seconds; // a value from the system clock
time(&seconds);
srand( (unsigned int) seconds);
} // seed_random
int get_random(int low, int high)
{
#ifdef RANDOM_CHECK
cerr << "get_random entered with low = (" << low << "😉 and high = ("
<< high << "😉." << endl;
#endif
assert ( low < high);
return rand() % (high-low+1) + low;
} // get_random
void report_header(apstring name)
{
#ifdef REPORT_DEBUG
cerr << "report_header entered with \"" << name << "\".\n";
#endif
assert (name.length() > 0);
cout << endl; // Blank line before title
cout << centered(3*COL_SIZE, "Results for "+name) << endl;
cout << endl; // Blank line after title
cout << centered(COL_SIZE,"Letter"😉
<< centered(COL_SIZE,"Count"😉
<< centered(COL_SIZE,"Percent"😉
<< endl;
cout << setw(3*COL_SIZE) << setfill('-') << '-' << setfill(' ') <<
endl;
} // report_header
void report_line( apstring label, int count, int allcount)
{
#ifdef REPORT_DEBUG
cerr << "report_line entered with label = \"" << letter
<< "\" count = (" << count << "😉 and allcount =(" << allcount <<
"😉.\n";
#endif
cout <<
setiosflags(ios::right)<<setiosflags(ios::fixed)<<setprecision(1);
cout << setw(COL_SIZE/2) << label
<< setw(COL_SIZE) << count;
if ( count != allcount)
cout << setw(COL_SIZE) << (double) count / allcount * 100.0;
cout << endl;
} // report_line
apstring centered(int field, apstring text)
{
#ifdef DEBUG_CENTER
cerr << "centered entered with field = " << field
<< " and text = \"" << text << "\""
// << " which is " << text.length() << " chars"
<< endl;
#endif
assert(field>0);
assert(text.length() > 0);
apstring ctext = " "; // One space to start
if ( text.length() >= field)
return text;
if ( (field-text.length()) % 2 )
return centered( field, text+ctext);
else
return centered((field-text.length())/2, ctext)
+ text +
centered((field-text.length())/2, ctext);
} // centered
void buy_bottles(char cap_let, apstring product,
int& cp0, int& cp1,int& cp2,int& cp3,int& cp4)
{
#ifdef BOTTLE_CHECK
cerr << "buy_bottles: cap_let= \'" << cap_let << "\'"
<< setw(COL_SIZE/2) << "cp0=(" << setw(COL_SIZE/2) << cp0 << "😉"
<< setw(COL_SIZE/2) << "cp1=(" << setw(COL_SIZE/2) << cp1 << "😉"
<< setw(COL_SIZE/2) << "cp2=(" << setw(COL_SIZE/2) << cp2 << "😉"
<< setw(COL_SIZE/2) << "cp3=(" << setw(COL_SIZE/2) << cp3 << "😉"
<< setw(COL_SIZE/2) << "cp4=(" << setw(COL_SIZE/2) << cp4 <<
"😉\n";
#endif
assert (product.length() == PRODUCT_NAME_LENGTH);
if ( cap_let == product[0] )
cp0++;
else if ( cap_let == product[1] )
cp1++;
else if ( cap_let == product[2] )
cp2++;
else if ( cap_let == product[3] )
cp3++;
else
cp4++;
} // buy_bottles
char cap_letter(int num,apstring product,
double b_part,double a_part,double s_part,double
i_part)
{
#ifdef CAP_CHECK
cerr << "cap_letter entered with num = (" << num <<"😉.\n";
#endif
#ifdef READ_CHECK
cerr << "b_part = (" << b_part <<"😉 a_part = (" << a_part
<< "😉 s_part = (" << s_part << "😉 and i_part = (" << i_part
<<"😉.\n";
#endif
assert(num>0 && num <= MAX_RAND );
assert(product.length() == PRODUCT_NAME_LENGTH );
char letter; // value to be returned, this value is temporary
if ( num > (b_part+a_part+s_part+i_part)/100 * MAX_RAND )
letter = product[product.length()-1];
else if ( num > (b_part+a_part+s_part)/100 * MAX_RAND )
letter = product[product.length()-2];
else if ( num > (b_part+a_part)/100 * MAX_RAND )
letter = product[product.length()-3];
else if ( num > (b_part)/100 * MAX_RAND )
letter = product[product.length()-4];
else
letter = product[product.length()-5];
#ifdef CHECK_CAP
cerr << "cap_letter returning a \'" << letter << "\'.\n";
#endif
return letter;
} // cap_letter
void learn_percents(apstring in_filename, apstring& product,
double& b_part,double& a_part,double&
s_part,double&i_part)
{
#ifdef READ_CHECK
cerr << "learn_percents entered." << endl;
#endif
ifstream inf; // file buffer to read percents
from
open_input_file(inf,in_filename.c_str() );
if ( ! inf.fail() )
{
inf >> product;
inf >> b_part;
inf >> a_part;
inf >> s_part;
inf >> i_part;
inf.close();
} // reading the perecntages
else
{
cerr << "Warning! Data not available." << endl;
assert ( inf.fail() );
} // data not available
} // learn_percents
Thanks so much!
Andrew
andrew@teenviolence.org
My teacher is being such a hardass about this assignment, and he wants us to use arrays. I have NO idea what an array is or how it works other than it uses [] operators and it can handle alot of data. I have included a cpp that I wrote last week in about 40 min for a program scheme called "BASIC". Its a stupid program, but I have to replace the reference parameters "aka cp0,cp1" and "b_part, a_part" with arrays by tommorow at this time. I really really really could use some help with this, any help at all would be appreciated greatly. I apoligize for my lack of programming skill, and since im new to this language, please dont make fun of me 🙂
Sorry for the length...
Here is the file:
**************
#define NORANDOM_CHECK
#define NOSEE_RANDOM
#define NOBOTTLE_CHECK
#define NODEBUG_INPUT
#define NOCAP_CHECK
#define NOCHECK_CAP
#define NOREPORT_DEBUG
#define NOREAD_CHECK
//**************************************************************************
//
// Program: B A S I C Relief for Programmers
// Contest Giveaway Simulation
//
// Description: The DEBUG company which markets BASIC, Relief for
Programmers
// have decided on a promotional campaign. Bottles of
BASIC
// will have a letter under the cap. Programmers who
collect
// all five letters, 'B', 'A', 'S', 'I', and 'C' thereby
// spelling out the product name will win either a
million Dollars
// or a date with their fantasy sex symbol. To avoid
giving away
// too much in prizes, the relative frequency of the
letters
// are being regulated by the following percentages:
//
// The name of the product as well as the relative
distribution
// of the cap letters will be read from an external data
file
//
// This program will simulate the process of buying
bottles
// through the use of pseudo-random numbers generated by
the
// computer.
//
// Mailbox: debug
//
//**************************************************************************
#include <iostream.h> // want cout/cerr
#include <iomanip.h> // need setw(), setiosflags
#include <assert.h> // functions need protection
#include <ctype.h> // need toupper function
#include <stdlib.h> // random number stuff
#include <time.h> // to seed the random number generator
#include <apstring.h> // words
#include <fstream.h> // to read the percenages
const int MAX_RAND = 10000; // Could be considered the number of
bottles
// in the promotional.
const int COL_SIZE = 8; // Columns of the output table
const int PRODUCT_NAME_LENGTH = 5; // Only allowing for 5 letters in
a name
typedef int Counter_Array[PRODUCT_NAME_LENGTH]
typedef double Percent_Array[PRODUCT_NAME_LENGTH-1]
//**************************************************************************
//
// Function seed_random
//
// Purpose: "seeds" the pseudorandom number generator with a value
// from the system clock
//
// Parameters: none
//
// Examples: Not Applicable, the "results" of this function are
internal
//
//**************************************************************************
void seed_random();
//**************************************************************************
//
// Function: get_random
//
// Purpose: returns a pseudorandom integer value through and
including
// the two parameter values. This function assumes that
the
// pseudo-generator has been previously "seeded".
//
// Parameters: int low -> smallest or lowest value desired
// int high -> largest or highest value desired
//
// Examples: get_random(0,1) returns either 0 or 1
// get_random(1,6) returns 1,2,3,4,5, or 6
// get_random(1,100) returns values between 1 and 100
// get_random(6,1) -> assert failed (low < high)
//
//**************************************************************************
int get_random(int low, int high);
//**************************************************************************
//
// Function: report_header
//
// Purpose: Displays the Report's Title and Column Headings
//
// Parameters: apstring name -> name of the product
//
// Examples:
// Results for "BASIC"
//
// Letter Count Percent
// --------------------------------------
//
//**************************************************************************
void report_header(apstring name);
//**************************************************************************
//
// Function: report_line
//
// Purpose: Outputs a single line of the results
//
// Parameters: apstring label -> a letter of the product's name
// int count -> number of "caps" of the letter
accumulated
// int allcount-> total number of bottles "bought"
//
// Examples: report_line('B',40,200) -> B 40 20
// report_line('A',40,200) -> A 40 20
// report_line('S',80,200) -> S 80 40
// report_line('I',39,200) -> I 39 19
// report_line('C', 1,200) -> C 1 1
//
//**************************************************************************
void report_line( apstring label, int count, int allcount);
//***************************************************************************
//
// Function : centered
//
// Purpose: positions a text string in the "middle" of an
apstring
// of spaces. if the text string is longer than the
field
// size, no centering takes place and only the text is
// returned.
//
// Parameter: int field -> Size of desired apstring with the
centered text
// apstring text -> message to be centered
//
1234567890123456789012345678901234567890
// Example: 10,"HELLO" => " HELLO "
// 5,"A" => " A "
// 11,"HELLO" => " HELLO "
// 10,"HELP" => " HELP "
// 3,"HELLO => "HELLO"
//
//***************************************************************************
apstring centered(int field, apstring text);
//**************************************************************************
//
// Function: buy_bottles
//
// Purpose: Updates the appropriate "cap" counter depending on the
value
// of cap_let parameter.
//
//
// Parameters: char cap_let -> one of the five letters spelling out
the
// apstring product-> product's name
// int cp0 -> counter of the first letter in
product name
// int cp1 -> counter of the second letter in
product name
// int cp2 -> counter of the third letter in
product name
// int cp3 -> counter of the fourth letter in
product name
// int cp4 -> counter of the fifth letter in
product name
// All five counters are assumed to be properly initialized
//
// Examples: Results
// buy_bottles('B', "BASIC", 0, 0, 0, 0,0) -> cp0++
// buy_bottles('A', "BASIC", 0, 0, 0, 0,0) -> cp1++
// buy_bottles('S', "BASIC", 0, 0, 0, 0,0) -> cp2++
// buy_bottles('I', "BASIC", 0, 0, 0, 0,0) -> cp3++
// buy_bottles('C', "BASIC", 0, 0, 0, 0,0) -> cp4++
//
//**************************************************************************
void buy_bottles(char cap_let, apstring product,
int& cp0, int& cp1,int& cp2,int& cp3,int& cp4);
//**************************************************************************
//
// Function: cap_letter
//
// Purpose: "converts" an integer into an appropriate letter
//
// Parameters: int num -> any positive integer between 1 and
MAX_RAND
// apstring product -> set of letters to accumulate
// double b_part -> relative number of
// double a_part -> each letter in the product name
// double s_part -> as planned by the "contest"
// doulbe i_part -> promoters.
//
// Examples: cap_letter( 1, "BASIC", 20,20,40,19) -> 'B'
// cap_letter( 21, "BASIC", 20,20,40,49) -> 'A'
// cap_letter( 41, "BASIC", 20,20,40,19) -> 'S'
// cap_letter( 81, "BASIC", 20,20,40,19) -> 'I'
// cap_letter(100, "BASIC", 20,20,40,19) -> 'C'
//
//**************************************************************************
char cap_letter(int num, apstring product,
double b_part,double a_part,double s_part,double
i_part);
//***************************************************************************
//
// Function : learn_percents
//
// Purpose: reads the name of the product as well as information
about
// the relativc distribution of the cap letters
//
// Parameters: apstring in_filename -> name of file with data to
learn
// apstring product-> Letters to accumulate
//
+-----------------------+
// double& b_part -> First letter | Relative
|
// double& a_part -> Second letter | planned
distribution |
// double& s_part -> Third letter | of the various
|
// double& i_part -> Fourth letter | letters on the
caps |
//
+-----------------------+
// Example: producnt -> BASIC
// b_part -> 20.0%
// a_part -> 20.0%
// s_part -> 40.0%
// i_part -> 19.0%
//
//***************************************************************************
void learn_percents(apstring in_filename, apstring &product,
double& b_part,double& a_part,double&
s_part,double&i_part);
//***************************************************************************
//
// Function : open_input_file
//
// Purpose: Opens an ifsream for reading from a file,
// Prompts user for a filename if none supplied
//
// Parameter: ifstream &inf - The file stream opened
// apstring fname -> name of file. If this is a NULL
string,
// the name of the file will be
prompted for
//
// Example: N/A, The function will repeat until a file can be
// successfully opened
//
//***************************************************************************
void open_input_file(ifstream& inf, apstring& fname);
void open_input_file(ifstream& inf, apstring& fname)
{
#ifdef OFILE
cerr << "open_input_file function entered." << endl;
#endif
if (fname.length() < 1)
{
cerr << "Enter the name of the file: ";
cin >> fname;
} // asking for the filename
inf.open(fname.c_str() );
if ( inf.fail() )
{
cerr << "WARNING! " << fname <<" could not be opened."<< endl;
assert (!inf.fail() );
} // file could not be opened
} // open_input_file
int main()
{
ifstream in_file; // file access buffer
apstring in_filename =""; // name of data file's name
int cap0 = 0; // counters for the number
int cap1 = 0; // of each cap "letter"
int cap2 = 0; // accumulated as the simulation
int cap3 = 0; // of the "purchase" of bottles
int cap4 = 0; // "Hardest" cap because lowest
availability
apstring product; // set of letters that must be collected
double b_part; // The relative distribution
double a_part; // of the letters in the product
double s_part; // that will come from an external
double i_part; // data file.
char letter; // a letter of the product's name
learn_percents(in_filename, product,b_part,a_part,s_part,i_part);
assert(product.length() == PRODUCT_NAME_LENGTH);
assert( b_part+a_part+s_part+i_part < 100.0 );
int newcap; // a simulated cap
seed_random();
while (cap0*cap1*cap2*cap3*cap4 == 0)
{
newcap = get_random(1,MAX_RAND);
letter = cap_letter(newcap, product,
b_part,a_part,s_part,i_part);
#ifdef SEE_RANDOM
cerr << "Random value (" << setw(COL_SIZE)
<<(cap0+cap1+cap2+cap3+cap4+1)
<<"😉 is " << setw(COL_SIZE) << newcap << "😉.\n";
#endif
buy_bottles( letter, product, cap0, cap1,cap2,cap3,cap4);
} // wasting money on bottles
report_header(product);
report_line(product.substr(0,1), cap0,cap0+cap1+cap2+cap3+cap4);
report_line(product.substr(1,1), cap1,cap0+cap1+cap2+cap3+cap4);
report_line(product.substr(2,1), cap2,cap0+cap1+cap2+cap3+cap4);
report_line(product.substr(3,1), cap3,cap0+cap1+cap2+cap3+cap4);
report_line(product.substr(4,1), cap4,cap0+cap1+cap2+cap3+cap4);
report_line("Total",cap0+cap1+cap2+cap3+cap4,cap0+cap1+cap2+cap3+cap4);
// Plop,Plop,Fizz,Fizz, Oh! What a relief it is?
return 0;
} // main
void seed_random()
{
#ifdef RANDOM_CHECK
cerr << "seed_random entered." << endl;
#endif
// no assert needed here
time_t seconds; // a value from the system clock
time(&seconds);
srand( (unsigned int) seconds);
} // seed_random
int get_random(int low, int high)
{
#ifdef RANDOM_CHECK
cerr << "get_random entered with low = (" << low << "😉 and high = ("
<< high << "😉." << endl;
#endif
assert ( low < high);
return rand() % (high-low+1) + low;
} // get_random
void report_header(apstring name)
{
#ifdef REPORT_DEBUG
cerr << "report_header entered with \"" << name << "\".\n";
#endif
assert (name.length() > 0);
cout << endl; // Blank line before title
cout << centered(3*COL_SIZE, "Results for "+name) << endl;
cout << endl; // Blank line after title
cout << centered(COL_SIZE,"Letter"😉
<< centered(COL_SIZE,"Count"😉
<< centered(COL_SIZE,"Percent"😉
<< endl;
cout << setw(3*COL_SIZE) << setfill('-') << '-' << setfill(' ') <<
endl;
} // report_header
void report_line( apstring label, int count, int allcount)
{
#ifdef REPORT_DEBUG
cerr << "report_line entered with label = \"" << letter
<< "\" count = (" << count << "😉 and allcount =(" << allcount <<
"😉.\n";
#endif
cout <<
setiosflags(ios::right)<<setiosflags(ios::fixed)<<setprecision(1);
cout << setw(COL_SIZE/2) << label
<< setw(COL_SIZE) << count;
if ( count != allcount)
cout << setw(COL_SIZE) << (double) count / allcount * 100.0;
cout << endl;
} // report_line
apstring centered(int field, apstring text)
{
#ifdef DEBUG_CENTER
cerr << "centered entered with field = " << field
<< " and text = \"" << text << "\""
// << " which is " << text.length() << " chars"
<< endl;
#endif
assert(field>0);
assert(text.length() > 0);
apstring ctext = " "; // One space to start
if ( text.length() >= field)
return text;
if ( (field-text.length()) % 2 )
return centered( field, text+ctext);
else
return centered((field-text.length())/2, ctext)
+ text +
centered((field-text.length())/2, ctext);
} // centered
void buy_bottles(char cap_let, apstring product,
int& cp0, int& cp1,int& cp2,int& cp3,int& cp4)
{
#ifdef BOTTLE_CHECK
cerr << "buy_bottles: cap_let= \'" << cap_let << "\'"
<< setw(COL_SIZE/2) << "cp0=(" << setw(COL_SIZE/2) << cp0 << "😉"
<< setw(COL_SIZE/2) << "cp1=(" << setw(COL_SIZE/2) << cp1 << "😉"
<< setw(COL_SIZE/2) << "cp2=(" << setw(COL_SIZE/2) << cp2 << "😉"
<< setw(COL_SIZE/2) << "cp3=(" << setw(COL_SIZE/2) << cp3 << "😉"
<< setw(COL_SIZE/2) << "cp4=(" << setw(COL_SIZE/2) << cp4 <<
"😉\n";
#endif
assert (product.length() == PRODUCT_NAME_LENGTH);
if ( cap_let == product[0] )
cp0++;
else if ( cap_let == product[1] )
cp1++;
else if ( cap_let == product[2] )
cp2++;
else if ( cap_let == product[3] )
cp3++;
else
cp4++;
} // buy_bottles
char cap_letter(int num,apstring product,
double b_part,double a_part,double s_part,double
i_part)
{
#ifdef CAP_CHECK
cerr << "cap_letter entered with num = (" << num <<"😉.\n";
#endif
#ifdef READ_CHECK
cerr << "b_part = (" << b_part <<"😉 a_part = (" << a_part
<< "😉 s_part = (" << s_part << "😉 and i_part = (" << i_part
<<"😉.\n";
#endif
assert(num>0 && num <= MAX_RAND );
assert(product.length() == PRODUCT_NAME_LENGTH );
char letter; // value to be returned, this value is temporary
if ( num > (b_part+a_part+s_part+i_part)/100 * MAX_RAND )
letter = product[product.length()-1];
else if ( num > (b_part+a_part+s_part)/100 * MAX_RAND )
letter = product[product.length()-2];
else if ( num > (b_part+a_part)/100 * MAX_RAND )
letter = product[product.length()-3];
else if ( num > (b_part)/100 * MAX_RAND )
letter = product[product.length()-4];
else
letter = product[product.length()-5];
#ifdef CHECK_CAP
cerr << "cap_letter returning a \'" << letter << "\'.\n";
#endif
return letter;
} // cap_letter
void learn_percents(apstring in_filename, apstring& product,
double& b_part,double& a_part,double&
s_part,double&i_part)
{
#ifdef READ_CHECK
cerr << "learn_percents entered." << endl;
#endif
ifstream inf; // file buffer to read percents
from
open_input_file(inf,in_filename.c_str() );
if ( ! inf.fail() )
{
inf >> product;
inf >> b_part;
inf >> a_part;
inf >> s_part;
inf >> i_part;
inf.close();
} // reading the perecntages
else
{
cerr << "Warning! Data not available." << endl;
assert ( inf.fail() );
} // data not available
} // learn_percents
Thanks so much!
Andrew
andrew@teenviolence.org