Professional Documents
Culture Documents
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
8
9
golf ann;
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
string input;
10
11 cout << "Enter a line:\n";
12 getline(cin,input);
13
14 while (input[0] != '\0')
15 {
16
strcount(input);
17
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}
1.
Vladimir says:
October 24, 2012 at 3:03 pm
I had to replace
while (input[0] != )
with
while (input != )
Regards
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
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}
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.
using std::cout;
using std::cin;
using std::endl;
s.sales[i] = ar[i];
22
23
min = ar[i];
24
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
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
56
min = s.sales[i];
57
58
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.";
cout << "\nSales Quarter #" << i+1 << ": " << s.sales[i];
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}
1.
Saad says:
//
//- 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