error: static_assert failed due to requirement '!is_signed<int>::value' "" static_assert((!is_signed<_Tp>::value), ""); [duplicate] - c++

This question already has answers here:
Why __gcd() is throwing error in macOS mojave?
(3 answers)
Closed 1 year ago.
note: in instantiation of function template specialization 'std::__1::__gcd' requested here
while (__gcd(n, k) <= 1) n++;
The above line was displayed along with the error shown earlier, I know there are many other methods to calculate gcd but I am confused why it's not working for (__gcd() ).
I am using MacBook : OS -> BgSur
Apple clang version 12.0.0 (clang-1200.0.32.29)
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int getSum(int n)
{
int sum;
for (sum = 0; n > 0; sum += n % 10, n /= 10)
;
return sum;
}
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
int k = getSum(n);
while (__gcd(n, k) <= 1) n++;
cout << n << endl;
}
}
here, getSum(125) = 1+2+5 = 8
INPUT :
3
11
31
75
OUTPUT:
12
32
75
EXPECTED OUTPUT:
12
33
75

As static assert suggests, your __gcd() implementation requires unsigned types for arguments (i.e. algorithm operates on non-negative numbers only), so replacing int with unsigned int should help - or you could replace
#include <bits/stdc++.h> // never include this
#include <cmath>
with
#include <iostream>
#include <numeric> // std::gcd
and use the GCD function that is included in the standard library instead:
while (std::gcd(n, k) <= 1) n++;

Related

Why does this program not give out binary output [duplicate]

This question already has answers here:
Why isn't `int pow(int base, int exponent)` in the standard C++ libraries?
(11 answers)
The most efficient way to implement an integer based power function pow(int, int)
(21 answers)
Closed 2 days ago.
I'm new to C++ and this program that I wrote does not give the output, which is a binary form of an integer input.
It does give the result in Python Tutor. But in VSCode the result is always one less than the actual binary output.
Example-
5 = 100
6 = 109
17 = 10000
#include <iostream>
#include <cmath>
int main(){
int n;
std::cout << "Enter n:- ";
std::cin >> n;
int ans = 0;
int i = 0;
while (n != 0){
int bit = n & 1;
ans = (bit * pow(10, i)) + ans;
n = n >> 1;
i++;
}
std::cout << ans;
return 0;
}
What did I do wrong?

bitwise conversion of decimal numbers to binary

The code works fine for some values like for eg 10 the output is 1010 which is correct but for 20 or 50 or 51 the output is wrong or atleast seems so to me.
please help !
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
cin >> n;
int ans = 0;
int i = 0;
while (n != 0)
{
int bit = n & 1;
ans = (bit * pow(10, i)) + ans;
n = n >> 1;
i++;
}
cout << " Answer is " << ans << endl;
}
change datatype of ans.
float ans = 0;
After trying to run your code, it works. 51 correctly comes out as 110011 and 50 as 110010 and 20 as 10100. Those are the correct bit values, you can try calculating them by counting or by just adding 10 (i.e. 1010) in different ways.

Fraction pattern in c++ [duplicate]

This question already has answers here:
C++. Dividing 1 by any number gives 0
(3 answers)
Closed 1 year ago.
I need to write a program to run this pattern in c++:
S=1/2+2/3+3/4+4/5+...+N-1/N
I have tried but my code is showing 0.
And its the code that I have written:
#include <iostream>
using namespace std;
int main()
{
unsigned int N;
float S=0;
cout << "Enter N:";
cin >> N;
for (int I = 2; I <= N; I++)
{
S = S + (I - 1) / I;
}
cout << S;
return 0;
}
I have to write it with for-loop, while and do-while
(I - 1) / I only contains integers, therefore any remainder is discarded.
You can avoid this by simply subtracting - 1.f off of I instead.

Squared numbers in loop output

I have this code here that should output all squared numbers before n. For example, if you type 10, it will show 1 4 9. The problem is, when I input 25, it should've given output 1 4 9 16 25. But, instead, it shows 1 4 9 16 24
#include <iostream>
using namespace std;
#include <cmath>
int main(){
int a, b;
cin >> a;
for(int i = 1; i <= a; i++)
{
b = pow(i,2);
if (b <= a) cout << b << " ";
}
return 0;
}
if you only use integers, and squared numbers you don't need any math library for the solution, you can get the square of a number by multiplying it to itself.
And you can have better performance if you break out of the loop when you reach the first square number that is bigger than your input number, no need to calculate any more.
example code:
#include <iostream>
int main()
{
int a;
std::cin >> a;
for (int b, i = 1;; ++i)
{
b = i * i;
if (b > a)
break;
std::cout << b << " ";
}
return 0;
}
Your problem is likely caused by the pow() function. While I was not able to reproduce this error (cmath in MinGW and Visual C++ uses std::pow that has integer overloads since C++98), I believe Your compiler uses the float overload for some reason. You should try replacing b = pow(i,2) with b = i * i.

Code runs but shows no output after inputting a value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to implement dp on a fibonacci sequence using vector.If i declare memo globally as an array with given size it runs fine.But while using vector it shows no output on the console.
What seems to be the problem here?
#include<bits/stdc++.h>
using namespace std;
int fib(int n)
{
vector<int>memo;
int f;
if(memo[n]!=0)
return memo[n];
if(n<=2)
return 1;
else
f = fib(n-1)+fib(n-2);
memo.push_back(f);
return f;
}
int main()
{
int num;
cin>>num;
cout<<fib(num);
}
The problem here is your declaration of memo:
vector<int>memo;
It is not static, and thus goes out of scope every time the function exits. However, you seem to expect it to still be in scope when the function exits.
Thus, make it static:
static vector<int>memo;
Side note: I would check that n is less than memo.size() before trying to do something like if(memo[n]!=0), because if n is greater than the size, then I believe this is undefined behavior.
Side note 2: You shouldn't include bits/stdC++.h
Here's the corrected code.
#include <iostream>
#include <vector>
using namespace std;
int fib (int n)
{
static vector<int>memo = {1, 1}; // should be static. also init it.
int f;
if (n < memo.size () && memo [n] != 0) // check the size of vector before accessing
return memo [n];
if (n <= 2)
return 1;
else
f = fib (n - 2) + fib (n - 1); // n-2 should be found and inserted before n-1
memo.push_back (f);
return f;
}
int main ()
{
int num;
cin >> num;
cout << fib (num);
}
There were three main issues with the code.
memo should have been declared as static. Previously with each call to fib(), it was creating a fresh 'memo' variable.
memo[n]!=0 might cause a segfault since the vector could be small. You should check the size before referencing nth item.
You were pushing the n'th value to (n-2)'th place. So let's first initialize the vector with {1,1}
Now the series will be generated as...
1 1 2 5 8 13 21 34 55
There are some problems in your code:
You are not allocating space on memo and this is why you get no output.
memo must be static so it lives over the recursion and you can get real memoization
You are using memo.push_back(f);. This defeats memoization because the indices will not correspond to what you want to find in memo
Fixing these issues leads to a code like this:
#include<iostream>
#include<vector>
using namespace std;
int fib(int n)
{
static vector<int> memo(n + 1, 0);
if (n > memo.capacity() - 1)
memo.resize(n + 1, 0);
int f;
if(memo[n]!=0)
return memo[n];
if(n<=2)
return 1;
else
f = fib(n-1)+fib(n-2);
memo[n] = f;
return f;
}
int main()
{
int num;
cin >> num;
cout << fib(num) << endl;
}
The reason nothing is shoring it is the program is running, but you don't know. Change to this it will show.
cout << "Enter a number : ";
cin >> num;
because you are using recursive, in the fib() function, you create memo multiple times. This will return "program terminated with signal 11 segmentation fault"
When you first call fib(), there's nothing in the memo, so you cannot do memo[n].
Hope this helps.
I figured it out.
I changed the checking statement
if(memo[n]!=0)
to this if(!memo.empty()).
And the rest are all the same.
This approach - recursion with memoization - is much easier with a map:
#include <iostream>
#include <map>
using namespace std;
int fib(int n) {
static map<int, int> memo; #pairs are n, f
int f = 0;
if (memo.count(n) > 0)
return memo.at(n);
if (n <= 2 )
return 1;
else
f = fib(n - 1) + fib(n - 2);
memo.emplace(n, f);
// cout << n << " " << f << endl; #run this with and w/o 'static' to see effect
return f;
}
int main() {
for (int i = 1, i < 12, i++)
cout << fib(i) << " ";
cout << endl;
}
gives:
1 1 2 3 5 8 13 21 34 55 89
[Edit] : Here's the vector version, it needed this loop inside however:
int fibVec(int n) {
static vector<int> memo;
int f = 0;
for (int i = 0; i < memo.size(); i++) { //search the vector by index
if (i == n)
return memo[i];
}
if (n <= 2 )
return 1;
else
f = fib(n - 1) + fib(n - 2);
memo.push_back(f);
return f;
}