What is wrong with my program ?
It works fine on my PC but in IDEone it gives the correct output but shows runtime error. Please help.
#include<bits/stdc++.h>
using namespace std;
struct student
{
int vote;
};
int main()
{
int t;
cin>>t;
while(t--)
{
int count=0;
int n;
cin>>n;
vector <int> a(n);
student s[n];
int k;
cin>>k;
for(int i=1;i<=n;i++)
{
s[i].vote=0;
}
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int temp=0;
for(int i=1;i<=n;i++)
{
if(a[i] != i)
{
temp=a[i];
s[temp].vote++;
}
}
for(int i=1;i<=n;i++)
{
if(s[i].vote==k)
{
count++;
}
}
printf("%d\n",count);
}
return 0;
}
This is the error shown in IDEone :-
Error in `./prog': free(): invalid next size (fast): 0x085cca10
student s[n];
This declares an array called s. It contains n values. The values are s[0] through s[n-1] (you can count them all on your fingers, if you'd like, using a small number of n, such as 5).
for(int i=1;i<=n;i++)
{
s[i].vote=0;
}
This attempts to initialize values s[1] through s[n]. The only problem is that s[n] doesn't exist. The last value in the array is s[n-1]. This code will corrupt memory on the stack, resulting in undefined behavior.
The same bug also occurs with the a array.
The index values for both vector and array are zero based and goes up to n - 1. So
for(int i = 0; i != n; ++i)
would be better.
Now you write one element too far, which free finds out later when the data after the memory block is invalid.
Related
Why is it giving wrong count
In this question we have to find out how many elements are greater than the next element of an array
#include
using namespace std;
int main() {
//number of testcases
int t;
cin>>t;
while(t--){
//taking number of elements in an array
int n,count;
cin>>n;
//taking array
int a[n];
count=0;
for(int i=1;i<=n;i++){
cin>>a[n];
}
//checking array
for(int j=1;j<=n-1;j++){
if(a[j]>a[j+1])
{
count++;
}
}
cout<<count<<endl;
}
return 0;
}
Input
5
1 2 3 4 1
Expected Output
1
Output through above code
2
There are several problems with your code:
Problem 1
You're going out of bounds of the array a which leads to undefined behavior, when you wrote
cin >> a[n]; //undefined behavior
Note that indexing of arrays starts from 0 instead of 1 in C++.
Problem 2
In standard C++, the size of an array must be a compile-time constant. This mean the following is incorrect in your code:
int n;
cin>>n;
int a[n]; //NOT STANDARD C++ BECAUSE n IS NOT A CONSTANT EXPRESSION
Better would be to use std::vector instead of built in array.
int n = 0;
std::cin >> n;
std::vector<int> a(n); //this creates a vector of size n
//take input from user
for(int& element: a)
{
std::cin >> element;
}
int count = 0;
for(int i = 0; i< n - 1; ++i)
{
if(a[i] > a[i+1])
{
++count;
}
}
I Am Trying To Find Partition Of Array ,On Condition By Checking Variable x ,when less then x they will be on one side or else on another. but my code need some correction.
HERE am not able to find the error , i will be thankful to you if you help me.
Code is:-
#include<iostream>
using namespace std;
int partition(int arr[],int n,int x){
for(int i=0;i<n;){
if(arr[i]<x){
i++;
}
else if(arr[i]==x){
int temp=arr[i];
arr[i]=arr[n];
arr[n]=temp;
i--;
}
else if(arr[i]>x){
int temp=arr[i];
for(int j=i;j<n;j++){
arr[j]=arr[j+1];
}
arr[n]=temp;
i--;
}
}
return 0;
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int x;
cin>>x;
partition(arr,n,x);
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Input >> array={2,10,15,1,3,15} ,x=10
Expected << {2,1,3,10,15,15}
Output I get << nothing .
The code isn't giving any output because, first, the "cin" and "cout" are in upper case which is syntactically incorrect, secondly, the variable j is in different case in loop statement and body inside the second else-if clause in the partition function, same goes for the "I" in the first for loop in the main() function. Sort this out and you should be good to go.
First in C++ the size of an array must be a compile-time constant. So for example, consider the following examples:
int n = 10;
int arr[n]; //INCORRECT
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, in your code,
int n;
cin>>n;
int arr[n]; //INCORRECT because n is not a constant expression
Second, in your code, when you wrote:
arr[n] = temp; Undefined behavior
you're going out of bounds and so you have undefined behavior.
Solution
You can use std::stable_partition and std::vector to solve your problem as shown below:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
int n;
std::cout <<"Enter n:"<<std::endl;
std::cin >> n;
std::vector<int> arr(n); //create a vector of size n instead of an array
std::cout<<"Enter elements: "<<std::endl;
//iterate and take input from user
for(int &elem: arr){
std::cin >> elem ;
}
int x;
std::cout << "Enter x: "<<std::endl;
std::cin>>x;
//use std::partition
std::stable_partition(arr.begin(), arr.end(), [x](int i){return (i < x);});
std::cout<<"This is the partitioned vector: "<<std::endl;
for(int i=0;i<n;i++)
{
std::cout<<arr[i]<<"\t";
}
return 0;
}
Output
The output of the above program is as follows:
Enter n:
6
Enter elements:
2
10
15
1
3
15
Enter x:
10
This is the partitioned vector:
2 1 3 10 15 15
which can be seen here.
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cout<<"enter the no till which prime nos to be found :"<<endl;
cin>>n;
vector<int> prime(n+1,0);// we want n index too
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
for(int j=i*i;j<=n;j=j+i) {
prime[j]=1;
}
}
}
cout<<"prime numbers are: "<<endl;
for(int i=2;i<=n;i++) {
if(prime[i]==0) {
cout<<i<<endl;
}
}
return 0;
}
above code should be working for n>=100000 since n(log(log(n))) will equal to 400000.
Your i*i overflows a 32-bit int when n is more than about 46000. This causes undefined behavior. On my system, it wraps around and initializes j with a negative value, causing a segmentation fault when prime[j] is accessed.
This has nothing to do with time complexity.
I am neither using pointer nor using freed memory but i do not understand what is causing sigsegv error.
For some test cases the algorithm is working without any error while for other test case it is showing SIGSEGV.
#include<iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin>>n>>m;
int arr[n],arrh[m];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=0;i<m;i++)
{
cin>>arrh[i];
}
int arrcc[m][n]={0}; //Precomputation Cumulative
int val;
for(int j=0;j<m;j++)
{
val=0;
for(int i=0;i<n;i++)
{
if(arr[i]==(j+1))
{
val++;
}
arrcc[j][i]=val;
}
}
int q,l,r;
cin>>q;
int k;
int arrc[m];
while(q--)
{
k=0;
for(int i=0;i<m;i++)
{
arrc[i]=0;
}
cin>>l>>r;
// for(int j=0;j<m;j++) //Time limit exceed need precomputation
// {
// for(int i=(l-1);i<r;i++)
// {
// if(arr[i]==(j+1))
// {
// arrc[j]++;
// }
// }
// }
for(int j=0;j<m;j++) //Calculating from cumulative
{
if(l!=1)
arrc[j]=(arrcc[j][r-1]-arrcc[j][l-2]);
else
arrc[j]=(arrcc[j][r-1]-0);
}
for(int i=0;i<m;i++)
{
if(arrc[i]!=0)
{
if(arrc[i]!=arrh[i])
{
cout<<"0"<<"\n";
k++;
break;
}
}
}
if(k==0)
{
cout<<"1"<<"\n";
}
}
}
link to problem the problem-
https://drive.google.com/open?id=1sYEbtdFTT9ZE67y4Wygvv_AKJWGWgXiI
https://www.hackerearth.com/challenges/competitive/april-circuits-20/algorithm/happy-segments-e290faa6/
cin>>n>>m;
int arr[n],arrh[m];
Firstly, the program is ill-formed in C++. The size of an automatic array must be compile time constant, which n and m are not.
Secondly, in case you intentionally use an extended C++ language: The storage for objects in automatic storage is typically strictly limited. The size of the execution stack is by default about 1-8 mega bytes. For large n or m, those arrays may overflow the stack which, if you are lucky, will cause the program to crash.
If you need an array of runtime size, then use dynamic storage. Simplest solution is to use std::vector. This removes the chance of the array overflowing the stack, and makes the program well-formed C++ in regard to the array size.
I am writing a code to find number of pairs in a given array.But i am getting segmentation fault.
I am sorting the given array and then comparing the elements to get pairs.
#include <bits/stdc++.h>
using namespace std;
int sockMerchant(int n, vector<int> ar) {
sort(&ar[0], &ar[n]);
int k=1;
int m=0;
for(int i=0;i<n;i++){
for(int j=1;j<n;j++){
if (ar[i]=ar[j])
k++;
if(ar[i]!=ar[j])
i=j;
m+=k/2;
break;
}
}
return m;
}
int main()
{
int n;
cin>>n;
vector<int> ar;
for(int i=0;i<n;i++){
cin>>ar[i];
}
int k = sockMerchant(n,ar);
cout<<k<<endl;
return 0;
}
When you say
vector<int> ar;
It just creates an empty vector.
So your for loop is basically trying to write to the elements of an empty vector.
for(int i=0;i<n;i++){
cin>>ar[i];
}
You can either:
Call ar.resize(n); before the for loop
or
You can use vector::push_back function to let the vector resize dynamically as you enter your values.
for(int i=0;i<n;i++){
int tmp;
cin>>tmp;
ar.push_back(tmp);
}
P.S: You don't have to pass n to your function. you can use ar.size() to get the size of your vector