Drill 7-8-9 (Chapter 4)

154 views
Skip to first unread message
Message has been deleted

pantera

unread,
Dec 24, 2009, 5:22:29 AM12/24/09
to PPP-public
My program for Drill 7-8-9... Still not sure if this is expected by
the requirement of the drills, especially, the Drill 9.
// Drill 7 - Chapter 4
#include "std_lib_facilities.h"
int main()
{
const double cm_per_m = 100;
const double cm_per_inch = 2.54;
const double inch_per_foot = 12;
double x;
double smallest = 0;
double largest = 0;
string s = " ";
double sum = 0;
int counter = 0;
cout << "Enter a number, followed by its unit: ";
while(cin>>x>>s && (s=="cm"||s=="m"||s=="in"||s=="ft")) {
++counter;
if (x<smallest) {
smallest = x;
}
else if (x>largest) {
largest = x;
}
if (s=="cm")
x = x/cm_per_m; // convert to m
else if (s=="in")
x = (x*cm_per_inch)/cm_per_m; // convert to cm
to m
else if (s=="ft")
x = (x*inch_per_foot*cm_per_inch)/cm_per_m; //
convert to inch to
cm to m
sum += x;
}
cout << "Smallest: " << smallest;
cout << "\nLargest: " << largest;
cout << "\nSum: " << sum;
cout << "\nNumber of values: " << counter << endl;

rober...@gmail.com

unread,
Jun 28, 2016, 7:18:26 PM6/28/16
to PPP-public, gelh...@gmail.com
I just got done with this Drill after a few hours of work. You can check it for reference. Feel free to ask any questions. Forgive the commented sections, they are from the earlier parts




#include "../../std_lib_facilities.h"
using std::cout; using std::cin; using std::endl;

int main()
{
vector<double> val1{0};
vector<double> val2;
double buffer = 0;
double smallest = 0;
double largest = 0;
double runningTotal = 0;
string indicator = " ";
string sbuffer;
int runningCount = 0;

cout << "Please enter a value followed by a unit indicator (cm, m, in, or ft): ";
while (cin >> val1[0] >> indicator)
{
if (indicator == "cm" || indicator == "m" || indicator == "in" || indicator == "ft")
{
buffer = val1[0];
sbuffer = indicator;
while (indicator != "m")
{
if (indicator == "cm")
{
val1[0] = val1[0] / 100;
indicator = "m";
}
else if (indicator == "in")
{
val1[0] = val1[0] * 2.54;
indicator = "cm";
}
else if (indicator == "ft")
{
val1[0] = val1[0] * 12;
indicator = "in";
}
}

cout << buffer << sbuffer << " is equal to " << val1[0] << indicator;
runningTotal += val1[0];
++runningCount;
val2.push_back(val1[0]);

if (smallest == 0 && largest == 0)
{
largest = smallest = val1[0];
cout << endl;
}
else if (val1[0] < smallest)
{
smallest = val1[0];
cout << " the smallest so far." << endl;
}
else if (val1[0] > largest)
{
largest = val1[0];
cout << " the largest so far." << endl;
}
else
cout << endl;

}
else
cout << "Value rejected, try a different unit!" << endl;



//cout << val1[0] << " " << val1[1] << endl;
//if (val1[0] == val1[1])
// cout << "The numbers are equal." << endl;
//else if (val1[0] > val1[1])
// cout << "The smaller value is: " << val1[1] << endl << "The larger value is: " << val1[0] << endl;
//else
// cout << "The smaller value is: " << val1[0] << endl << "The larger value is: " << val1[1] << endl;

////if val1[0] and val1[1] are less than 1.0/100 difference
//if ((val1[0] - val1[1] <= .10 && val1[0] - val1[1] > 0) || (val1[1] - val1[0] <= .10 && val1[1] - val1[0] > 0))
// cout << "The numbers are ALMOST the same." << endl;
}
cout << "The highest value was: " << largest << indicator << endl;
cout << "The lowest value was: " << smallest << indicator << endl;
cout << "The sum of all numbers entered is: " << runningTotal << indicator << endl;
cout << "You enetered a total of " << runningCount << " numbers." << endl;

sort(val2);

cout << "The values you entered are: " << endl;
for (double x : val2)
{
cout << x << indicator << endl;
}


return 0;
}
Reply all
Reply to author
Forward
0 new messages