Problem Solving With C++ 9th Edition Savitch Solutions Manual Instant Download
Problem Solving With C++ 9th Edition Savitch Solutions Manual Instant Download
https://testbankfan.com/product/problem-solving-with-c-9th-
edition-savitch-solutions-manual/
https://testbankfan.com/product/problem-solving-with-c-9th-edition-
savitch-test-bank/
https://testbankfan.com/product/problem-solving-with-c-10th-edition-
savitch-solutions-manual/
https://testbankfan.com/product/problem-solving-with-c-10th-edition-
savitch-test-bank/
https://testbankfan.com/product/fundamentals-of-financial-management-
concise-edition-8th-edition-brigham-test-bank/
History of Psychology The Making of a Science 1st Edition
Kardas Test Bank
https://testbankfan.com/product/history-of-psychology-the-making-of-a-
science-1st-edition-kardas-test-bank/
https://testbankfan.com/product/management-the-essentials-4th-edition-
robbins-solutions-manual/
https://testbankfan.com/product/building-management-skills-an-action-
first-approach-1st-edition-daft-test-bank/
https://testbankfan.com/product/comm-4th-edition-verderber-test-bank/
https://testbankfan.com/product/business-law-australian-10th-edition-
gibson-test-bank/
Labor Relations Striking a Balance 4th Edition Budd Test
Bank
https://testbankfan.com/product/labor-relations-striking-a-
balance-4th-edition-budd-test-bank/
Chapter 7
ARRAYS
Regarding the solutions presented here.: C++ leaves many options on how to do a problem,
and any book will necessarily choose a subset to present. It is necessary to read the text, as
that is what the student has to work with. I am striving to produce a complementary
document to the text, a document for the instructor. Most of the time, will use the
initialization list style for constructors. If you present the code using the initializer list, you
should also assign Appendix 7 on constructor initialization.
Here, I have done the interactive version only. The solution is broken into two files. The
source for each is listed, along with test data, command generating the output and the
output.
I have done the solution using structs here. The enhancement of adding the choice of
graphical output is provided. I have done only the interactive version here. The variation
using file i/o is left to the reader. Notes will be supplied for doing this without the struct.
//Program is to
//1)read in long term month by month average rain fall
// averages
//2)read in the previous year's month by month actual rain
// fall.
1
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
//Output:
//'nicely' formatted table showing rainfall for each of the
// previous
//12 months as well as how much above or below the monthly
// average the rainfall is for each month. The output
// should correctly label months.
//
//Month name handling: code months as integers and do a
//conversion at output. Any input scheme acceptable as long
//as the user interface is relatively easy to use.
//Enhanced version:
//Add capability to print two bar graphs for each month,
//showing the average rainfall and the actual rainfall for
//each of the previous 12 months. Ask the user's preference:
//a table or a graph, and display the format requested.
//
//Provide a loop to repeat the process at the user's request.
#include <iostream>
#include <cstring>
2
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
#include <cstdlib>
struct monthly_rain
{
int month;
double rain_fall;
double avg_rain_fall;
};
3
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
int main()
{
using namespace std;
int current_month;
monthly_rain rainfall[12];
char ans;
char tg;
do
{
input( rainfall, current_month);
cout << "Please choose between g)raphical and t)abular "
<< "output" << endl;
cin >> tg; //tg means table/graphics choice :)
if ( 't' == tg || 'T' == tg )
{
cout << "You chose the tabular output." << endl;
output_table( rainfall, current_month );
}
else if ( 'g' == tg || 'G' == tg )
{
cout << "You chose the graphical output" << endl;
output_bargraph( rainfall, current_month);
}
else
{
cout << "You typed neither of the choices. "
4
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
void full_month_name(int);
5
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
rainfall[i].month = i;
cout << endl;
}
cout << endl << "The actual rainfall: " << endl;
cout << "What is the current month? Please give the "
<< "number of the month (Jan = 0, etc." << endl;
6
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
{
cout << "For month ";
full_month_name(i);
cout << ' ';
cin >> rainfall[i].rain_fall;
cout << "\tRainfall: " << rainfall[i].rain_fall;
cout << endl;
}
7
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
8
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
{
cout.width(4);
cout << rainfall[i].avg_rain_fall << " |";
}
cout.width(4);
cout << rainfall[i].avg_rain_fall << endl;
9
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
{
case 0: cout << "Jan";
break;
case 1: cout << "Feb";
break;
case 2: cout << "Mar";
break;
case 3: cout << "Apr";
break;
case 4: cout << "May";
break;
case 5: cout << "Jun";
break;
case 6: cout << "Jul";
break;
case 7: cout << "Aug";
break;
case 8: cout << "Sep";
break;
case 9: cout << "Oct";
break;
case 10: cout << "Nov";
break;
case 11: cout << "Dec";
break;
default: cout << "NO SUCH MONTH " << month << endl;
exit(1);
}
}
10
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
11
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
default: cout << "NO SUCH MONTH " << month << endl;
exit(1);
}
}
//file: Ch7Prg1.bargraph.cpp
//
//to print two bargraphs for each month, showing average and
//actual rainfall each month.
#include <iostream>
struct monthly_rain
{
int month;
double rain_fall;
double avg_rain_fall;
};
12
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
13
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
{
full_month_name( i );
if ( i >= current_month )
cout << "(Previous year)";
cout << "\naverage\t ";
bargraph( rainfall[i].avg_rain_fall );
cout << "\nactual\t ";
bargraph( rainfall[i].rain_fall );
cout << endl;
if ( 5 == i )
{
output_scale();
}
}
output_scale();
}
The rainfall averages in the data come from The World Almanac and Book of Facts 1993,
published by the World Almanac. (C) Phaos Books,1992. The rainfall numbers presented
here are made-up.
$cat data
3.7 3.6 5.1 3.8 4.2 4.2 4.4 4.8 4.0 3.3 3.3 3.5
4
4.3 3.2 5.5 5.1 4.0 2.2 2.0 1.1 4.1 3.3 3.9 3.7
t
y
3.7 3.6 5.1 3.8 4.2 4.2 4.4 4.8 4.0 3.3 3.3 3.5
14
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Savitch Instructor’s Resource Guide
Problem Solving w/ C++, 9e Chapter 7
4
4.3 3.2 5.5 5.1 4.0 2.2 2.0 1.1 4.1 3.3 3.9 3.7
g
y
3.7 3.6 5.1 3.8 4.2 4.2 4.4 4.8 4.0 3.3 3.3 3.5
4
4.3 3.2 5.5 5.1 4.0 2.2 2.0 1.1 4.1 3.3 3.9 3.7
h
n
The command used to run the program and the output is:
17:19:43:~/AW$ a.out < data > ch7prb2.out
There follows edited output obtained from this run. I have 'redacted' the input and the
output where either is identical for all three runs and after it has been seen.
$cat ch7prb2.out
Monthly rainfall input routine.
15
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
Random documents with unrelated
content Scribd suggests to you:
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.
• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.
testbankfan.com