Sie sind auf Seite 1von 16

1. Write a program that requests the user to enter two integers.

The
program should then
calculate and report the sum of all the integers between and including
the two integers.
At this point, assume that the smaller integer is entered first. For
example, if the user
enters 2 and 9, the program should report that the sum of all the
integers from 2 through
9 is 44.
My Answer:
1 #include <iostream>
2
3 int main()
4 {
5 using namespace std;
6
7 int num1, num2, sum = 0;
8
9 cout << "Enter two digits (1st is lowest): ";
1 cin >> num1;
0
cin >> num2;
1
1
for (int i=num1; i<=num2; i++)
1
2
sum=sum+i;
1 cout << "Sum of all integers (" << num1 << " - " << num2 << ") is " << sum
3 << endl;
1
4

cin.get();
1
5 cin.get();
1 return 0;
6 }
1
7
1
8
1

9
2
0

2. Write a program that asks the user to type in numbers. After each
entry, the program
should report the cumulative sum of the entries to date. The program
should terminate
when the user enters 0.
My Answer:
1

#include <iostream>

2
3

int main()

using namespace std;

6
7

int entry,sum, i=0;

sum = 0;

do

10 {
11

i++;

12

cout << "Type integer: ";

13

cin >> entry;

14

sum = sum + entry;

15

cout << "This was entry #" << i << endl;

16

cout << "Sum of entries so far: " << sum << endl;

17
18 } while (entry != 0);
19
20 cin.get();
21 return 0;
22 }

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


1.

Naeblis says:
April 8, 2011 at 9:28 am
Thanks for posting these solutions.
Id been using the while loop to solve this problem and thinking the problem was in
somehow reading the 0.

3. Daphne invests $100 at 10% simple interest. That is, every year, the
investment earns
10% of the original investment, or $10 each and every year:
interest = 0.10 original balance
At the same time, Cleo invests $100 at 5% compound interest. That is,
interest is 5% of
the current balance, including previous additions of interest:
interest = 0.05 current balance
Cleo earns 5% of $100 the first year, giving her $105. The next year she
earns 5% of
$105, or $5.25, and so on. Write a program that finds how many years it
takes for the
value of Cleos investment to exceed the value of Daphnes investment
and then displays
the value of both investments at that time.
My Answer:
1 #include <iostream>
2
3 int main()
4{
5 using namespace std;
6
7 double DaphneInv, CleoInv = 0; // 10% of $100
8 int years=1;
9
1 DaphneInv = 100+ (100*0.10);
0

1
1
1
2
1
3
1
4

CleoInv = 100+ (100*0.05);

1 while (CleoInv < DaphneInv)


5
{
1
cout << "Year " << years << ", Daphne's investment = $" << DaphneInv
6
<< ", Cleo's investment = $" << CleoInv << ".\n";
1
CleoInv = CleoInv + (CleoInv*0.05);
7
DaphneInv +=10;
1
8
years=years+1;
1}
9
2
cout << "\n\nBreakthrough Year - " << years << endl;
0
cout << "Final Balances: Daphne's: $" << DaphneInv << ", Cleo's: $" <<
2
CleoInv <<".\n";
1
2
2 cin.get();
2 return 0;
3
}
2
4
2
5
2
6

2 Responses to C++ Primer Plus Chapter 5 Exercise 3 Answer


1.

Ian says:
December 16, 2012 at 7:02 am

This is my code:
#include
using namespace std;
int main()
{
int years = 1;
int cleoTot = 100 + (100*0.05);
int daphTot = 100 + (100*0.1);
cout << endl;
while (cleoTot <= daphTot)
{
cout << "Year " << years << ": Daphne's Investment = $" << daphTot << ", Cleo's
Investment = $" << cleoTot << endl;
daphTot += (daphTot*0.05);
cleoTot += 10;
years++;
}
cout << endl <<"Cleo's investment becomes larger than Daphne's on year " << years
<< endl << endl;
cout << "Final balances:" << endl << "Daphne: $" << daphTot << ", Cleo: $" <<
cleoTot << endl << endl;
return 0;
}
Reply

2.

Ian says:
December 16, 2012 at 7:04 am
Apparently it deleted the iostream header include part. I guess it thought I was trying
to use HTML. And it also removed all my spaces. Oh well

4. You sell the book C++ for Fools. Write a program that has you enter a
years worth of
monthly sales (in terms of number of books, not of money). The
program should use a
loop to prompt you by month, using an array of char * (or an array of
string objects, if
you prefer) initialized to the month strings and storing the input data in
an array of int.
Then, the program should find the sum of the array contents and report

the total sales


for the year.
My Answer:
1 #include <iostream>
2 #include <string>
3
4 int main()
5 {
6 using namespace std;
7
8 string month[12]= {"January","February","March","April","May",
9 "June","July","August","September","October","November","December"};
10 int sales[12],sum=0;
11
12
13 cout << "Enter sales by month.\n";
14
15 for (int i=0; i < 12; i++)
16 {
17

cout << month[i] << ": ";

18

cin >> sales[i];

19 }
20
21 cout << "\nEntered Results: \n";
22
23 for (int i=0; i<12;i++)
24 {
25

sum=sum+sales[i];

26

cout << month[i] << ": " << sales[i] << endl;

27 }
28
29 cout << "Total sales: " << sum;

30
31
32 cin.get();
33 cin.get();
34 return 0;
35 }

5. Do Programming Exercise 4, but use a two-dimensional array to store


input for 3 years
of monthly sales. Report the total sales for each individual year and for
the combined
years.
My Answer:
1 #include <iostream>
2 #include <string>
3
4 int main()
5 {
6 using namespace std;
7
8 string month[12]= {"January","February","March","April","May",
9 "June","July","August","September","October","November","December"};
10 int sales[3][12],sum, yeartotal, alltotal =0;
11
12
13 cout << "Enter sales by month.\n";
14 for (int y=0; y<3; y++)
15 {
16

cout << "\n\nYear " << (y+1) << ". \n";

17

for (int i=0; i < 12; i++)

18

19

cout << month[i] << ": ";

20

cin >> sales[y][i];

21

22 }
23
24
25 cout << "\nEntered Results: \n";
26
27 for (int y=0; y<3; y++)
28 {
29

yeartotal=0;

30

cout << "\nYear " << (y+1) << ". \n";

31

for (int i=0; i < 12; i++)

32

33

cout << month[i] << ": " << sales[y][i] << endl;

34

alltotal += sales[y][i];

35

yeartotal += sales[y][i];

36

37

cout << "Total sales for this year: $" << yeartotal << endl;

38 }
39
40 cout << "\nThe Sum of all sales - $" << alltotal << endl;
41
42 cin.get();
43 cin.get();
44 return 0;
45 }

6. Design a structure called car that holds the following information


about an automobile:
its make, as a string in a character array or in a string object, and the
year it was built,
as an integer. Write a program that asks the user how many cars to
catalog. The program
should then use new to create a dynamic array of that many car

structures. Next, it
should prompt the user to input the make (which might consist of more
than one word)
and year information for each structure. Note that this requires some
care because it
alternates reading strings with numeric data (see Chapter 4). Finally, it
should display
the contents of each structure. A sample run should look something like
the following:
How many cars do you wish to catalog? 2
Car #1:
Please enter the make: Hudson Hornet
Please enter the year made: 1952
Car #2:
Please enter the make: Kaiser
Please enter the year made: 1951
Here is your collection:
1952 Hudson Hornet
1951 Kaiser
My Answer:
1 #include <iostream>
2 #include <string>
3
4
5 struct car
6 {
7

std::string make;

int yearbuilt;

9 };
10
11 int main()
12 {
13 using namespace std;
14 int numcars;
15
16 cout << "How many cars do you wish to catalog ? ";
17 (cin >> numcars).get();
18

19 car *ArrayofCars = new car [numcars];


20
21 for (int i=0; i <numcars; i++)
22 {
23

cout << "Car #" << (i+1) << ":\n";

24

cout << "Please enter the make: ";

25

getline(cin,ArrayofCars[i].make);

26

cout << "Please enter the year made: ";

27

(cin >> ArrayofCars[i].yearbuilt).get();

28 }
29
30 cout << "\nHere's your collection:\n";
31
32 for (int i=0; i < numcars; i++)
33

cout << ArrayofCars[i].yearbuilt << " " << ArrayofCars[i].make << endl;

34
35 delete [] ArrayofCars;
36
37 cin.get();
38 cin.get();
39 return 0;
40 }

3 Responses to C++ Primer Plus Chapter 5 Exercise 6 Answer


1.

Jim Smith says:

January 22, 2012 at 11:21 am


Thank you for posting these answers. This problem gave me a few gray hairs. I found
the C version on the books website, but had no idea how to get the C++ string
version to work (mostly due to the line getline(cin,ArrayofCars[i].make);).

I have to say that I think C++ Primer Plus (5th ed.) is an excellent book, but I dont
like how the author shows an older C-style way of doing something, followed by
the C++ way, followed by 3 more C-style ways that are less efficient. Still, its better
to have too much info than not enough. It just makes it harder to weed out the good
from the bad (or the correct from the not-quite as efficient but still correct).
Your answers are helping me tremendously in that regard. Again, thank you.
Reply

2.

Kiwon says:
May 10, 2012 at 6:38 pm
if you are kind,
Will you take a look at my program
#include
#include
using namespace std;
struct Car
{
string make;
int year;
};
int main()
{
using namespace std;
int cars;
cout<>cars;
Car* catalogue=new Car[cars];
for(int x=1; x<=cars; x++)
{
cout<<"Car #"<<x<<endl;
cout<<"Please enter the make: ";
getline(cin, catalogue[x].make);
cout<>catalogue[x].year;
}
cout<<"Here is your collection:\n";
for(int x=1; x<=cars; x++)
cout<<catalogue[x].year<<" "<<catalogue[x].make<<endl;

delete[] catalogue;
cin.get();
return 0;
}
my program keeps freezing. Why???
Reply
o

SP says:

August 1, 2012 at 8:56 pm


At first glance, your catalogue array should be accepting an x that starts at 0,
not 1. Remember that catalogue[0] refers to the first array element and
catalogue[n-1] refers to the nth array element.
Oh yeah, and your #includes are not including any header/include files (ie
iostream and string).

7. Write a program that uses an array of char and a loop to read one
word at a time until
the word done is entered. The program should then report the number
of words entered
(not counting done). A sample run could look like this:
Enter words (to stop, type the word done):
anteater birthday category dumpster
envy finagle geometry done for sure
You entered a total of 7 words.
You should include the cstring header file and use the strcmp() function
to make the
comparison test.
1 #include <iostream>
2 #include <cstring>
3
4 int main()
5 {

6 using namespace std;


7
8 char word[80];
9 char matchword[] = "done";
10 int numwords=0;
11
12 cout << "Enter words (to stop, type the word done):\n";
13 cin >> word;
14
15 while(strcmp(word,matchword)!=0)
16 {
17

cin >> word;

18

numwords++;

19 };
20
21 cout << "\nYou entered a total of " << numwords << " words.";
22
23 cin.get();
24 cin.get();
25 return 0;
26 }

2 Responses to C++ Primer Plus Chapter 5 Exercise 7 Answer


1.

Tyler says:
February 28, 2012 at 5:58 pm
that wont work with that code if u write a word after done program ends =)

8. Write a program that matches the description of the program in


Programming Exercise
7, but use a string class object instead of an array. Include the string
header file and
use a relational operator to make the comparison test.
My Answer:

1 #include <iostream>
2 #include <string>
3
4
5 int main()
6 {
7 using namespace std;
8
9 string word;
10 string matchword = "done";
11 int numwords=0;
12
13 cout << "Enter words (to stop, type the word done):\n";
14 cin >> word;
15
16 while(word != matchword)
17 {
18

cin >> word;

19

numwords++;

20 };
21
22 cout << "\nYou entered a total of " << numwords << " words.";
23
24 cin.get();
25 cin.get();
26 return 0;
27 }
9. Write a program using nested loops that asks the user to enter a
value for the number of
rows to display. It should then display that many rows of asterisks, with
one asterisk in
the first row, two in the second row, and so on. For each row, the
asterisks are preceded
by the number of periods needed to make all the rows display a total
number of characters

equal to the number of rows. A sample run would look like this:
Enter number of rows: 5
.*
**
..***
.****
*****
My Answer:
1

#include <iostream>

2
3

int main()

using namespace std;

6
7

int numrows=0;

int denominator=1;

9
10 cout << "Enter number of rows: ";
11 (cin >> numrows).get();
12
13 for (int i=0; i < numrows; i++)
14 {
15
16
17

for (int j=0; j < (numrows-denominator); j++)


cout << ".";

18
19
20

for (int y=0; y < denominator; y++)


cout << "*";

21
22

cout << "\n";

23

denominator++;

24 }
25
26

27 cin.get();
28 cin.get();
29 return 0;
30 }
Alternative:
#include
int main()
{
using namespace std;
int numRows;
cout <> numRows;
int i = 1;
for (i; i = 1; j)
{
cout << ".";
}
for (j; j < i; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}

Das könnte Ihnen auch gefallen