C++ Giving wrong output for a simple math calculation [duplicate] - c++

This question already has answers here:
Why does long long n = 2000*2000*2000*2000; overflow?
(6 answers)
C++ integer overflow
(4 answers)
Closed 1 year ago.
So my code looks like this:
unsigned long i=(27984 * 619246) + (1402 * 615589);
cout<<" i= "<<i;
I don't use the variable i anywhere else etc
Output looks like this
i= 1012166658
The correct answer is 18192035842 .
WHY is this happening?

Try this:
#include<iostream>
#include <string>
using namespace std;
int main() {
unsigned long long i = (27984ULL * 619246ULL) + (1402ULL * 615589ULL);
cout << " i= " << i;
}
using the long long literal suffix LL, and upgrading the type to a bigger type solves the problem.
I use the U (unsigned) literal suffix so that we can have it the same as the resulting type.
With your original code, there was an overflow (the result did not fit in the original type, or the operator) so it returned a truncated version. An example compiler warning that I got:
warning C4307: '*': signed integral constant overflow

Related

Number of permutations calculation in c++ [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 6 months ago.
I am learning c++ and having a bit of trouble with my homework.
We have to write code using the double variable type and use two variables to calculate the number of permutations of the potential team arrangements…
The question specifies that there are 18 people in the group and you want to divide the group into teams of 3 members.
This is my current code:
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
int fact(int n) {
if (n == 0) return 1;
if (n > 0) return n * fact(n - 1);
}
int main()
{
double n = 18.0;
double r = 3.0;
double answer = fact(n) / (fact(r) * fact(n - r));
cout << “The number of arrangements = “ << answer << endl;
system(“pause”);
return 0;
}
When I run the code I am receiving “The number of arrangements = 1”
This is not the correct answer. Can someone please help me figure out what I am doing wrong?
Thanks!
Your fact function is returning an int, not a double.
Since your factorial function is returning an int, fact(n) is an int, and fact(r) * fact(n - r) is also an int.
Together, they will perform integer division, i.e. floor division, not true division.
Edit: Looked at the code again, I realized the problem wasn't integer division, it was the fact that int overflowed, where as double wouldn't overflow until 171!.

Why is leetcode modulo operator of negative numbers = 0? [duplicate]

This question already has answers here:
Why doesn't a negative number modulo a vector size give a negative number? [duplicate]
(5 answers)
Closed 1 year ago.
I was working on a practice coding question on leetcode in c++, and I found that using the modulo operator on negative numbers returned 0 when it should not be returning 0. For context, I was testing -4 % 3, which returns 0. Does anyone know why this happens?
Here is my code:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vec {1,2,3};
int k{4};
std::cout << (-k) % vec.size();
return 0;
}
This prints:
0
It is because in C++ when one operand of operator % is unsigned long long then other operand is also converted to it.
Unsigned number can not be negative.
So int -4 is implicitly converted to unsigned long long 18446744073709551612
that happens to divide by 3.

Why does C++ programs return a code instead of the answer when it's too big? [duplicate]

This question already has an answer here:
Why do I get a negative number by multiplying two short ints?
(1 answer)
Closed 2 years ago.
I am coding a C++ program to calculate a math problem. I first write:
int x = 1
int y = 1
int z = 1
int answer = x * y * z
cout << answer << endl;
And when I build and run that, it prints 1. But when I do this:
int x = 1234;
int y = 5243;
int z = 1142;
int answer = x * y * z;
cout << answer << endl ;
It prints out instead a answer, which is 7388502404, but a code I don't understand: "-1201352188".
Could you explain why does this code happens, and how to solve the code?
It's not a code, it's the value as stored in the integer result. You're experiencing integer overflow. An int can only hold values of a certain size, and when you exceed that size you end up with bit patters that aren't what you want but that are still storable in an int. Sometimes those bit patterns are the integer representations of negative numbers which is what's happened here.
You might consider using a larger integer type such as long or long long.

Type conversion char to int to use as an index of a vector C++ [duplicate]

This question already has answers here:
Convert char to int in C and C++
(14 answers)
Closed 4 years ago.
In my program I have a string of caracters that are all digits and I want to use them as indexes to get values from a vector. But the output of this code is a 15 digit number, so not 1 which is vect[0] i.e. what I thought it would output.
char c = '0';
vector<int> vect = {1};
int i = (int)c;
cout<<vect[i];
I have seen that converting from a caracter to an integer is done with
int i=(int)c;
So i really don't get how that is not an appropriate type for an index.
First, it is bad practice to use C-style casts (int i = (int) c;), use a static cast instead (int i = static_cast<int>(c);). But indeed, as other mentioned, char are promoted to int automatically.
Then character '0' is not the same as the index 0. Have a look:
int i = c;
std::cout << i << std:endl;
If you want to use the character '0' as index 0, use:
int i = c - '0';

Why a+=b*pow(10,c-i-1) == 99 c++? [duplicate]

This question already has answers here:
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 4 years ago.
I wrote this code and first time of loop result is 99. Why is result 99, when it should be 100?
#include <iostream>
#include<math.h>
using namespace std;
int main ()
{
int skt = 0;
int sk[3];
int nsk = 3;
sk[0]=1;
sk[1]=2;
sk[2]=8;
for (int i=0; i<nsk; i++)
{
skt = skt + (sk[i]*pow(10.0,nsk-i-1));
cout <<" "<<skt<<endl;
}
}
the result of this code
99
119
127
,but if I use library cmath it is correct answer
#include <iostream>
#include<cmath>
using namespace std;
int main ()
{
int skt = 0;
int sk[3];
int nsk = 3;
sk[0]=1;
sk[1]=2;
sk[2]=8;
for (int i=0; i<nsk; i++)
{
skt = skt + (sk[i]*pow(10.0,nsk-i-1));
cout <<" "<<skt<<endl;
}
}
the result of this code
100
120
128
Could anybody explain why?
The math.h and cmath versions of pow are slightly different. Basically there are more supported inputs for the cmath version. You can compare the two at these links.
math.h and cmath
As people have commented, it's probably related to variable type conversion and floating point errors. Pow operates on doubles, which can have floating point errors. Chances are pow is returning a value like 99.9999999 which is then being converted to an integer. Since the integer conversion truncates instead of rounds, you get 99.
from this link cplusplus.com
Additional overloads are provided in this header ( cmath ) for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).
I bet if you cast it it would be correct in math.h