C++ Checking if a double is within .1 of another double (+/-) - c++

I'm running a bit of code where I need to compare two 2D arrays for any variance. I've tried using the following line of code to check and compare the values, but the test fails every time = if(arr1[a][b] != arr2[a][b] || arr1[a][b] + .1 != arr2[a][b] || arr1[a][b] - .1 != arr2[a][b]) {.
I know this is failing because of the || statement, because one of the requirements is met. So I've got to find another way to determine if the double stored in a specific location in the array matches the other array in the parallel location.
Here's my full code:
int numberOfFailedCompares = 0;
for(int a = 0; a < 20; a++) {
int b = 0;
while(b < 20) {
if(arr1[a][b] != arr2[a][b] || arr1[a][b] + .1 != arr2[a][b] || arr1[a][b] - .1 != arr2[a][b]) {
numberOfFailedCompares++;
cout << numberOfFailedCompares << endl;
}
b++;
}
}
Is there a statement in C++ which will allow me to check if the value is within the +/- .1 threshold? Something like
if(arrLocation1 (+/- .1) == arrLocation1) {
...
}

"Variance" means "within X", and not "equal to something plus X or something minus X". Instead of comparing for equality, you compare for less/greater than your variance. So, for example, to test for variance of +/- .1:
if (b >= a-.1 && b <= a+.1)

How about this?
#define eps .1
...
if( fabs(x-y) <= eps )
...

Related

not getting a proper sum

I'm trying to get ready for an exam for my year one programming course. I need my if statement to return a sum with the number of characters before the user enters a 'p' or 'P' in a 10 character string. However, I can't seem to get it to count properly.
for (int i=0;i<=strlen(toProcess);i++){
if (toProcess[i]!='p'|| toProcess[i]!='P'){
sum=sum+1;
printf("%c ", toProcess[i]);
}
}
if (toProcess[i] != 'p' || toProcess[i] != 'P') {
Short of some quantum-mechanical-weird universe where something can be p and P at the same time, this condition will always be true(a):
if it's neither p nor P: true || true -> true.
if it's p: false || true -> true.
if it's P: true || false -> true.
You need to use && for this particular case:
if it's neither p nor P: true && true -> true.
if it's p: false && true -> false.
if it's P: true && false -> false.
And, of course, if you want to stop after finding the first p or P, you need to break out of the loop as well. You could either do that as an else block for your (corrected) condition:
int sum = 0; // probably needed.
size_t len = strlen(toProcess); // cache for efficiency.
for (int i = 0; i < len); i++) {
if (toProcess[i] != 'p' && toProcess[i] != 'P') {
sum++;
printf("%c ", toProcess[i]);
} else {
break;
}
}
Or, you could change the condition to just be an early exit:
int sum = 0; // probably needed.
size_t len = strlen(toProcess); // cache for efficiency.
for (int i = 0; i < len); i++) {
if (toProcess[i] == 'p' || toProcess[i] == 'P')
break;
sum++;
printf("%c ", toProcess[i]);
}
You'll notice in both those cases, I've also changed <= into <. A ten-character string has character positions 0..9 so those are generally the indexes you should use. I've also ensured that, for efficiency, we're only calling strlen() once, on the assumption the string doesn't change while processing.
(a) Of course, my physicist buddies will tell me that the act of observing toProcess[i] will cause it to collapse to one or the other, so this isn't technically correct. Leave it to physicists to spoil an otherwise funny physics joke :-)

Solving a dp problem from codeforces - Cut Ribbon

I was trying to solve this problem and from the comments section in the editorial, I was directed to the following solution :
#include <bits/stdc++.h>
using namespace std;
#define MAX(a,b,c) max(a,max(b,c))
int n,a,b,c,dp[4001];
int f(int x)
{
if (x == 0) return 0;
if (x < 0 || (x > 0 && x < a && x < b && x < c))
return 0xACCE97ED; // <- **I have doubt here**
if (!dp[x]) dp[x] = MAX(f(x-a),f(x-b),f(x-c)) + 1;
return dp[x];
}
int main()
{
cin >> n >> a >> b >> c;
memset(dp,0,sizeof(dp));
cout << f(n) << endl;
}
I wanted to know:
What is the need of the if statement that returns 0xACCE97ED for the test case:
4000 1 2 3. This test case dosen't work when that specific if statement is missing.
Why specifically 0xACCE97ED is being returned? Because when I tried to return any other number (say 9999), then the output is expected output + 9999.
if (x < 0 || (x > 0 && x < a && x < b && x < c))
return 0xACCE97ED; // -1395746835
Well looking at the dp function, it is basically maximizing values and this specific if statement is saying:
if x < 0
the length of the ribbon you cut is negative (which should be impossible)
or if x > 0 and x < a, b, c which means you can still cut X but all available sizes would result into having a ribbon of negative length
return 0xACCE97ED; return a random negative value which happens to spell out ACCEPTED because this state is invalid
And since the third if statement will try to get the max value, 0xACCE97ED will never be selected as the max value.
0xACCE97ED means "ACCEPTED" in the 1ee7 speech. nothing else specific about this value.
What is the need of the if statement that returns 0xACCE97ED for the test case: 4000 1 2 3
if (x < 0 || (x > 0 && x < a && x < b && x < c))
return 0xACCE97ED; // <- **I have doubt here**
because the function f is recursive, in the next line it calls itself:
if (!dp[x]) dp[x] = MAX(f(x-a),f(x-b),f(x-c)) + 1;
return dp[x];
with a smaller values for x so presumable it will eventually make that if statement true and will return "accepted" (0xACCE97ED).

After input my program seems to skip the first IF statement and go directly to ELSE

I have a problem with the code. It compiles with no errors, but right after taking input from the user it even with correct values it seems to skip the first conditional statement and go directly to ELSE which causes the program to terminate. I can't seem to find the cause for this behavior.
I think it might the issue with the way the conditional statement is constructed:
if( ((S <= 25 && S <= 75) % 5 == 0) && (U < 0.2 && U < 0.7) ){
I need to check if the value entered is 25 <= S <= 75 and is divisible by 5, as well as the other value being 0.2 < U < 0.7
Course Assignment
//#include "stdafx.h" // Header File used VS.
#include <iostream>
//#include <iomanip> // Used to format the output.
#include <cstdlib> // Used for system().
#include <math.h> // Used for sqrt().
using namespace std;// ?
int main (){
int S; // Gram/Litre
double U; // Specific Max. Growth Rate. Per Hour.
double D; // Maximum Dilution Rate.
const int K = rand() % 7 + 2; // Saturation Constant - Randomly Gegerated Number Between 2 & 7. In Hour/Litre.
cout << "Enter value between 25 and 75, divisible by 5, for S in Gram/Litre: ";
cin >> S;
cout << "Enter value bigger than 0.2, but less than 0.7, for U per Hour: ";
cin >> U;
if( ((S <= 25 && S <= 75) % 5 == 0) && (U < 0.2 && U < 0.7) ){ // Check Condition ***May Need Adjustments***
D = U * ( 1 - sqrt ( K / ( K + S) ) ); // Might have to adjust values to fit data type double. Add .00
cout.precision(3); // Prints 3 values after decimal point.
cout << "Maximum dilution rate is: " << D << endl;
if( D < 0.35 && D < 0.45 ){
cout << "Kinetic parameters are acceptable." << endl;
}
else{
cout << "Kinetic parameters are not acceptable." << endl;
}
}
else{
cout << "Invalid Input. Program will now terminate." << endl;
}
system("PAUSE"); // Pauses the program before termination.
return 0;
}
First, if you want 25 <= S <= 75, you should have
25 <= S && S <= 75, not S <= 25 && S <= 75. Same with U < 0.2 and D < 0.35 - they should be 0.2 < U and 0.35 < D.
Second, the above statement returns a boolean - thus, if S is a value between 25 and 75, the boolean will be cast to an integer value of 1, and 1 % 5 == 0 always be false. (Similarly, if S is outside this range, the boolean will be cast to an integer 0 and 0 % 5 == 0 will always be true)
A correct, complete if-statement is as follows:
if((25 <= S && S <= 75) && (S % 5 == 0) && (0.2 < U && U < 0.7)){
...
if(0.35 < D && D < 0.45){
...
}
...
}
If you read a number between 25 and 75 from the input, the if( ((S <= 25 is always false.
You must use if( ((S >= 25 && ....
The problem mainly lies in your loop conditions. For example, in this line from your code:
if( ((S <= 25 && S <= 75) % 5 == 0) && (U < 0.2 && U < 0.7) ){
//...
}
The if condition S <= 25 && S <= 75 simply can be rewritten as S <= 25 because in words, your parameter states that if S is less than or equal to 25 OR if S is less than or equal to 75, and so on.
The same problem exists here: U < 0.2 && U < 0.7. Once again, the if statement simply checks whether U is less than 0.2 and U is less than 0.7, the latter always being true if the former is true.
However, in your output statement before accepting the 2 inputs, you state that S should have a range of 25 <= S <= 75, meaning that S is greater than 25; not less. The same issue for U: you are expecting input ranging between 0.2 < U < 0.7.
How you should rewrite your if-then statement is as follows:
if( (S >= 25 && S <= 75) && (S % 5 == 0) && (U > 0.2 && U < 0.7) ){
//...
}
This not only makes your if-statement's conditions easier to read and comprehend, but it also eliminates the errors. This should work now. The meaning of the code stays the same: S has to be between 25 and 75 (including these numbers), it should be divisible by 5, and U should be between 0.2 and 0.7.
BTW, the same mistake exists in this part of your code also:
if( D < 0.35 && D < 0.45 ){...
I fixed it below:
if( D > 0.35 && D < 0.45 ){...
Good luck!

Compare two string as numeric value

How should I compare two string representing numbers in C++? I thought of converting to number of long long type but the problem is the numerical value represented by string can exceed the long long MAX limit.
It is guaranteed that the string represents a numerical value.
There is a similar question in Java compare two numeric String values.
but that makes use of the BigInteger Library that we don't have in C++.
Compare them digit by digit:
a = "3254353245423345432423133423421"
b = "3254353245423345432443133423421"
for(int i = 0; i < a.length(); ++i):
if ((a[i] - '0') < (b[i] - '0'))
{
std::cout << "b is larger!"
}
I'm sure you can take it from here if you want to find out whether b is larger than a, or if they are equal. Alternatively, if they are different lengths, the larger one wins! (Check for zeros at the beginning, i.e. "000443342") Don't forget to consider negative numbers.
If you think about it a bit, its not hard. For simplicity we will assume two positive numbers with no leading zeros. If they have leading zeroes, discard them.
Now consider two numbers:
123456
23456 Its apparent that the first one is larger, because it is longer. This allows us to quickly solve most comparisons. Now, if they have equal length, just compare them from the beginning. The number with smaller leading digit is smaller. If they are equal, take the next digit.
Now what about other cases? Well, one positive and one negative number is easy, the negative one is smaller, period. If you have two negative numbers, then you have to do the same thing as you would when comparing two positive numbers, but this time the number with larger leading digit is smaller.
As was pointed to me in the comments, std::string already implements lexicographical comparison, which means that you only have to sanitize the strings into valid numbers, call std::string::compare and decide whether -1 means smaller (positive numbers) or larger (negative numbers).
If you are comparing two strings as integers,
chances are you are going to find yourself wanting to do other math as well.
Use GMP and save yourself the headaches.
#include <iostream>
#include <gmpxx.h>
int main(){
mpz_class x("12323423434534234234234");
mpz_class y("9994828945090011626439");
std::cout << std::boolalpha;
std::cout << (x < y) << '\n';
}
//example compilation: g++ mycxxprog.cc -lgmpxx -lgmp
This may be overkill for your particular problem, as it increases the project dependencies.
Be sure to consider other options.
Takes 2 strings as input, returns 1 if 2nd(b in this case) is larger, 0 otherwise.
int find(char a[],char b[])
{
int i = 0;
int flag = 0;
if(a[0] == '-' && b[0] !='-')
{
printf("%s is larger: %s is -ve",b,a);
return 1;
}
else if(b[0] == '-' && a[0] !='-')
{
printf("%s is larger: %s is -ve",a,b);
return 0;
}
for(i = 0; i < strlen(a); ++i)
{
if(a[i] == '.' && b[i] != '.')
{
printf("%s is larger: %s is .",b,a);
return 1;
}
else if(b[i] == '.' && a[i] != '.')
{
printf("%s is larger: %s is .",a,b);
return 0;
}
else if(a[i] == '.' && b[i] == '.')
{
printf("passed\n");
continue;
}
if ((a[i] - '0') < (b[i] - '0'))
{
flag = 1;
//break;
}
}
if(flag == 0)
return 0;
else
return 1;
}

If condition issue (C++)

I have an issue with a piece of code in which I have an if statement that checks the diagonal elements of a complex array, and if they are (1,0) (meaning the real part is 1 and the imaginary part is 0) or very close to it then proceed; otherwise stop. But for some reason the operation always returns 0 although the elements are (0.999999, 0.000000), (1.000001, 0.000000), etc.
Here's the code I have:
for (i=1; i<=n; i++){
if ((real(c[i][i][1]) < (1/100000)+1) && (imag(c[i][i][1]) < 1/100000) && (real(c[i][i][1]) > (-1/100000+1) && (imag(c[i][i][1]) > -1/100000)){
cout<<"Operation continues...";
}
else
return 0;
}
Thanks in advance.
This integer division yields 0 because the magnitude of the denominator is greater than that of the numerator:
1/100000
You need to use at least one floating point number here. For example
1.0/100000
1 / 100000 == 0 as it is integer division:
you may write 0.00001 or 1. /100000.
BTW, you may write your loop:
constexpr double thresold = 0.00001;
for (i = 1; i <= n; i++) {
if (std::abs(real(c[i][i][1]) - 1.) < thresold
&& std::abs(imag(c[i][i][1]) - 1.) < thresold) {
cout << "Operation continues...";
}
else
return 0;
}