Beruflich Dokumente
Kultur Dokumente
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()
6
7
sum = 0;
do
10 {
11
i++;
12
13
14
15
16
cout << "Sum of entries so far: " << sum << endl;
17
18 } while (entry != 0);
19
20 cin.get();
21 return 0;
22 }
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
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
18
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 }
17
18
19
20
21
22 }
23
24
25 cout << "\nEntered Results: \n";
26
27 for (int y=0; y<3; y++)
28 {
29
yeartotal=0;
30
31
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 }
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
24
25
getline(cin,ArrayofCars[i].make);
26
27
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 }
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:
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 {
18
numwords++;
19 };
20
21 cout << "\nYou entered a total of " << numwords << " words.";
22
23 cin.get();
24 cin.get();
25 return 0;
26 }
Tyler says:
February 28, 2012 at 5:58 pm
that wont work with that code if u write a word after done program ends =)
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
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()
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
18
19
20
21
22
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;
}