What is truncation in C++? - c++

I'm having my code compiled successfully, in most of the cases it works perfectly well, but on some cases containing long and great numbers of inputs to my array the result turns into something strange and minus, and the compiler shows 'truncated' in front of the last input. What is wrong with it? When does this happen?
Here's my code:
#include<iostream>
using namespace std;
int main()
{
int n, index, max, deletion;
cin>>n;
int ar[n];
long long freq[100]={0};
for (int i = 0; i < n; i++)
{
cin>>ar[i];
}
for (int i = 0; i < n; i++)
{
index=ar[i];
freq[index]++;
}
max=freq[0];
for (int i = 0; i < 101; i++)
{
if(max < freq[i])
{
max=freq[i];
}
}
deletion=n-max;
cout<<deletion<<endl;
return 0;
}

Related

Why the output is not showing?

My program is to find the smallest positive number missing from an array. With the following input I expect an output of 2.
6
0
-9
1
3
-4
5
My problem is that it does not give any output. Can anyone explain this please?
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int array[n];
for (int i = 0; i < n; i++)
{
cin >> array[n];
}
int const N = 1e4+2;
bool indexarray[N];
for (int i = 0; i < N; i++)
{
indexarray[i] = false;
}
for (int i = 0; i < n; i++)
{
if (array[i] > 0)
{
indexarray[array[i]] = true;
}
}
int ans = -1;
for (int i = 1; i < N; i++)
{
if (indexarray[i] == false)
{
ans = i;
}
}
cout << ans << endl;
return 0;
}
I think because int array[n]; makes an array called array with n elements in it, with the first one starting at array[0].
cin >> array[n]; needs to modify array[n], but because the first element is array[0], the last element is array[n-1], and array[n] does not exist. Your code gave an error and exited.
Try changing
for (int i = 0; i < n; i++)
{
cin >> array[n];
}
to
for (int i = 0; i < n; i++)
{
cin >> array[i];
}
Also, I think variable length arrays are non-standard, so maybe try changing that. Replace it with std::vector<int> array(n) should work.

the program is stuck while giving output

I dont know why the program is failed to give output, This is the code to find number of zero's
in a given array
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
int b=0 , a;
for(int j=0; j<n; j++)
{
a=arr[j];
while(a==0)
{
b=b++;
}
}
cout<<b;
}
Try changing it to
if (a==0) //you had a while here
{
b=b++;
}
Since you are not changing the value of 'a' inside the loop, it gets stuck inside the while and this leads to an infinite loop!
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int b = 0, a = 0;
for (int j = 0; j < n; j++)
{
a = arr[j];
if (a == 0) //here you used while use if as you are checking the condition every iteration of loop.
{
b++; //here instead of using b=b++ use this.
}
}
cout << b;
return 0; //return is optional.
}
just do this in the j for loop. you don't need any a variable or anything
and also runtime variable arrays are not allowed in C++ use dynamic arrays or vectors
for(int j=0; j<n; j++)
{
if(arr[j]==0){
b++;
}
}

How do I resolve sigabrt error on codechef ide

Sigabrt runtime error occurs of a fatal error, because of an assert statement not returning true? Or use of excessive memory, I'm not able to figure out what I'm doing wrong here, help me out?
( problem 1343 C on codeforces) link
so here's the code.
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
long int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while (i<n && check(i,a)== s) {
if (a[i] > max)max = a[i];
i++;
}
b.push_back(max);
}
int s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
cout << s << endl;
}
}
I have debugged your code and also the modified code has been accepted for the above question.
Mistakes you made:
1. In the below loop, value at i'th index of vector<int> b is being added to long int s. Instead, b[k] should be added to long int s because the variable being used in the loop is k not i.
for (int k = 0; k< b.size(); k++) {
s += b[i];
}
2. In the question, range of variable n is given as (1 ≤ n ≤ 2.10^5). So, it is safe to use int n instead of long int n. Also, when I submitted my code on codeforces it gave me signed integer overflow error when I used long int n.
3. You need to use long long s instead of long int s because the value of each element of array A lies between (−10^9 ≤ a[i] ≤ 10^9 , ai ≠ 0) and when we add the elements it can easily surpass int and long int ranges.
4. Although, the answer got accepted when I used vector<int> a in the function
int check(int i,vector<int> a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
But as the user Scheff has said and is correct that it comes with a penalty in space and time, you should use call by reference i.e. vector<int> &a.
Modified Code:
#include <iostream>
#include <stdlib.h>
#include<vector>
using namespace std;
int check(int i, vector<int> &a) {
if (a[i] > 0) {
return 1;
}
else return 0;
}
int main() {
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> a(n), b;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int i = 0;
while (i < n)
{
int max = a[i];
int s = check(i,a);
i++;
while ((i<n) && (check(i,a)== s)) {
if (a[i] > max)
max = a[i];
i++;
}
b.push_back(max);
}
long long s = 0;
for (int k = 0; k< b.size(); k++) {
s += b[k];
}
cout << s << endl;
}
}
Screenshot of Accepted Answer:

I am getting a segmentation error in PRIME1 on spoj. How should I eradicate it?

My answer to problem PRIME1
please explain me where am I wrong. I am receiving a segmentation error.
here it is:
#include<cstdlib>
#include<iostream>
using namespace std;
int main(int argc, char** argv) {
int t=0,i=0,m=0,n=0;
cin>>t;
while(t--&&t<=10)
{
cin>>m>>n;
if(m>=1&&n-m<=100000)
{
int prime[n];
for(i=0;i<n;i++)
prime[i]=1;
for (int i=2; i*i<=n; i++)
{
if (prime[i] == true)
{
for (int j=i*2; j<=n; j += i)
prime[j] = false;
}
}
for (int k=m+1; k<n; k++)
if (prime[k])
cout <<k<<endl;
}
}
return 0;
}
for (int j=i*2; j<=n; j += i)
prime[j] = false;
this is what causes the bug, prime is an array of size n, meaning the index of its last member is n-1, this for statement tries to access prime[n] at its last iteration(j<=n, meaning n is still valid) to fix this change the condition to:
for(int j=i*2; j<n; ++j)
edit:
perhaps the segmentation fault occurs due to bad logic of the code or poor formatting, after formatting the code, it compiles fine and does not cause a segmentation fault (tried with input {1,2,3} and {10,10,10} and a few more combinations):
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int t=0,i=0,m=0,n=0;
cin>>t;
while(t--&&t<=10)
{
cin>>m;
cin>>n;
if(m>=1&&n-m<=100000)
{
int prime[n];
for(i=0;i<n;i++)
{
prime[i]=1;
}
for (int i=2; i*i<=n; i++)
{
if (prime[i] == true)
{
for (int j=i*2; j<=n; j += i)
{
cout << "poopy\n";
prime[j] = false;
}
}
}
for (int k=m+1; k<n; k++)
{
if (prime[k])
{
cout <<k<<endl;
}
}
}
}
return 0;
}

C++ program is crashing for some unknown reason

I started learning C++ recently, and thought I would test my mettle with Project Euler problems. I solved the first two, but I am stuck on the third. It is compiling correctly without any errors, but it is crashing as soon as it is executed. I tried removing the nested for loops to isolate the problem, and it still crashed.
#include<iostream>
#include<math.h>
int main()
{
float quot;
int num = 0;
int array[100];
float next;
for(int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
if((i % j) == 0)
{
quot=j/i;
num=num+1;
}
if (num=2)
{
array[i]=i;
}
}
}
for (int i = 0; i < 100; i++)
{
if((13195 % i) == 0)
{
std::cout << i;
}
}
}
In if((i%j)==0) if i and j are zero, your next line is dividing i and j. This is division by zero.