• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Really, really, screwed. C++ programmers please help this poor soul.

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: &quot;seeds&quot; the pseudorandom number generator with a value
// from the system clock
//
// Parameters: none
//
// Examples: Not Applicable, the &quot;results&quot; 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 &quot;seeded&quot;.
//
// 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 &quot;BASIC&quot;
//
// 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 &quot;caps&quot; of the letter
accumulated
// int allcount-> total number of bottles &quot;bought&quot;
//
// 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 &quot;middle&quot; 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,&quot;HELLO&quot; => &quot; HELLO &quot;
// 5,&quot;A&quot; => &quot; A &quot;
// 11,&quot;HELLO&quot; => &quot; HELLO &quot;
// 10,&quot;HELP&quot; => &quot; HELP &quot;
// 3,&quot;HELLO => &quot;HELLO&quot;
//
//***************************************************************************
apstring centered(int field, apstring text);

//**************************************************************************
//
// Function: buy_bottles
//
// Purpose: Updates the appropriate &quot;cap&quot; 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', &quot;BASIC&quot;, 0, 0, 0, 0,0) -> cp0++
// buy_bottles('A', &quot;BASIC&quot;, 0, 0, 0, 0,0) -> cp1++
// buy_bottles('S', &quot;BASIC&quot;, 0, 0, 0, 0,0) -> cp2++
// buy_bottles('I', &quot;BASIC&quot;, 0, 0, 0, 0,0) -> cp3++
// buy_bottles('C', &quot;BASIC&quot;, 0, 0, 0, 0,0) -> cp4++
//
//**************************************************************************
void buy_bottles(char cap_let, apstring product,
int&amp; cp0, int&amp; cp1,int&amp; cp2,int&amp; cp3,int&amp; cp4);

//**************************************************************************
//
// Function: cap_letter
//
// Purpose: &quot;converts&quot; 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 &quot;contest&quot;
// doulbe i_part -> promoters.
//
// Examples: cap_letter( 1, &quot;BASIC&quot;, 20,20,40,19) -> 'B'
// cap_letter( 21, &quot;BASIC&quot;, 20,20,40,49) -> 'A'
// cap_letter( 41, &quot;BASIC&quot;, 20,20,40,19) -> 'S'
// cap_letter( 81, &quot;BASIC&quot;, 20,20,40,19) -> 'I'
// cap_letter(100, &quot;BASIC&quot;, 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&amp; b_part -> First letter | Relative
|
// double&amp; a_part -> Second letter | planned
distribution |
// double&amp; s_part -> Third letter | of the various
|
// double&amp; 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 &amp;product,
double&amp; b_part,double&amp; a_part,double&amp;
s_part,double&amp;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 &amp;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&amp; inf, apstring&amp; fname);
void open_input_file(ifstream&amp; inf, apstring&amp; fname)
{
#ifdef OFILE
cerr << &quot;open_input_file function entered.&quot; << endl;
#endif

if (fname.length() < 1)
{
cerr << &quot;Enter the name of the file: &quot;;
cin >> fname;
} // asking for the filename

inf.open(fname.c_str() );

if ( inf.fail() )
{
cerr << &quot;WARNING! &quot; << fname <<&quot; could not be opened.&quot;<< endl;
assert (!inf.fail() );
} // file could not be opened

} // open_input_file


int main()
{
ifstream in_file; // file access buffer
apstring in_filename =&quot;&quot;; // name of data file's name
int cap0 = 0; // counters for the number
int cap1 = 0; // of each cap &quot;letter&quot;
int cap2 = 0; // accumulated as the simulation
int cap3 = 0; // of the &quot;purchase&quot; of bottles
int cap4 = 0; // &quot;Hardest&quot; 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 << &quot;Random value (&quot; << setw(COL_SIZE)
<<(cap0+cap1+cap2+cap3+cap4+1)
<<&quot😉 is &quot; << setw(COL_SIZE) << newcap << &quot😉.\n&quot;;
#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(&quot;Total&quot;,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 << &quot;seed_random entered.&quot; << endl;
#endif

// no assert needed here

time_t seconds; // a value from the system clock
time(&amp;seconds);
srand( (unsigned int) seconds);
} // seed_random
int get_random(int low, int high)
{
#ifdef RANDOM_CHECK
cerr << &quot;get_random entered with low = (&quot; << low << &quot😉 and high = (&quot;
<< high << &quot😉.&quot; << endl;
#endif

assert ( low < high);

return rand() % (high-low+1) + low;
} // get_random

void report_header(apstring name)
{
#ifdef REPORT_DEBUG
cerr << &quot;report_header entered with \&quot;&quot; << name << &quot;\&quot;.\n&quot;;
#endif

assert (name.length() > 0);

cout << endl; // Blank line before title
cout << centered(3*COL_SIZE, &quot;Results for &quot;+name) << endl;
cout << endl; // Blank line after title
cout << centered(COL_SIZE,&quot;Letter&quot😉
<< centered(COL_SIZE,&quot;Count&quot😉
<< centered(COL_SIZE,&quot;Percent&quot😉
<< endl;
cout << setw(3*COL_SIZE) << setfill('-') << '-' << setfill(' ') <<
endl;

} // report_header
void report_line( apstring label, int count, int allcount)
{
#ifdef REPORT_DEBUG
cerr << &quot;report_line entered with label = \&quot;&quot; << letter
<< &quot;\&quot; count = (&quot; << count << &quot😉 and allcount =(&quot; << allcount <<
&quot😉.\n&quot;;
#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 << &quot;centered entered with field = &quot; << field
<< &quot; and text = \&quot;&quot; << text << &quot;\&quot;&quot;
// << &quot; which is &quot; << text.length() << &quot; chars&quot;
<< endl;
#endif

assert(field>0);
assert(text.length() > 0);

apstring ctext = &quot; &quot;; // 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&amp; cp0, int&amp; cp1,int&amp; cp2,int&amp; cp3,int&amp; cp4)
{
#ifdef BOTTLE_CHECK
cerr << &quot;buy_bottles: cap_let= \'&quot; << cap_let << &quot;\'&quot;
<< setw(COL_SIZE/2) << &quot;cp0=(&quot; << setw(COL_SIZE/2) << cp0 << &quot😉&quot;
<< setw(COL_SIZE/2) << &quot;cp1=(&quot; << setw(COL_SIZE/2) << cp1 << &quot😉&quot;
<< setw(COL_SIZE/2) << &quot;cp2=(&quot; << setw(COL_SIZE/2) << cp2 << &quot😉&quot;
<< setw(COL_SIZE/2) << &quot;cp3=(&quot; << setw(COL_SIZE/2) << cp3 << &quot😉&quot;
<< setw(COL_SIZE/2) << &quot;cp4=(&quot; << setw(COL_SIZE/2) << cp4 <<
&quot😉\n&quot;;
#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 << &quot;cap_letter entered with num = (&quot; << num <<&quot😉.\n&quot;;
#endif

#ifdef READ_CHECK
cerr << &quot;b_part = (&quot; << b_part <<&quot😉 a_part = (&quot; << a_part
<< &quot😉 s_part = (&quot; << s_part << &quot😉 and i_part = (&quot; << i_part
<<&quot😉.\n&quot;;
#endif
assert(num>0 &amp;&amp; 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 << &quot;cap_letter returning a \'&quot; << letter << &quot;\'.\n&quot;;
#endif

return letter;
} // cap_letter

void learn_percents(apstring in_filename, apstring&amp; product,
double&amp; b_part,double&amp; a_part,double&amp;
s_part,double&amp;i_part)
{
#ifdef READ_CHECK
cerr << &quot;learn_percents entered.&quot; << 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 << &quot;Warning! Data not available.&quot; << endl;
assert ( inf.fail() );
} // data not available

} // learn_percents



Thanks so much!
Andrew

andrew@teenviolence.org
 
Using variables like cp0, cp1, cp2, etc are the very purpose of arrays in the first place. Instead, you could do:

int cp[3];

then use cp[0] for cp0, and cp[1] for cp1, etc. Same effect. An array is just a &quot;list&quot; of multiple variables of the same type.. and you use the [] operator to index (slice out) a single element of that array.
 
OK that kind of makes sense... where does the &quot;list command&quot; come into play? Is there anyway you could fill in the main with what you just told me so I can study from that? Thanks so much for replying
 
Array is quite simple.
You can find almost all (probably all) C/C++ books discuss
about array.

Here is a simple example to show how to use array.
This example has several items and compute the sum of
the items (this is a very stripped down code, so
that you can understand it easily).
I'll contrast an example without array and with array.
I don't want to use your example since your code is too long.

EXAMPLE #1 without array:

int main()
{
int item0, item1, item2;
int total;

item0 = 0;
item1 = 1;
item2 = 2;
total = item0 + item1 + item2;
}

EXAMPLE #2 with array:

int main()
{
int item[3]; // declare an array of items with 3 elements
int total;

item[0] = 0;
item[1] = 1;
item[2] = 2;

total = item[0] + item[1] + item[2];
}

Pretty simple eh? The code looks identical, so why do we
want to use array? Why bother with more techniques to learn?
The power of array comes when you have LOTS of items.

Suppose you have 1000 items, each initialized from 0 to 999.
Without using array, you have to create 1000 variables and
initialize them one by one.

Using array, you have this simple code (I assumed that
you have learned looping technique before):

int main()
{
int i;
int item[1000]; // create an array of 1000 items
int total = 0;

// initialize the items.
// We only need 2 lines, instead of 1000 lines of code
for (i=0;i<1000;i++)
{
item[ i ] = i;
}

// compute the total.
// You don't have to type all the items one by one if
// you use array.
for (i=0; i<1000;i++)
{
total += item[ i ];
}

}


Are you convinced yet that array is pretty powerful?

edit: sorry this message board won't let me display
indexing array correctly.
 
Yes, I see now, but I still dont know how I can fix my program... I am hopelessly lost with this 🙁

I need to use these two arrays:

&quot;Counter_Array&quot;
&quot;Percent_Array&quot;

I know not why my teacher is an idiot and is making us use an array with only 5 varibles.. but you never know.

Anyone else please?
 
I am guessing that the list command you are thinking of is the list from STL (standard template library). I am not too sure how it is implemented but it is a dynamic linked list which can grow and shrink, much like a vector... not to sure though, I use vectors a lot but not lists.
 
Thanks weezer, I would really appreciate it if someone could re-write main with the array, and I can fill in the rest myself. Thanks for trying 🙂
 
I don't quite understand what your program does, but...

Instead of:
int cap0=0;
int cap1=0;

you can use:
int cap[5]=0; //creates an array of size 5, and initializes each to 0.

you can then reference each cap using cap[2]+=1 if you want to increment, instead of cap2++.

I don't know what the percent counter of your program does. Sorry.
 


<<
while (int i=0; i <5; i++)
{
Counter_Array=0;
}
//int cap0 = 0; // counters for the number
//int cap1 = 0; // of each cap &quot;letter&quot;
//int cap2 = 0; // accumulated as the simulation
//int cap3 = 0; // of the &quot;purchase&quot; of bottles
//int cap4 = 0; // &quot;Hardest&quot; cap because lowest

>>



so wherever u have cap0 use counter_array[0] instead...etc...
 
Doofy:

Without compiling its hard to understand, sorry.
Basically it seeds a random generator that picks random numbers and then it runs until it has at least one of B,A,S,I,C. Then the percent counter adds it up and makes a table showing the percent of each cap.
 
just do the same for percent_counter. set percent_counter[0] to be the same as part_b or whatevers, and make percent_counter[1] to be part_a...etc...and substitute.
 
1. for your own sanity create an enum type that holds that indices to both arrays, let's say

enum E_CAP_LETTERS { B, A, S, I, C };

2. define the array size

#define CAP_SIZE 5

3. now declare both values

int cap[CAP_SIZE];
int part[CAP_SIZE];


these get mapped to your original values as follows

cap0 - cap[.B] ( ignore the dot, i had to put it due to page parsings )
cap1 - cap[A]
cap2 - cap you get the idea

4. now you can replace all of the occurences of capN ( N = 0 -> CAP_SIZE - 1 ) according to the mapping above.

5. if you have any functions that take those values as seperate arguments ( NOT AS A SUM of ONE ARGUMENT ), you should change them accordigly to take advantage of the arrays implementation.

6. above applies as well for the percent vars

7. if you do a lot of short repetitive procedures ( like summing ), it's always a good idea to utilize macros

#define CAP_SUM(cap) (cap[.B] + cap[A] + cap + cap[.I] + cap[C])

every occurece of cap cap[.B] + cap[A] + cap + cap[.I] + cap[C] in the code should be then replaced by CAP_SUM(cap).
 
If I get what you're saying correctly, I think this program can be made with simple loops without using a lot of functions. This compiles, but the results aren't extravagant. This program assumes that every cap has some letter on it. I suppose the weights of each letter can be adjusted easily.



#include <iostream.h>
#include <stdlib.h>


int main()
{

unsigned seed;
int cap[5];
int percentages[5];
char basic[]=&quot;BASIC&quot;;
cout << &quot;enter seed: &quot;;
cin >> seed;
srand(seed);


for (int h=0;h<5;h++)
cap[.H]=0; //remove the .

for (int i=0; i < 10000; i++)
{
cap[ rand() % 5 ] += 1;
if(cap[0]*cap[1]*cap[2]*cap[3]*cap[4] != 0)
break;
}
for (int k=0;k<5;k++)
percentages[k]=(cap[k]*100)/i+1;

for (int j=0;j<5;j++)
{cout << &quot;The number of &quot; << basic[j] << &quot;'s is: &quot; << cap[j] << endl;
cout << &quot;The percentage of &quot; << basic[j] << &quot;'s is: &quot; << percentages[j] << endl;
}

return 0;
}
 
Thanks doofy, and thanks alot higherground, I just got it compile and this pos works! You guys are so awesome, I cant describe how greatful I am right now 😀
 
Back
Top