I need to write a program that has as input a floating point number e.g. 2.3162
The program then generates a range of floating point numbers and it must them identify which number within the range generated comes the closest to the given value:
This does not work. It gives the last number as the closeset - which is wrong. Is there a simple way to achieve this?
------------
#include <iostream>
#include <cmath>
#include <vector>
int main()
{
// place floating point values in a vector
std::vector<double> numbers;
for(int i = 0; i <= 10; ++i)
{
numbers.push_back(sqrt(i));
std::cout << numbers[i] << '\n';
}
const double myval(2.3162);
// variables to store the differnce?
double difference(0);
double smallestdifference(0); // arbitrary
//go through the vector and determine the closest number to myval 2.3162
for(int i = 0; i <= 10; ++i)
{
//THIS GIVE ME WRONG RESULTS
difference = myval-numbers[i];
std::cout << "difference is " << difference << '\n';
if(difference < smallestdifference)
smallestdifference = numbers[i];
}
std::cout << "The number with the smallest difference is " << smallestdifference << '\n';
}
------------------
Output gives:
0
1
1.41421
1.73205
2
2.23607
2.44949
2.64575
2.82843
3
3.16228
difference is 2.3162
difference is 1.3162
difference is 0.901986
difference is 0.584149
difference is 0.3162
difference is 0.080132
difference is -0.13329
difference is -0.329551
difference is -0.512227
difference is -0.6838
difference is -0.846078
The number with the smallest difference is 3.16228
---------------------------------
you should initialize smallestdifference to a high value or it would always be smaller than difference, difference should be set to abs( myval-numbers[i] )
and if difference is smaller than smallestdifference you should set smallestdifference to difference, not to numbers[i]
Unfortunately this is not working. Also i must find a solution without using abs() The output is giving me the number which has the greatest difference as the number with the smallest difference:
#include <iostream>
#include <cmath>
#include <vector>
int main()
{
std::vector<double> numbers;
for(int i = 0; i <= 10; ++i)
{
numbers.push_back(sqrt(i));
std::cout << "number " << i << " has a sqrt of " << sqrt(i) << '\n';
}
const double myval(2.3162);
double difference(0);
double smallestdifference(10); // arbitrary
std::cout << "My value is 2.3162 \n";
for(int i = 0; i <= 10; ++i)
{
std::cout << "number " << i << " with sqrt " << sqrt(i) << '\n';
difference = myval-numbers[i];
std::cout << " has a difference from my value of " << difference << '\n';
if(difference < smallestdifference)
smallestdifference = difference;
}
std::cout << "The number with the smallest difference from my value is " << smallestdifference << '\n';
}
--------------------
output
number 0 has a sqrt of 0
number 1 has a sqrt of 1
number 2 has a sqrt of 1.41421
number 3 has a sqrt of 1.73205
number 4 has a sqrt of 2
number 5 has a sqrt of 2.23607
number 6 has a sqrt of 2.44949
number 7 has a sqrt of 2.64575
number 8 has a sqrt of 2.82843
number 9 has a sqrt of 3
number 10 has a sqrt of 3.16228
My value is 2.3162
number 0 with sqrt 0
has a difference from my value of 2.3162
number 1 with sqrt 1
has a difference from my value of 1.3162
number 2 with sqrt 1.41421
has a difference from my value of 0.901986
number 3 with sqrt 1.73205
has a difference from my value of 0.584149
number 4 with sqrt 2
has a difference from my value of 0.3162
number 5 with sqrt 2.23607
has a difference from my value of 0.080132
number 6 with sqrt 2.44949
has a difference from my value of -0.13329
number 7 with sqrt 2.64575
has a difference from my value of -0.329551
number 8 with sqrt 2.82843
has a difference from my value of -0.512227
number 9 with sqrt 3
has a difference from my value of -0.6838
number 10 with sqrt 3.16228
has a difference from my value of -0.846078
The number with the smallest difference from my value is -0.846078
------------------------
10 with a square root of 3.16228 is the FAREST away from my value of 2.3162. The program must identify the closest number to this value.
It's because it's negative. You could use difference=sqrt(difference*difference). (I know about the pow() function, but I forgot it's exact syntax :) ).
Let's say we have a smallest difference of 0.5. Now we find a difference of -2. This will be set as new smallest difference, since -2<0.5 will return true. To avoid this, you can multiply it by itself, and then take the sqrt of it:
i tried to substitute the statement that you suggested but this does not produce the result that i need. Am i wrong in saying that you are suggesting i should remove the variable myval? The whole point of diference is to say how much a number differs from myval - without it the program is meaningless.
Suppose i want to find the closest positive floating point number to myval - the value of difference should not change if differnce goes below zero. So what i need is the closest positive number to zero.
How can i express this in code using a if statement? (to prevent difference from going below zero)
No, I'm not saying you should remove myval. You are searching the difference with some point and myval. This difference is the distance between those points; it's an abosolute value (it can only be positive).
De difference between 1 and 5 is 4, you will agree with me on that. But in your program it's found as -4, since 1-5 returns -4. Now, the normal syntax to find the absolute returnvalue, is by taking the squareroot of it's square. This will always return a positive value, like i showed with the examples of 5 and -5 in my previous post.
So before you check or the current difference is smaller as the smallest difference, you need to make sure "difference" is positive by using the code I showed previous.
Please can you change this program so it works to output the value which is CLOSEST to myval? I have re-written it from earlier:
Can you please incorporate the change that you have said would fix it? Thank you
int main()
{
std::vector<double> numbers;
for (int i = 1; i <= 100; ++i)
{
numbers.push_back(sqrt(i));
}
const double myval(2.3162);
double difference(0);
double smallestdifference(10); // arbitrary
std::cout << "My value is 2.3162 \n";
for (int i = 1; i <= 100; ++i)
{
std::cout << "number " << i << " with sqrt " << sqrt(i) << '\n';
// only change difference while value remains positive
if ((myval-sqrt(numbers[i])) > 0)
{
// identify the last positive number closest to myval
difference = myval - sqrt(numbers[i]);
std::cout << " has a difference from my value of " << difference << '\n';
if (difference < smallestdifference)
smallestdifference = difference;
}
else
difference = difference;
}
std::cout << "The number with the smallest difference from my value is " << smallestdifference << '\n';
}
I don't completly follow your new version and I dont have the time to figure that out now. Here is your old version with a line added so it will find the smallest difference:
#include <iostream>
#include <cmath>
#include <vector>
int main()
{
std::vector<double> numbers;
for(int i = 0; i <= 10; ++i)
{
numbers.push_back(sqrt(i));
std::cout << "number " << i << " has a sqrt of " << sqrt(i) << '\n';
}
constdouble myval(2.3162);
double difference(0);
double smallestdifference(10); // arbitrary
std::cout << "My value is 2.3162 \n";
for(int i = 0; i <= 10; ++i)
{
std::cout << "number " << i << " with sqrt " << sqrt(i) << '\n';
difference = myval-numbers[i];
std::cout << " has a difference from my value of " << difference << '\n';
difference = sqrt(pow(difference,2)); //make sure difference is positive
if(difference < smallestdifference)
smallestdifference = difference;
}
std::cout << "The number with the smallest difference from my value is " << smallestdifference << '\n';
std::cin.ignore();
}
Now it the program will find the smallest difference, now you need to add a variable in wich you can store wich number is the closest to myVal (in this case sqrt(5) ).