You are on page 1of 17

1.

Here is a header file:


// golf.h for pe9-1.cpp
const int Len = 40;
struct golf
{
char fullname[Len];
int handicap;
};
// non-interactive version:
// function sets golf structure to provided name, handicap
// using values passed as arguments to the function
void setgolf(golf & g, const char * name, int hc);
// interactive version:
// function solicits name and handicap from user
// and sets the members of g to the values entered
// returns 1 if name is entered, 0 if name is empty string
int setgolf(golf & g);
// function resets handicap to new value
void handicap(golf & g, int hc);
// function displays contents of golf structure
void showgolf(const golf & g);
Chapter 9 MEMORY MODELS AND NAMESPACES 441
Note that setgolf() is overloaded. Using the first version of setgolf() would look like
this:
golf ann;
setgolf(ann, Ann Birdfree, 24);
The function call provides the information thats stored in the ann structure. Using the
second version of setgolf() would look like this:
golf andy;
setgolf(andy);
The function would prompt the user to enter the name and handicap and store them in
the andy structure. This function could (but doesnt need to) use the first version internally.
Put together a multifile program based on this header. One file, named golf.cpp, should
provide suitable function definitions to match the prototypes in the header file. A second
file should contain main() and demonstrate all the features of the prototyped functions.
For example, a loop should solicit input for an array of golf structures and terminate
when the array is full or the user enters an empty string for the golfers name. The
main() function should use only the prototyped functions to access the golf structures.
My answer:
golf.h
1 #ifndef GOLF_H_
2 #define GOLF_H_
3
4 const int Len = 40;

5 struct golf
6 {
7 char fullname[Len];
8 int handicap;
9 };
10// non-interactive version:
11// function sets golf structure to provided name, handicap
12// using values passed as arguments to the function
13void setgolf(golf & g, const char * name, int hc);
14// interactive version:
15// function solicits name and handicap from user
16// and sets the members of g to the values entered
17// returns 1 if name is entered, 0 if name is empty string
18int setgolf(golf & g);
19// function resets handicap to new value
20void handicap(golf & g, int hc);
21// function displays contents of golf structure
22void showgolf(const golf & g);
23
24#endif
golf.cpp
1 #include <iostream>
2 #include "golf.h"
3
4 // non-interactive version:
5 // function sets golf structure to provided name, handicap
6 // using values passed as arguments to the function
7 void setgolf(golf & g, const char * name, int hc)
8 {
9

strcpy(g.fullname,name);

10 g.handicap = hc;

11}
12// interactive version:
13// function solicits name and handicap from user
14// and sets the members of g to the values entered
15// returns 1 if name is entered, 0 if name is empty string
16int setgolf(golf & g)
17{
18 using namespace std;
19 cout << "\nEnter golfer's name: ";
20 cin.getline(g.fullname,Len);
21 if (g.fullname[0] =='\0')
22

return 0;

23
24 cout << "\nEnter handicap number: ";
25 cin >> g.handicap;
26 return 1;
27}
28
29// function resets handicap to new value
30void handicap(golf & g, int hc)
31{
32 g.handicap = hc;
33}
34// function displays contents of golf structure
35void showgolf(const golf & g)
36{
37 using namespace std;
38 cout << "\n\nDisplaying Golf structure:";
39 cout << "\nGolfer's name is " << g.fullname;
40 cout << "\nGolfer's handicap # is " << g.handicap;
41}

cp9ex1.cpp
1 #include <iostream>
2 #include "golf.h"
3
4 int main()
5 {
6
7

using namespace std;

8
9

golf ann;

10 setgolf(ann, "Ann Birdfree", 24);


11
12 golf andy;
13 cout << setgolf(andy);
14
15 showgolf(ann);
16 showgolf(andy);
17
18 handicap(andy,3);
19 handicap(ann,6);
20
21 cout << "\n\nAfter updating...";
22 showgolf(ann);
23 showgolf(andy);
24
25 cin.get();
26 cin.get();
27 return 0;
28}

2. Redo Listing 9.8, replacing the character array with a string object. The program
should no longer have to check whether the input string fits, and it can compare the
input string to to check for an empty line.
My answer:
1 #include <iostream>
2 #include <string>
3 // constants
4 // function prototype
5 void strcount(const std::string & str);
6 int main()
7 {
8

using namespace std;

string input;

10
11 cout << "Enter a line:\n";
12 getline(cin,input);
13
14 while (input[0] != '\0')
15 {
16

strcount(input);

17

cout << "Enter next line (empty line to quit):\n";

18

getline(cin,input);

19 }
20
21 cout << "Bye\n";
22 cin.get();
23 cin.get();
24 return 0;
25}
26
27void strcount(const std::string & str)

28{
29 using namespace std;
30 static int total = 0; // static local variable
31 int count = 0; // automatic local variable
32 cout << "\"" << str <<"\" contains ";
33
34 count = str.size();
35 total += count;
36 cout << count << " characters\n";
37 cout << total << " characters total\n";
38}

One Response to C++ Primer Plus Chapter 9 Exercise 2 Answer

1.

Vladimir says:
October 24, 2012 at 3:03 pm
I had to replace
while (input[0] != )
with
while (input != )
Regards

3. Begin with the following structure declaration:


struct chaff
{
char dross[20];
int slag;
};
Write a program that uses placement new to place an array of two such structures in a
buffer. Then assign values to the structure members (remembering to use strcpy() for
the char array) and use a loop to display the contents. Option 1 is to use a static array,

like that in Listing 9.9, for the buffer. Option 2 is to use regular new to allocate the
buffer.
My answer:
1 #include <iostream>
2 #include <new>
3
4 struct chaff
5 {
6 char dross[20];
7 int slag;
8 };
9
10char buffer[512];
11
12int main()
13{
14
15using std::cout;
16using std::cin;
17using std::endl;
18
19int choice = 0;
20chaff * p1, *p2;
21
22while (choice != 1 && choice != 2)
23{
24 cout << "How to allocate array ? 1 for Static (buffer), 2 for reg NEW: ";
25 cin >> choice;
26}
27
28if (choice == 1 || choice == 2)

29 if (choice == 1)
30

p1 = new (buffer) chaff[2];

31 else
32

p1 = new chaff[2];

33
34strcpy(p1[0].dross,"Dross Name 1");
35strcpy(p1[1].dross,"Dross Name 2");
36p1[0].slag = 13;
37p1[1].slag = 22;
38
39if (choice == 1)
40

cout << "Using static array, p1 is located at " << &p1 << endl;

41 else
42

cout << "Using reg NEW array, p1 is located at " << &p1 << endl;

43
44for (int i = 0; i < 2; i++)
45 cout << "Dross for chaff #" << i+1 << " is " << p1[i].dross << ", Slag is " << p1[i].slag << endl;
46
47if (choice == 2)
48 delete [] p1;
49
50cin.get();
51cin.get();
52return 0;
53
54}

One Response to C++ Primer Plus Chapter 9 Exercise 3 Answer

1.

Fran says:
August 6, 2012 at 11:17 am

The exercise states that one option is to allocate the buffer with the regular new,
instead of a static variable. Youre not doing that, instead you always allocate the
buffer with a static variable. You should always allocate the array of two chaff
structures with placement new.

4. Write a three-file program based on the following namespace:


namespace SALES
{
const int QUARTERS = 4;
struct Sales
{
double sales[QUARTERS];
double average;
double max;
double min;
};
442 C++ PRIMER PLUS, FIFTH EDITION
// copies the lesser of 4 or n items from the array ar
// to the sales member of s and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
void setSales(Sales & s, const double ar[], int n);
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minumum values
void setSales(Sales & s);
// display all information in structure s
void showSales(const Sales & s);
}
The first file should be a header file that contains the namespace. The second file should
be a source code file that extends the namespace to provide definitions for the three prototyped
functions. The third file should declare two Sales objects. It should use the
interactive version of setSales() to provide values for one structure and the non-interactive
version of setSales() to provide values for the second structure. It should display
the contents of both structures by using showSales().
My answer:
sales.h
1 #ifndef SALES_H_
2 #define SALES_H_
3
4 namespace SALES
5 {

6 const int QUARTERS = 4;


7 struct Sales
8 {
9 double sales[QUARTERS];
10double average;
11double max;
12double min;
13};
14// copies the lesser of 4 or n items from the array ar
15// to the sales member of s and computes and stores the
16// average, maximum, and minimum values of the entered items;
17// remaining elements of sales, if any, set to 0
18void setSales(Sales & s, const double ar[], int n);
19// gathers sales for 4 quarters interactively, stores them
20// in the sales member of s and computes and stores the
21// average, maximum, and minumum values
22void setSales(Sales & s);
23// display all information in structure s
24void showSales(const Sales & s);
25}
26
27#endif
sales.cpp
1 #include <iostream>
2 #include "sales.h"
3
4 namespace SALES
5 {
6

using std::cout;

using std::cin;

using std::endl;

9 // copies the lesser of 4 or n items from the array ar


10// to the sales member of s and computes and stores the
11// average, maximum, and minimum values of the entered items;
12// remaining elements of sales, if any, set to 0
13void setSales(Sales & s, const double ar[], int n)
14{
15 int numsales = n < QUARTERS ? n : QUARTERS;
16 double min,max,average,total = 0;
17
18 min = max = ar[0];
19 for (int i = 0; i < numsales; i++)
20 {
21

s.sales[i] = ar[i];

22

if (min > ar[i])

23

min = ar[i];

24

else if (max < ar[i])

25

max = ar[i];

26

total += ar[i];

27 }
28 average = total / numsales;
29 s.average = average;
30 s.max = max;
31 s.min = min;
32
33 for (int i = numsales; i < QUARTERS; i++)
34

s.sales[i] = 0;

35}
36// gathers sales for 4 quarters interactively, stores them
37// in the sales member of s and computes and stores the
38// average, maximum, and minumum values
39void setSales(Sales & s)

40{
41 double qt,min,max,average,total = 0;
42
43 cout << endl;
44 for (int i = 0; i < QUARTERS; i++)
45 {
46

cout << "Enter sales for quarter #" << i+1 << ": ";

47

cin >> qt;

48

s.sales[i] = qt;

49

total += qt;

50 }
51
52 min = max = s.sales[0];
53 for (int i = 0; i < QUARTERS; i++)
54 {
55

if (min > s.sales[i])

56

min = s.sales[i];

57
58

else if (max < s.sales[i])


max = s.sales[i];

59 }
60
61 average = total / QUARTERS;
62 s.average = average;
63 s.max = max;
64 s.min = min;
65}
66
67// display all information in structure s
68void showSales(const Sales & s)
69{
70 cout << "\nDisplaying Sales structure.";

71 for (int i = 0; i < QUARTERS; i++)


72

cout << "\nSales Quarter #" << i+1 << ": " << s.sales[i];

73 cout << "\nMin = " << s.min;


74 cout << "\nMax = " << s.max;
75 cout << "\nAverage = " << s.average;
76}
77
78}
cp9ex4.cpp
1 #include <iostream>
2 #include "sales.h"
3
4 int main()
5 {
6

double ar[3] = {100,200,300};

SALES::Sales s1, s2;

8
9

SALES::setSales(s1,ar,3);

10 SALES::showSales(s1);
11
12 SALES::setSales(s2);
13 SALES::showSales(s2);
14
15 std::cin.get();
16 std::cin.get();
17 return 0;
18}

Responses to C++ Primer Plus Chapter 9 Exercise 4 Answer

1.

Saad says:

March 6, 2012 at 12:26 am


// C++ Primer Plus Exercise
// Chapter 9, Exercise 4
// CH9EX4_FUNC.cpp for CH9EX4.cpp
#include
#include CH9EX4.h
//using SALES::Sales;
//using namespace SALES;
namespace SALES
{
// copies the lesser of 4 or n items from the array ar
// to the sales member of s and computes and stores the
// average, maximum, and minimum values of the entered items;
// remaining elements of sales, if any, set to 0
void setSales(Sales & s, const double ar[], int n)
{
//- BUBBLE SORT
//| Create tmpAr[] and copy ar[] to it
double *tmpAr = new double[n];
for(int i=0; i<n; i++)
{
tmpAr[i] = ar[i];
}
//|for (int i=0; i<n; i++)
{
for (int j=0; jtmpAr[j+1])
{
double temp = tmpAr[j+1]; // Declared locally so its memory is freed upon use
tmpAr[j+1] = tmpAr[j];
tmpAr[j] = temp;
}
}
}
/* Used to check to see if the tmpAr[] array is sorted
for(int i=0; i<n; i++)
{
std::cout << tmpAr[i] << std::endl;
}
*/

//
//- SALES
// Copies lowest values from tmpAr[] to s.sales[] & frees up tmpAr[]'s memory after
for(int i=0; i<SALES::QUARTERS; i++)
{
s.sales[i] = tmpAr[i];
}
delete [] tmpAr;
//
//- MINIMUM
s.min = s.sales[0];
//
//- MAXIMUM
s.max=0;
for(int i=0; i<n; i++)
{
if(s.max < ar[i])
s.max = ar[i];
}
//
//- AVERAGE
s.average = 0;
for(int i=0; i<n; i++)
{
s.average += ar[i];
}
s.average = s.average/4;
//
}
// gathers sales for 4 quarters interactively, stores them
// in the sales member of s and computes and stores the
// average, maximum, and minimum values
void setSales(Sales & s)
{
for(int i=0; i<SALES::QUARTERS; i++)
{
std::cout <<"Enter Sales Item #" << i+1 <> s.sales[i];
}

//- AVERAGE
s.average = 0;
for(int i=0; i<SALES::QUARTERS; i++)
{
s.average+= s.sales[i];
}
s.average = s.average/4;
//
//- MINIMUM
s.min = s.sales[0];
for(int i=0; i s.sales[i])
s.min = s.sales[i];
}
//
//- MAXIMUM
s.max = s.sales[0];
for(int i=0; i<SALES::QUARTERS; i++)
{
if(s.max < s.sales[i])
s.max = s.sales[i];
}
//
}
// display all information in structure 's'
void showSales(const Sales & s)
{
std::cout << "\n\t\t<>\n;
//- QUARTERLY SALES
for(int i=0; i<SALES::QUARTERS; i++)
{
std::cout << "\tQ" << i+1 << ": "<< s.sales[i] << std::endl;
}
//
//- STATIC MEMBER OUTPUT
std::cout << "\nSales Average: " << s.average;
std::cout << "\nSales Maximum: " << s.max;
std::cout << "\nSales Minimum: " << s.min;
//
}
}
Reply

2.

Saad says:
March 6, 2012 at 12:27 am
// C++ Primer Plus Exercise
// Chapter 9, Exercise 4
#include
#include CH9EX4.h
//using SALES::Sales;
//using namespace SALES;
int main()
{
SALES::Sales salesMulti, salesInter;
double array[12] = {12.12, 19.043, 67.13, 6.78, 99.1, 1.12, 33.19, 79.33, 8.79, 5.68,
78.45, 3.1};
std::cout << "\n\t\t<-<>->\n;
SALES::setSales(salesInter);
SALES::showSales(salesInter);
std::cout << "\n\n\n\t\t<-<>->\n;
SALES::setSales(salesMulti, array, 12);
SALES::showSales(salesMulti);
return 0;
}
Reply

3.

Saad says:
March 6, 2012 at 12:31 am
Sorry about syntax formatting, posting seems to remove most of the formatting and
some of the code too.
Solution sorts the array contents into a temporary storage array and then copies the 4
smallest elements of the array, deletes temporary storage later.
/Saad

You might also like