In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:
http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
My version is spelt differently so adjust the code accordingly if copying and pasting.
Chapter 4 Drill // 4.6
http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h
My version is spelt differently so adjust the code accordingly if copying and pasting.
Chapter 4 Drill // 4.6
Now change the body of the loop so that it reads just one double each time around. Define two variables to keep track of which is the smallest and which is the largest value you have seen so far. Each time through the loop write out the value entered. If it is the largest so far, write the largest so far after the number, if its the smallest write the smallest so far after the number.
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
int main()
{
double number;
double largest = 0;
double smallest = 0;
cout << "Please enter a number: \n";
while (cin >> number)
{
cout << "\nYou entered: " << number;
{
if (largest == 0 && smallest == 0)
{
largest = number;
smallest = number;
cout << "\nThe smallest value so far is: " << smallest;
cout << "\nThe largest value so far is : " << largest << endl;
}
else if (number > largest)
{
largest = number;
cout << "\nThe smallest value so far is: " << smallest;
cout << "\nThe largest value so far is : " << largest << endl;
}
else if (number < largest)
{
if (number < smallest)
{
smallest = number;
}
cout << "\nThe smallest value so far is: " << smallest;
cout << "\nThe largest value so far is : " << largest << endl;
}
cout << "\nPlease enter a number: \n";
}
}
keep_window_open();
return 0;
}
#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;
int main()
{
double number;
double largest = 0;
double smallest = 0;
cout << "Please enter a number: \n";
while (cin >> number)
{
cout << "\nYou entered: " << number;
{
if (largest == 0 && smallest == 0)
{
largest = number;
smallest = number;
cout << "\nThe smallest value so far is: " << smallest;
cout << "\nThe largest value so far is : " << largest << endl;
}
else if (number > largest)
{
largest = number;
cout << "\nThe smallest value so far is: " << smallest;
cout << "\nThe largest value so far is : " << largest << endl;
}
else if (number < largest)
{
if (number < smallest)
{
smallest = number;
}
cout << "\nThe smallest value so far is: " << smallest;
cout << "\nThe largest value so far is : " << largest << endl;
}
cout << "\nPlease enter a number: \n";
}
}
keep_window_open();
return 0;
}
This one didn't take me as long as 4.5 and after some head scratching got away with only 4 'if' statements.
I've found that Bjarne likes to deliberately make his exercises as vague as possible, in this one you are practically abandoning everything you have done from 4.1-4.5.
Seeing as how I only had 1 variable to play with this time I couldn't directly start off comparing it with other values (as we have none) so the first 'if' statement gets the ball rolling setting number as the largest and smallest value we've seen so far.
The next if statement checks if number is larger than largest, if it is then it sets that as the largest variable and prints the current smallest and largest variables we've seen so far.
The 3rd if statement checks if number is smaller than the current largest number and has another if statement embedded in it. If number is smaller than largest and then number is smaller than smallest, smallest becomes number. If not nothing happens and the current largest and smallest numbers are printed.
It was this last bit that kept throwing me as my loop would work fine but if number was smaller than largest then it just kept setting the current number as smallest which I didn't want because sometimes it wasn't the smallest.
No comments:
Post a Comment