identifier "__int128_t" is undefined - c++

I am trying to use a 128 data type "__int128_t" in my VSCode while writing a C++ program, but I am getting an error saying identifier "__int128_t" is undefined. However, it works perfectly on my friend's PC. I thought since he installed the MinGW GCC compiler recently so there must be an issue with my version as it is comparatively old as compared to his, so I updated my g++ to version 11.2.0. Still, even after this, "__int128_t" is not working in my C++ program which I am writing in VS Code. This is my code.
#include <bits/stdc++.h>
using namespace std;
void fastIO()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int main()
{
fastIO();
long long input[100000], n;
cin >> n;
__int128_t sum1 = (n * (n + 1)) / 2;
__int128_t sum2 = 0;
for (int i = 0; i < n - 1; i++)
{
cin >> input[i];
sum2 += input[i];
}
cout << sum1 - sum2;
return 0;
}

Related

Why I am getting different output on HackerRank than the output of my IDE?

I am trying to solve a problem named "Jumping on the Clouds" on HackerRank.
I have written a code primarily and it gives the right output as my expectations. But when I am submitting the code on HackerRank it gives different output with the same input. How it is possible!
I tried to compile in different IDE and text editors like CodeBlocks, VSCode, and an online compiler(Ideone) and they give the correct output but HackerRank is showing a different output.
My code (C++):
#include <iostream>
using namespace std;
int main() {
int n, count = 0;
cin >> n;
int arr[n];
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
for(int i = 0; i < n; i++) {
if(arr[i + 2] == 0) {
count++;
i = i + 1;
} else if(arr[i + 1] == 0) {
count++;
} else {
continue;
}
}
cout << count << endl;
return 0;
}
Outputs:
Output on VSCode:
Output on Ideone:
Output on HackerRank:
Problem link: Jumping on the Clouds
Where is the problem and how to solve it?
Your code has undefined behavior because arr is accessed out of bounds. The loop constraint is i < n, but you access arr[i + 2] and arr[i + 1].

My program displays the result only in case of 1 numera, task: sort out all simple numeras from array

I need to complete the task, meeting the following requirements:
Algorithms for input and output of array elements, as well as for performing a given operation on an array, should be formatted as functions.
When accessing array elements, use a pointer.
In principle, my program should work, but it works only for 1 number, for 2 it already shows some kind of weird, and for 3 or more - nothing at all.
#include <iostream>
#include <stdlib.h>
using namespace std;
void input(int x[], int n);
int spisok(int x[], int n);
int main() {
int n, x[n];
cout << "Vvedite kolichestvo chisel v massive" << endl;
cin >> n;
input(x, n);
spisok(x, n);
for (int i = 0; i < n; i++) {
if (x[i] != 0) {
cout << "Prostiye chisla " << x[i] << ' ';
}
}
return 0;
}
void input(int x[], int n) {
int * p = x;
for (int i = 1; i <= n; i++) {
cout << "Vvedite " << i << "-oe chislo" << endl;
cin >> * p;
p++;
}
}
int spisok(int x[], int n) {
int i = 0;
for (int j = 2; j <= x[i]; j++) {
if (i > n) {
break;
}
if (x[i] % j == 0 && x[i] != j) {
x[i] = 0;
break;
}
i++;
}
return *x;
}
As you actually did not ask a question I take the liberty to interpret it as: How to find the mistake?
Further I am assuming that you use gcc. The answer for different compilers is similar (check their manual).
It might be a bit of a surprise, but as a matter of fact, gcc does not compile standard C++ with its default settings.
Unless you know what you are doing you should at least use the flags:
-pedantic to make it compile the code as standard C++
-Wall to make it report most common warnings
-Werror to report warnings as errors
With that flags your code produces following error message:
<source>: In function 'int main()':
<source>:10:12: error: ISO C++ forbids variable length array 'x' [-Werror=vla]
10 | int n, x[n];
| ^
<source>:10:15: error: 'n' is used uninitialized in this function [-Werror=uninitialized]
10 | int n, x[n];
| ^
cc1plus: all warnings being treated as errors
The first is because array sizes must be compile time constants. See Why aren't variable-length arrays part of the C++ standard? for details and use a std::vector for dynamically sized arrays instead.
The second is because you use n before it is initialized. You read n from user input, but that is after you declare the array in this line int n, x[n];. Code is executed from top to bottom. The value of n doesn't magically travel back in time after read from user input to use the right size for the array.

Invalid type error in c++ code in output[left]=input[i] line

I am getting this
'invalid types int*[std::ios_based&(std::ios_base&)]' for array subscript in line
output[left]=input[i];
Can't able to figure out the solution to it. here output is the array in which i want my answer
#include <bits/stdc++.h>
using namespace std;
void recover(int input[], int output[], int n)
{
int i = 0;
int mid = n / 2;
if (n % 2 != 0)
{
output[mid] = input[i];
int left = mid - 1;
int right = mid + 1;
i++;
}
else
{
int left = mid - 1;
int right = mid;
}
while (i < n)
{
output[left] = input[i];
left--;
i++;
output[right] = input[i];
right++;
i++;
}
}
int main()
{
int input[100], output[100];
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> input[i];
}
recover(input, output, n) for (int i = 0; i < n; i++)
{
cout << output[i] << " ";
}
}
First of all you have a scope problem, as Richard Critten pointed out in comments. right and left do not have the definition in the while loop that you gave them in the if and else statements.
Compounding this, you use #include <bits/stdc++.h> and using namespace std;. These are not causing your issue, but they are making it harder to understand.
Using #include <bits/stdc++.h> is a bad idea. It includes the entirety of the C++ standard library, which is lazy, unnecessary, and increases compile times. It's also not portable.
Using using namespace std; is a bad idea. It makes everything in namespace std available without qualification. This means any common names like left and right may match something you didn't intend. This can lead to bugs that are horrendously difficult to find and fix.
Using both together is really awful, because now you have the entirety of the std namespace, with lots and lots of commonly used names, colliding with the names you want to use in your code.

Compiler difference between GCC for c++11 and USACO compiler

For the USACO training gateway exercise humble numbers, I have a solution that works for the first 4 test cases, but then for test case 5 which has been supplied below to circumvent the need for input files, my program gets the correct answer locally when compiled by GCC and on my phone ( not sure what compiler). If I submit the answer to USACO it does not give the correct answer. From what I read that means the program has some undefined behaviour somewhere, but I could not find anything.
#include <fstream>
#include <iostream>
#include <set>
using namespace std;
//ifstream fin("humble.in");
//ofstream fout("humble.out");
set<unsigned long> cont;
int main() {
int K = 6;
int N = 25000;
unsigned long values[]= {2, 3, 5, 7, 11, 13};
cont.insert(1);
unsigned long r = 0;
for (int i = 0; i <= N; i++) {
r = *cont.begin();
for (int j = 0; j < K; j++)
cont.insert(r*values[j]);
cont.erase(r);
}
cout << r << endl;
return 0;
}
The answer I get locally (and the correct answer) is 682628310.
The answer I get on USACO is 67108864.

Library incompatibility? - C++

I'm trying to solve 56th problem of Project Euler and I have some troubles while using the Big Integer Library. Here is the code :
#include <iostream>
#include "BigInteger.hh"
#include <cmath>
using namespace std;
int digitSum(BigInteger x){
int num[(int)floor(log10(x))+2];
int n = 0;
BigInteger sum = 0;
while (x != 0){
sum += x - floor(x/10) * 10;
x = floor(x/10);
n++;
}
return sum.toInt();
}
int main(int argc, const char * argv[])
{
int max = 0;
for (int i = 1; i <= 100; i++){
for (int j = 1; j <= 100; j++){
cout << "i = %i, j = %i ",i,j;
if (digitSum(pow(i,j)) < max)
max = digitSum(pow(i,j));
}
}
cout << digitSum(pow(2,63));
return 0;
}
The problem is that when I try building, the compiler gives error on the lines using the log10, floor, and pow functions saying that they are used but not defined. When I comment the line #include "BigInteger.hh", everything goes fine but this time, of course, I can't use the Big Integer library.
Why is there such a problem? How to solve it?
Is it a compiler or a linker problem? If latter, try adding -lm to the command line to link the math library. Assuming GNU gcc here.