How to deal with overlapping values in c++? - c++

I'm currently trying to solve a programming problem that involves different ranges of values that overlap. The task is to accept input, in E-notation, and that is where the overlap of range inevitably occurs.
I have 2 ranges that overlap at 1E-11. 1E-11 and lower and 1E-11 and higher
The output would be 1E-11 is either x or it is y. Programmatically i would solve it like this:
(X_MIN would be 1E-11 and X_MAX 1E-8)
(Y_MAX would be 1E-11 and Y_MIN 1E-13)
(lengthOfRange <= X_MIN) && (lengthOfRange >= Y_MAX) ?
cout << "This value entered indicates that it is x\n" :
cout << "It is y";
Expressed this way if i input IE-11 it shows me "This value entered indicates ..." but will never show me it is y (understandably - overlap!)
The other way around would be expressing it this way:
(lengthOfRange <= X_MIN) && (lengthOfRange != Y_MAX) ?
cout << "This value entered indicates that it is x\n" :
cout << "It is y";
The output would always be "... It is y ..." (Same difference - overlap!) There is no other determining factor that would tell range is x or y coming in to play there as of right now.
...
if (lengthOfRange <= X_MIN) && (lengthOfRange == Y_MAX)
{
cout << "The input indicates that it could be either x or y\n";
}
...
Even if i were to solve the problem in a way such as defining the range with different values, would in the end lead to the very same problem. I COULD define MIN and MAX as constants in lengthOfFrequency, which is totally different, bit then i would have to say: lengthOfFrequency = 1E-11; and voila same problem once again. 1 input 2 ranges that are technically different, getting the same one and only correct value in E-notation.
Is there a way around this without involving to simply say input is either x || y? Which it is technically of course, and if it were to be solved physically there are ways of telling it apart that 1E-11 is not 1E-11 though it is. (I hope i make sense here). But, again, ... is there such way, and how would i go about writing it? (Not asking for code specifically though it would be highly welcome, just a pointer in the right direction.) Or should i rather go with saying input is either x || y?
Thanks in advance for any answer!
**Minimum Complete Code:**
#include <iostream>
using std::cout;
using std::cin;
int main()
{
/* Constants for ranges, min and max */
const double X_RAYS_MIN = 1E-13,
X_RAYS_MAX = 1E-11,
Y_RAYS_MIN = 1E-11,
Y_RAYS_MAX = 1E-8,
Z_RAYS_MIN = 1E-7,
Z_RAYS_MAX = 3.8E-7;
double lengthOfRange;
/* Test output and validation */
cout << "Enter value in scientifc notation: ";
cin >> lengthOfRange;
/* X_RAYS_MIN < is 1E-14, 1E-15, 1E-16 etc. > 1E-12, 1E-11 etc.. */
if (lengthOfRange >= X_RAYS_MIN && lengthOfRange <= X_RAYS_MAX)
{
cout << "X_RAYS\n";
}
else if (lengthOfRange >= Y_RAYS_MIN && lengthOfRange <= Y_RAYS_MAX)
{
cout << "Y_RAYS\n";
}
system("pause");
return 0;
}
Output is: 1E-10 is Y_RAYS, 1E-9 is Y_RAYS, 1E-11 X_RAYS, 1E-12 X_RAYS
Somehow i found the solution for my problem myself without going any roundabout ways ... By hovering over the 1E-13:
X_RAYS_MIN = 1E-13
VS showed me 1.(numberofzeros)3E-13, and guess what ... if instead the input for 1E-11 is 2E-11, the output for X_RAYS becomes Y_RAYS ... so the problem "magically" solved itself ... lucky me i guess ... :)

Related

Prevent a user from entering a value which would cause integer to overflow

I'm very new to C++ programming, and have written a simple program to calculate the factorial of an integer provided by the user. I am attempting to account for inputs which would cause an error, or do not make sense (e.g. I have accounted for input of a negative number/-1 already). I want to print out an error if the user enters a number whose factorial would be larger than the maximum integer size.
I started with:
if(factorial(n) > INT_MAX)
std::cout << "nope";
continue
I tested this with n = ~25 or 26 but it doesn't prevent the result from overflowing and printing out a large negative number instead.
Second, I tried assigning this to a variable using a function from the 'limits.h' header and then comparing the result of factorial(n) against this. Still no luck (you can see this solution in the code sample below).
I could of course assign the result to a long and test against that but you wouldn't have to go very far until you started to wrap around that value, either. I'd prefer to find a way to simply prevent the value from being printed if this happens.
#include <iostream>
#include <cstdlib>
#include <limits>
int factorial(int n)
{
auto total = 1;
for(auto i = 1; i <= n; i++)
{
total = total * i; //Product of all numbers up to n
}
return total;
}
int main()
{
auto input_toggle = true;
auto n = 0;
auto int_max_size = std::numeric_limits<int>::max();
while(input_toggle = true)
{
/* get user input, check it is an integer */
if (factorial(n) > int_max_size)
{
std::cout << "Error - Sorry, factorial of " << n << " is larger than \nthe maximum integer size supported by this system. " << std::endl;
continue;
}
/* else std::cout << factorial(n) << std::endl; */`
As with my other condition(s), I expect it to simply print out that small error message and then continue asking the user for input to calculate. The code does work, it just continues to print values that have wrapped around if I request the factorial of a value >25 or so. I feel this kind of error-checking will be quite useful.
Thanks!
You are trying to do things backwards.
First, no integer can actually be bigger than INT_MAX, by definition - this is a maximum value integer can be! So your condition factorial(n) > int_max_size is always going to be false.
Moreover, there is a logical flaw in your approach. You calculate the value first and than check if it is less than maximum value allowed. By that time it is too late! You have already calculated the value and went through any overflows you might have encountered. Any check you might be performing should be performed while you are still doing your calculations.
In essence, you need to check if multiplying X by Z will be within allowed range without actually doing the multiplication (unfortunately, C++ is very strict in leaving signed integer overflow undefined behavior, so you can't try and see.).
So how do you check if X * Y will be lesser than Z? One approach would be to divide Z by Y before engaging in calculation. If you end up with the number which is lesser than X, you know that multiplying X by Y will result in overflow.
I believe, you know have enough information to code the solution yourself.

Why do I keep getting Nan as output?

I am trying to write a simple gradient descent algorithm in C++ (for 10,000 iterations). Here is my program:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
double learnrate=10;
double x=10.0; //initial start value
for(int h=1; h<=10000; h++){
x=x-learnrate*(2*x + 100*cos(100*x));
}
cout<<"The minimum is at y = "<<x*x + sin(100*x)<<" and at x = "<<x;
return 0;
}
The output ends up being: y=nan and x=nan. I tried looking at the values of x and y by putting them into a file, and after a certain amount of iterations, I am getting all nans (for x and y). edit: I picked the learning rate (or step size) to be 10 as an experiment, I will use much smaller values afterwards.
There must be something wrong with your formula. Already the first 10 values of x are increasing like hell:
-752.379
15290.7
-290852
5.52555e+06
-1.04984e+08
1.9947e+09
-3.78994e+10
7.20088e+11
-1.36817e+13
2.59952e+14
No matter what starting value you choose the absolute value of the next x will be bigger.
|next_x| = | x - 20 * x - 100 * cos(100*x) |
For example consider what happens when you choose a very small starting value (|x|->0), then
|next_x| = | 0 - 20 * 0 - 100 * cos ( 0 ) | = 100
Because at h=240 the variable "x" exceeds the limits of double type (1.79769e+308). This is a diverging arithmetic progression. You need to reduce your learn rate.
A couple of more things:
1- Do not use "using namespace std;" it is bad practice.
2- You can use "std::isnan() function to identify this situation.
Here is an example:
#include <iomanip>
#include <limits>
int main()
{
double learnrate = 10.0;
double x = 10.0; //initial start value
std::cout<<"double type maximum=" << std::numeric_limits<double>::max()<<std::endl;
bool failed = false;
for (int h = 1; h <= 10000; h++)
{
x = x - learnrate*(2.0*x + 100.0 * std::cos(100.0 * x));
if (std::isnan(x))
{
failed = true;
std::cout << " Nan detected at h=" << h << std::endl;
break;
}
}
if(!failed)
std::cout << "The minimum is at y = " << x*x + std::sin(100.0*x) << " and at x = " << x;
return 0;
}
Print x before the call to the cosine function and you will see that the last number printed before NaN (at h = 240) is:
-1.7761e+307
This means that the value is going to infinity, which cannot be represented (thus Not a Number).
It overflows the double type.
If you use long double, you will succeed in 1000 iterations, but you will still overflow the type with 10000 iterations.
So the problem is that the parameter learnrate is just too big. You should do let steps, while using a data type with larger range, as I suggested above.
The "learn rate" is far too high. Change it to 1e-4, for example, and the program works, for an initial value of 10 at least. When the learnrate is 10, the iterations jump too far past the solution.
At its best, gradient descent is not a good algorithm. For serious applications you want to use something better. Much better. Search for Brent optimizer and BFGS.

How to have C++ solve the equation for an input value?

I am stuck trying to figure out a way using C++ to solve a situation where the user inputs a value using cin and then have the computer solve for a way to get the value of cin, given that the format is given. a super fast example is written below.. yes i know there is a lot of code missing... but the concept is there..
int x;
int y;
int w;
int x = 30 < w < 50;
int y = 60 < w < 90;
cin >> input;
x + y = input;
cout << x;
cout << y;
Naturally though x + y cant be on the lvalue on the right. so i cant just write x + y = input.. so how would i have it solve x + y = input? Additionally I want x and y to be between the numbers listed, which limits the numbers between those inputs.. however in my actual coding i did this with a function.
has school even started yet? no its not homework. im teaching myself C++.. – Sean Holt 1 min ago edit
No im just trying to figure a way of having the computer solve for x/y of an input value if x and y are between specified values in a function
It looks like you think that C++ is going to solve equations for you. It won't. C++ is an imperative style language that is based around the concept of you telling it exactly what to do.
You will have to figure out how to solve for x and y so that you can make an algorithm. This algorithm is then what you make your program from.
There exists other languages in which you can in a sense describe what you want and have the compiler or runtime figure out how to get it for you. C++ is not one of them.
Different ways to solve your particular problem would be to set up an equation system and solving that. Or do brute force approach and iterate through the values of x and y in order to find out which values match.
It looks like you have a 'mathematical' problem here: a couple of values constrained by equations, and you want 'the computer' to find all possible values that fit into the constraints (equations). Am I right?
While some computer programs can certainly do that, the C++ language is not designed for this purpose. The role of the C++ is to give you a way of giving instructions to the processor, like "store this value in memory" or "add these two numbers". But there is no way of saying "solve this mathematical problem".
What you need is some equation solver. But I am not familiar with any. There are tools like Matlab or Mathematica. But I do not think they are free.
If you want to solve the math problem algorithmically, here is a brute force idea in pseudocode:
Input a number.
for each value x between 30 and 50
for each value y between 60 and 90
if x+y equals the number
print x and y
Now you can take a good book or tutorial and code in C++. Look for the for and if keywords (algorithmic concepts of iteration and selection) in your teaching material. Have fun!
This case can be solved trivially by interval arithmetic. C++ code that solves your "sum of two interval-constrained variables problem" is given below.
int min_x = 30, max_x = 50;
int min_y = 60, max_y = 90;
// solutions exist in this interval
int min_z = min_x + min_y, max_z = max_x + max_y;
cin >> input;
// solutions possible?
if (input >= min_z && input <= max_z)
{
// determine solution interval for x (y is dependent)
cout
<< "Solution:\n"
<< "x in [" << min(max( input - max_y , min_x),max_x)
<< ";" << min(max( input - min_y , min_x),max_x) << "], "
<< "y = " << input << " - x" << endl;
}
else
{
cout << "No solution." << endl;
}
Computers are "basically stupid" and if they do smart things it is the software.
Using a general purpose programming language like C++ requires you (or at least
the libraries you eventually use) to be very specific on how exactly to solve
a problem based on the simple arithmetic means of the bare computer.
Although the programming language won't magically and somehow do things
for you, algorithms exist to solve many mathematical standard problems such
as e.g. systems of equations. Numerical Recipes in C++ covers a variety of
algorithms and their C++ implementations.

C++ program on determining triangle

I need help with the following code that requires me to:
Declare 3 double type variables, each representing one of three sides of a triangle.
Prompt the user to input a value for the first side, then
Set the user’s input to the variable you created representing the first side of the triangle.
Repeat the last 2 steps twice more, once for each of the remaining 2 sides of the triangle.
Use a series of nested if / else statements to determine if the triangle having side-lengths as set by the user is an EQUILATERAL, ISOSCELES, or SCALENE triangle.
[Note: look to the Wikipedia page on ‘triangle’ for definitions of these three types of triangles.]
Print the resulting triangle type to the console.
Ensure your Triangle detector works by running it 5 times as in the example above. You may use the same values as in the example.
I currently have:
//lab eleven program code on triangles
#include <iostream.h>
main()
{
//variables
float aside, bside, cside;
//enter side a
cout<<"enter the length of side a "<<endl;
cin>>aside;
//enter side b
cout<<"enter the length of side b "<<endl;
cin>>bside;
//enter side c
cout<<"enter the length of side c "<<endl;
cin>>cside;
// all sides equal
if(aside==bside && bside==cside)
cout << "Equilateral triangle\n";
// at least 2 sides equal
else if(aside==bside || aside==cside || bside==cside)
cout << "Isosceles triangle\n";
// no sides equal
else
cout << "Scalene triangle\n";
}
But I need help with the if and else if statements to determine the type triangle. Our professor has not covered this topic in class.
We use the program Ch 6.3 on Windows.
if(a==b && b==c) // all sides equal
cout << "Equilateral triangle\n";
else if(a==b || a==c || b==c) // at least 2 sides equal
cout << "Isosceles triangle\n";
else // no sides equal
cout << "Scalene triangle\n";
As your professor suggested, you should look at:
http://en.wikipedia.org/wiki/Triangle#Types_of_triangles
You should also look at:
http://www.teacherschoice.com.au/maths_library/trigonometry/solve_trig_sss.htm
Algorithm:
Solve for all angles, a1, a2, a3 (see the article above)
If you can't find a solution:
Output "Error: Not a valid triangle"
Else:
If (a1 == a2) && (a2 == a3):
Output "EQUILATERAL" and stop
If (a1 == a2) || (a2 == a3) || (a1 == a3):
Output "ISOSCELES" and stop
Output "SCALENE" and stop
Also note: Be careful about "equality" with floating point (float/double) values (such as angles). If you are doing such a comparison, you should usually use this instead:
abs(x - y) < epsilon
Where epsilon is a "sufficiently small value".
The logic falls out neatly from the definition of these different types of triangles, which as the professor notes, is information readily obtained from Wikipedia. It just involves a simple comparison of side lengths; you don't have to go as far as angles. But I'll give you some help with the "not a triangle" condition. Don't be afraid to put on your math hat here and go wandering in, a little logic isn't a bad thing for a poli sci student to endure every now and then. :-)
For the sides to make a proper triangle, for each pair of sides (I'll call them f and g), they must add up to greater than the third side's length (I'll call it h). If you're dealing with equilateral triangles, you automatically know this condition is met (why?). If you're dealing with isosceles or scalene triangles, you technically only need to check the smaller two sides against the largest side, and if it's true for them, it's true for the other two cases as well (why?). However, it may be just as convenient for you to check all three cases.
Looking at why this inequality has to hold: if the sum of two sides was exactly equal to the third side's length, you'd have a "degenerate" triangle where sides f and g could only lay on top of h! If they added up to less, the two sides could connect to the endpoints of h but then would never meet at a third point! You can test this yourself by cutting lengths of string or strips of paper and trying it out.
Three other things to think about:
In C++, double and float are not the same thing. One has less precision than the other. Make sure you use the one the professor asks for.
Checking to make sure the sides are non-negative is a great idea. You could probably reasonably rule out lengths of 0 as well, to eliminate the possibility of degenerate triangles that just look like line segments or points.
When comparing floating-point numbers, you should always be careful to consider whether a strict equality is going to get you what you want. For checking the equilateral/isosceles/scalene conditions, you're fine because the user is directly entering in the floating-point numbers and you're not manipulating them, so there's no chance for you to introduce error. But when checking the "not a triangle" condition, it's relatively easy to set up a situation where adding the two sides rounds off (because of the vicissitudes of floating-point arithmetic in the CPU) to something that's very close to, but not quite exactly, the third side. In those cases, if you want to catch degenerate triangles, what you usually do is pick an "epsilon" value (some very small value relative to the numbers you're dealing with) that represents the maximum amount of roundoff you're willing to tolerate. You then check whether the sum of f and g is somewhere between h - epsilon and h + epsilon – or put another way, whether the absolute value of f + g - h is less than or equal to epsilon. If it is, you claim that f + g = h (as best as you can tell) and deal with the degenerate case.
To complete this program you will need the following:
Make sure input is valid. In this case, input must be greater than 0. You could catch your input using a loop like
while (invar <= 0)
{
cout<<"Enter length"<<endl;
cin>>invar;
if (invar <= 0)
{
cout<<"invalid input"<<endl;
}
}
I am not sure if this is proper c++ syntax, I haven't used it in about 8 years.
you can do this for all 3 inputs. I would probably make a function to determine the triangle using 3 input variables and 1 return variable. The following is pseudo-code
if (a + b <= c) or (a + c <= b) or (b + c <= a)
{
return "you don't have a triangle."
}
else
{
if (a == b) or (a == c) or (b == c)
{
if (a == b and b == c)
{
return "equilateral"
}
return "isosceles"
}
return "scalene"
}
return -1
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
#include<math.h>
int main()
{
float Side1,Side2,Side3;
float Flag1,Flag2,Sum_of_sq1,Sum_of_sq2,Sum_of_sq3;
clrscr();
printf("Enter Three Sides Side1 Side2 Side3 :");
scanf("%f %f %f", &Side1 , &Side2 , &Side3);
Flag1=(Side1==Side2)?(Side2==Side3?1:0):((Side2==Side3)?0:-1);
if(Flag1==0)
{ printf("Triangle is Isoceles\n");
}
if (Flag1==1)
{ printf("Equilateral Triangle");
}
Sum_of_sq1=pow(Side1,2)+pow(Side2,2);
Sum_of_sq2=pow(Side1,2)+pow(Side3,2);
Sum_of_sq3=pow(Side2,2)+pow(Side3,2);
if (sqrt(Sum_of_sq1)==Side3 ||sqrt(Sum_of_sq2)==Side2 || sqrt(Sum_of_sq3)==Side1)
printf("The Triangle is Right Angled Triangle");
getch();
return(0);
}
#include<iostream>
using namespace std;
//create a class
class Triangle {
//declare three sides for the triangle
double side1;
double side2;
double side3;
public:
//constructor to initialize the data members
Triangle(double s1, double s2, double s3) {
side1 = s1;
side2 = s2;
side3 = s3;
}
void triangleType() {
//all sides equal
if((side1 == side2)&&(side2 == side3))
cout << "It is an Equilateral Triangle" << endl;
//at least two sides are equal
else if((side1 == side2) || (side2 == side3) || (side1 == side3))
cout << "It is an Isosceles Triangle" << endl;
//all are different
else
cout << "It is a Scalene Triangle" << endl;
}
};
int main() {
//local variable
double a_side, b_side, c_side;
//taking the user inputs
cout << "Enter the three sides of a triangle: " << endl;
cin >> a_side >> b_side >> c_side;
Triangle t1(a_side, b_side, c_side); //create an object of Triangle
t1.triangleType(); //call the function
return 0;
}

C++ reading a sequence of integers

gooday programers. I have to design a C++ program that reads a sequence of positive integer values that ends with zero and find the length of the longest increasing subsequence in the given sequence. For example, for the following
sequence of integer numbers:
1 2 3 4 5 2 3 4 1 2 5 6 8 9 1 2 3 0
the program should return 6
i have written my code which seems correct but for some reason is always returning zero, could someone please help me with this problem.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int x = 1; // note x is initialised as one so it can enter the while loop
int y = 0;
int n = 0;
while (x != 0) // users can enter a zero at end of input to say they have entered all their numbers
{
cout << "Enter sequence of numbers(0 to end): ";
cin >> x;
if (x == (y + 1)) // <<<<< i think for some reason this if statement if never happening
{
n = n + 1;
y = x;
}
else
{
n = 0;
}
}
cout << "longest sequence is: " << n << endl;
return 0;
}
In your program, you have made some assumptions, you need to validate them first.
That the subsequence always starts at 1
That the subsequence always increments by 1
If those are correct assumptions, then here are some tweaks
Move the cout outside of the loop
The canonical way in C++ of testing whether an input operation from a stream has worked, is simply test the stream in operation, i.e. if (cin >> x) {...}
Given the above, you can re-write your while loop to read in x and test that x != 0
If both above conditions hold, enter the loop
Now given the above assumptions, your first check is correct, however in the event the check fails, remember that the new subsequence starts at the current input number (value x), so there is no sense is setting n to 0.
Either way, y must always be current value of x.
If you make the above logic changes to your code, it should work.
In the last loop, your n=0 is execute before x != 0 is check, so it'll always return n = 0. This should work.
if(x == 0) {
break;
} else if (x > y ) {
...
} else {
...
}
You also need to reset your y variable when you come to the end of a sequence.
If you just want a list of increasing numbers, then your "if" condition is only testing that x is equal to one more than y. Change the condition to:
if (x > y) {
and you should have more luck.
You always return 0, because the last number that you read and process is 0 and, of course, never make x == (y + 1) comes true, so the last statement that its always executed before exiting the loop its n=0
Hope helps!
this is wrong logically:
if (x == (y + 1)) // <<<<< i think for some reason this if statement if never happening
{
Should be
if(x >= (y+1))
{
I think that there are more than one problem, the first and most important that you might have not understood the problem correctly. By the common definition of longest increasing subsequence, the result to that input would not be 6 but rather 8.
The problem is much more complex than the simple loop you are trying to implement and it is usually tackled with Dynamic Programming techniques.
On your particular code, you are trying to count in the if the length of the sequence for which each element is exactly the successor of the last read element. But if the next element is not in the sequence you reset the length to 0 (else { n = 0; }), which is what is giving your result. You should be keeping a max value that never gets reset back to 0, something like adding in the if block: max = std::max( max, n ); (or in pure C: max = (n > max? n : max );. Then the result will be that max value.