logic help for smallest/largest value - c++

I went thru so many version of the algorithm to sort smallest and largest that my brain is fried. The book up to this point and searching online haven't helped at all.
I'm having difficulties at saving the last.
I used 3 in, 10 cm and 5 cm as test cases. Entering 3 in first, becomes the largest, entering 5 cm second becomes smallest and then 10 cm becomes smallest again. Tried different version for over 2 hours, even re-wrote that entire section. In the book Programming Principles and Practices using C++, its in the review section, before that I cant find anything to help me out.
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdlib.h>
#include <iomanip>
using namespace std;
int main()
{
vector<double>all_meters;
double smallest= 0,print_smallest, largest = 0,print_largest, num = 0;
string unit, s_input, num_s_input, small_unit, large_unit;
while(cin.good()){
cout << "\n\t\t\t\tEnter '|' to exit.\n\n";
cout << "\t\tNumber to compare followed by white space and unit:";
cin >> num_s_input;
if(num_s_input.compare("|") == 0 || (s_input.compare("|") == 0)){
double sum = 0;
for (double x : all_meters) sum+=x;
cout << "Sum: " << setprecision(4) << sum << "m\n";
cout << "Smallest number: " << print_smallest << small_unit << endl
<< "Largest number: " << print_largest << large_unit << endl
<< "Total number of values: " << all_meters.size() << endl
<< "All the entered numbers converted to meters are: \n";
for (double i = 0; i<all_meters.size(); ++i){
cout << all_meters[i] << setprecision(2) <<"m ";
}
cout << "\nAlright now, goodbye then !\n" << endl;
break;
}
else{
cin >> s_input;
num = strtod(num_s_input.c_str(), NULL);
unit = s_input;
double meter = 0;
if(unit=="cm"){
meter = num / 100;}
else if(unit=="in"){
meter = num / 39.370;}
else if(unit=="ft"){
meter = num / 3.2808;}
else if(unit=="m"){
meter = num;}
else {
cout << "\n\tYou entered wrong unit!\t\n";}
if(largest==0){
largest = meter;
print_largest = num;
large_unit = unit;
cout << num << unit << " largest so far.\n";
}
else if(smallest==0&&meter<largest){
smallest = meter;
print_smallest = num;
small_unit = unit;
cout << num << unit << " smallest so far.\n";
}
else if(largest<meter){
largest = meter;
print_largest = num;
large_unit = unit;
cout << num << unit << " largest so far.\n";
}
else if(smallest>meter){
smallest = meter;
print_smallest = num;
small_unit = unit;
cout << num << unit << " smallest so far.\n";
}
all_meters.push_back(meter);
sort(all_meters.begin(),all_meters.end());
}
}
}
Managed to solve it without using limit, added the new changes to the code. Thanks for the help guys !

More than likely your problem comes from the fact that you are initializing smallest to 0. If you never enter anything smaller than 0 then smallest will never change.
When finding the minimum and maximum values you want to set the the initial value to the largest or smallest number respectively that it can hold. So in this case we would use
double smallest = std::numeric_limits<double>::max();
double largest = std::numeric_limits<double>::lowest()
double num = 0;
This was anything in your data set should be less than smallest and everything should be grater than largest.
This does require #include <limits>

You need to choose a standard unit of measure. The question suggests meters, so use that (you can use float or double for this, depending on what precision you need).
The problem is then simple, create some variables for the sum, smallest seen, and largest seen, the for each new input, convert to the standard format, and update the variables.
The solution (to get you started, not working code) might look something like this:
// You can represent the different types of units as integers
float convertToMeters(float unconvertedValue, int unit) {
// Convert unconvertedValue based on unit
}
float smallest = std::numeric_limits<float>::max();
float largest = std::numeric_limits<float>::lowest();
float sum = 0.0f;
// Update for each new input
while (new_input) {
float convertedValue = convertToMeters(new_value, unit);
// Update total
sum += convertedValue;
// Update smallest and largest
if (convertedValue > largest) largest = convertedValue;
else if (convertedValue < smallest) smallest = convertedValue;
}
As Nathan mentioned, #include <limits> for the limits.

Related

How to show large integer numbers on C++

I am developing a single code that calculates Fatorial Number on C++.
The code
// Exemple: 5! = 5 x 4 x 3 x 2 x 1 = 120
#include <iostream>
using namespace std;
int main() {
int number, total;
cout << "Calculate fatorial number" << endl;
cout << "-------------------" << endl << endl;
cout << "Type a number... ";
cin >> number;
total = 1;
for (int i = number; i > 0; i-- ){
if (i == number){
total = i * total;
cout << number << "! = " << i << " x ";
} else if (i > 1) {
total = i * total;
cout << i << " x ";
} else {
total = i * total;
cout << i << " = ";
}
}
cout << total;
return 0;
}
The problem
When I give it numbers, do not return as expected.
What I want
I Want to know how bypass the bigger number problems so I can calculate at least 100!
Codes Output
number = 10; total = 3628800
number = 20 ; total = -2102132736
Compiler used
OnlineGDB
You need to either use an existing library that deals with large numbers, or implement your own. There's many options, gnu multi-precision, boost, etc...
If you choose to implement your own, you'll store digits in something like:
A string "90120304153543643626424262"
A std::vector<int> of digits (base 10)
{9,0,1,2,0,....}
A std::vector<int> of digits (large base, for efficiency. 2^16 works well)
{42567, 29183, 10987, ...}
Then, you'd need to roll your own multiplication, addition, assignment.
I guess you are searching for bignum arithmetic. You probably need to select some arbitrary-precision arithmetic library like GNU MP and use it.
hello the way i solve for large numbers is by doing this
#include <iostream>
using namespace std;
typedef unsigned long long int bigint; //big int
int main() {
bigint total = 9494949494949497989;
cout<<total<<endl;
return 0;
}
so i make a custom data type called bigint and then instead of doing int total i do bigint total thus i am able to store large int values

Avoid rounding off Nth digit of Pi

I am trying to solve the first problem of this collection
To find and display the value of Pi till the Nth digit (taken as user input limited by some maximum value, my guess the maximum number of decimal places the biggest ds can hold which is long double i guess?)
Below is a snippet of my attempt.
//declarations
typedef numeric_limits< long double > db;
int MAX = db::digits10;
int N = 0; //digit allowance
long double pi_val = M_PI;
string string_pi;
//user input
while(1){
cout << "Enter a value of n < or = " << MAX << endl;
cin >> N;
if(N>18){cout << "Lesser please" << endl;}
else
break;
}
//outputs
cout << "Pi till the " << N << " digit is: ";
cout << setprecision(N+1) << fixed << pi_val << endl;
string_pi = to_string((long double)pi_val * pow(10, N));
cout << string_pi << endl;
cout << "The Nth digit of pi is: " << string_pi[N] << endl;
Ps I am aware that more novel ways exist to go about the problem but since I am an extreme beginner at cpp, I'd attain a better sense of achievement by making my idea work.
Now the problem, it displays the Nth digit correctly but the output of Pi till the Nth value always gets rounded off despite the 'fixed' and 'setprecision()' so I had to display an extra digit to tally with the latter output
How can I fix this problem, if possible without changing the main logic I have used
Thank you

Keeping track of which is the smallest and which is the largest value so far in a loop

could you please help me with solving simple problem? I am very fresh with C++ and learning from book "Programming: Principles and Practice Using C++ by Bjarne Stroustrup". I have never learnt C++ before so I am not familiar with many useful features. The drill says:
"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’s the
smallest so far, write the smallest so far after the number. If it is
the largest so far, write the largest so far after the number"
I do not know how to do this correctly without using vector. Here is my code:
#include "C:/std_lib_facilities.h"
int main()
{
double a, b,differ=0;
char c=' ';
cout << "Enter two values: \n";
while (c != '|' && cin >> a >> b )
{
if (a > b)
{
cout << "The smaller value is: "<< b << " and the larger value is: " << a << "\n \n";
differ = a - b;
if (differ < 1.0 / 100)
cout << "Numbers are almost equal\n\n";
}
else if (a < b)
{
cout << "The smaller value is: " << a << " and the larger value is: " << b << "\n \n";
differ = b - a;
if (differ < 1.0 / 100)
cout << "Numbers are almost equal\n\n";
}
else
{
cout << "These values are equal!\n";
}
cout << "Enter a character | to break loop: \n";
cin >> c;
}
cout << "You have exited the loop.\n";
keep_window_open();
}
And here are previous steps, these I have solved with code above:
Write a program that consists of a while-loop that (each time around the loop) reads in two ints and then prints them. Exit the
program when a terminating '|' is entered.
Change the program to write out the smaller value is: followed by the smaller of the numbers and the larger value is: followed by the
larger value.
Augment the program so that it writes the line the numbers are equal (only) if they are equal.
Change the program so that it uses doubles instead of ints.
Change the program so that it writes out the numbers are almost equal after writing out which is the larger and the smaller if the two
numbers differ by less than 1.0/100.
Could you give me some hint how to do step 6.? I had some ideas but none of them worked..
Here is new code:
#include "C:/std_lib_facilities.h"
int main()
{
double smallestSoFar = std::numeric_limits<double>::max();
double largestSoFar = std::numeric_limits<double>::min();
double a,differ=0;
char c=' ';
cout << "Enter value: \n";
while (c != '|' && cin >> a)
{
if (a > largestSoFar)
{
largestSoFar = a;
cout <<"Largest so far is: "<< largestSoFar << endl;
}
else if (a < smallestSoFar)
{
smallestSoFar = a;
cout <<"Smallest so far is: "<< smallestSoFar << endl;
}
else if(smallestSoFar >= a && a<=largestSoFar)
cout << a << endl;
cout << "Enter a character | to break loop: \n";
cin >> c;
}
cout << "You have exited the loop.\n";
keep_window_open();
}
I do not know how to do this correctly without using vector.
You do not need vector for this. The description correctly says that two variables would be sufficient:
// Declare these variables before the loop
double smallestSoFar = std::numeric_limits<double>::max();
double largestSoFar = std::numeric_limits<double>::min();
Modify your loop to read into a, not into both a and b. Check the newly entered value against smallestSoFar and largestSoFar, do the printing, and re-assign smallest and largest as necessary. Note that the first time around you should see both printouts - for largest so far and for smallest so far.
Based on the knowledge that you are suppose to know at the current stage for the this assignment. The code should go something like this:
#include < iostream>
#include < cstdlib>
int main() {
double num_1 = 0;
double num_2 = 0;
double largest = 0;
double smallest = 0;
bool condition1 = true;
while (true) {
std::cin >> num_1;
if (num_1 > largest){
largest = num_1;
}
else if (num_1 < smallest) {
smallest = num_1;
}
std::cout << "The largest so far: " << largest << std::endl;
std::cin >> num_2;
if (condition1) {
smallest = largest;
condition1 = false;
}
if (num_2 < smallest) {
smallest = num_2;
}
else if (num_2 > largest) {
largest = num_2;
}
std::cout << "The smallest so far: " << smallest << std::endl;
}
system("pause");
return 0;
}
double large = 0;
double small = 0;
double input;
int counter = 0;
while (counter < 5) {
cin >> input;
cout <<"Large value: "<< large << '\t' <<"Small value: "<< small\
<< '\t' <<"Input value: "<< input << '\n';
if (input < small) {
cout << "The smallest value is " << input<<\
"\nthe largest value is "<< large<<'\n';
small = input;
}
else if (input > small&& input < large) {
cout << "The smallest value is " << small << \
"\nthe largest value is " << large<<'\n';
}
else if (input > small&& input > large) {
cout << "The smallest value is " << small << \
"\nthe largest value is " << input << '\n';
large = input;
}
counter += 1;

Getting a number and a unit from input as a double and a string -- Programming: Principles and Practices Using C++

I've been going through Bjarne Stroustrup's Programming: Principles and Practices Using C++ for my own benefit. This is not homework and this is not for school.
I'm at my wit's end with this drill from Chapter 4. I'm supposed to take in a number and a unit from input, store the number as a double and the unit as a string in a while loop, and keep track of the largest and smallest numbers seen so far. It works perfectly for units that are one character like "m" or "g", but when I enter a two-character unit such as "cm" or "ft" the loop ends and the program terminates. Below is my code:
#include <iostream>
using namespace std;
int main()
{
double temp = 0;
string unit = " ";
double largest = 0;
double smallest = 0;
while (cin >> temp >> unit)
{
if (largest == 0 && smallest == 0)
{
largest = temp;
smallest = temp;
cout << "That's the largest number seen so far.\n";
cout << "That's the smallest number seen so far.\n";
}
else if (temp >= largest)
{
largest = temp;
cout << "That's the largest number seen so far.\n";
}
else if (temp <= smallest)
{
smallest = temp;
cout << "That's the smallest number seen so far.\n";
}
else
{
cout << temp << '\n';
}
}
return 0;
}
I really appreciate any help in solving this problem. It's driving me insane.
I needed to #include <string> before it would even compile, and I added extra "printf()" stmts for debugging.
But your code should work: one character, or many characters.
Here's my (modified) code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
double temp = 0;
string unit = " ";
double largest = 0;
double smallest = 0;
while (cin >> temp >> unit)
{
cout << "NEXT: temp=" << temp << ", unit=" << unit << "\n";
if (largest == 0 && smallest == 0)
{
largest = temp;
smallest = temp;
cout << "That's the largest number seen so far.\n";
cout << "That's the smallest number seen so far.\n";
}
else if (temp >= largest)
{
largest = temp;
cout << "That's the largest number seen so far.\n";
}
else if (temp <= smallest)
{
smallest = temp;
cout << "That's the smallest number seen so far.\n";
}
else
{
cout << temp << '\n';
}
}
cout << "DONE: temp=" << temp << ", unit=" << unit << ", smallest=" << smallest << ", largest=" << largest << "\n";
return 0;
}
And here's sample output:
12 cm
NEXT: temp=12, unit=cm
That's the largest number seen so far.
That's the smallest number seen so far.
6 "
NEXT: temp=6, unit="
That's the smallest number seen so far.
18 feet
NEXT: temp=18, unit=feet
That's the largest number seen so far.
^D
DONE: temp=18, unit=feet, smallest=6, largest=18
Okay, thanks for all your help everyone.
It seems the issue was compiler/system specific. My code was having issues with OSX's default compiler, but running the same code on Windows 10 using MinGW as a compiler worked flawlessly. What a strange bug. If anyone has any tips on how to get this program to work on OSX using its default compiler, it'd be very appreciated.

A part of the program doesn't execute after the first time run

I was checking out a new book on C++ when I did one of the exercises in it. The program gets a length followed by its unit. The program converts the value into centimeters and then compares the value with previous inputs and gives an output saying which is the largest and smallest value entered so far.
The problem is that, there seems to be a problem when using the 'feet' unit.
Below is my main.cpp:
#include "std_lib_facilities.h"
int main()
{
double value{0};
double largest{0};
double smallest{0};
string unit;
constexpr double cm_per_in {2.54};
constexpr double cm_per_m {100};
constexpr double cm_per_ft {cm_per_in*12};
int sum;
while(cin >> value >> unit)
{
if(unit=="cm")
{
if(largest==0 && smallest == 0){
smallest = value;
largest = value;
cout << value << "cm is the only value entered till now.\n";
}
else if(value<smallest)
{
smallest = value;
cout << "\n\nThe smallest so far is "<<smallest<<"cm.\n";
cout << "\nThe largest so far is "<<largest<<"cm.\n";
}
else if(value>largest){
largest = value;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "\nThe largest so far is "<<largest<<"cm.\n";
}
sum += value;
}
else if(unit=="m")
{
if(largest==0 && smallest == 0){
smallest = value*cm_per_m;
largest = value*cm_per_m;
cout << value*cm_per_m << "cm is the only value entered till now.\n";
}
else if(value*cm_per_m<smallest)
{
smallest = value*cm_per_m;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "\nThe largest so far is "<<largest<<"cm.\n";
}
else if(value*cm_per_m>largest){
largest = value*cm_per_m;
cout << "\nThe smalles so far is "<<smallest<<"cm.\n";
cout << "The largest so far is "<<largest<<"cm.\n";
}
sum += value*cm_per_m;
}
else if(unit=="ft")
{
if(largest==0 && smallest == 0){
smallest = value*cm_per_ft;
largest = value*cm_per_ft;
cout << value*cm_per_ft << "cm is the only value entered till now.\n";
}
else if(value*cm_per_ft<smallest)
{
smallest = value*cm_per_ft;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "\nThe largest so far is "<<largest<<"cm.\n";
}
else if(value*cm_per_ft>largest){
largest = value*cm_per_ft;
cout << "\nThe smallest so far is "<<smallest<<"cm.\n";
cout << "The largest so far is "<<largest<<"cm.\n";
}
sum += value * cm_per_ft;
}
else if(unit=="in")
{
if(largest==0 && smallest == 0){
smallest = value*cm_per_in;
largest = value*cm_per_in;
cout << value*cm_per_in << "cm is the only value entered till now.\n";
}
else if(value*cm_per_in<smallest)
{
smallest = value*cm_per_in;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "The largest so far is "<<largest<<"cm.\n";
}
else if(value*cm_per_in>largest){
largest = value*cm_per_in;
cout << "The smallest so far is "<<smallest<<"cm.\n";
cout << "The largest so far is "<<largest<<"cm.\n";
}
sum += value * cm_per_in;
}
}
return 0;
}
Below is the input/output which happened with the program:
D:\C++\testing\subtesting>program
24 cm
24cm is the only value entered till now.
24 in
The smallest so far is 24cm.
The largest so far is 60.96cm.
24 m
The smallest so far is 24cm.
The largest so far is 2400cm.
24 ft
As you can see in the above output, there is no output when the 'feet' unit is used. Another test's output:
D:\C++\testing\subtesting>program
24 ft
731.52cm is the only value entered till now.
24 ft
The smallest so far is 731.52cm.
The largest so far is 731.52cm.
200 ft
The smallest so far is 731.52cm.
The largest so far is 6096cm.
30 ft
There seems to be some sort of bug in the 'feet' code. Sometimes the code works and sometimes it doesn't. Can somebody help me?
Nothing is wrong with the code. Something is wrong with your expectations.
You display largest and smallest only when one of them changes, which is a clear design intent, not bug.
After entering 24 in and 24 m, the third value of 24 ft is neither largest nor smallest, so correctly produces no output.