Why this RE(SIGSEGV) Error comes in C++ while submitting? - c++

In the time of code submission I got RE(SIGSEGV) error.
My code is:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T,val,v;
int min = INT_MAX;
int N,M;
cin >> T;
while(T--){
cin >> N >> M;
vector<int> vec(M+1,0);
vector<int> arr1;
for(int i=0;i<N;i++){
cin >> v;
arr1.push_back(v);
}
for(int j=0;j<N;j++){
cin >> val;
vec[arr1[j]]+=val;
}
for(int i=1;i<=M;i++){
if((vec[arr1[i]])<min && (vec[arr1[i]])!=0){
min = vec[arr1[i]];
}
}
cout << " box: "<<endl;
for(int a=0;a<vec.size();a++){
cout << vec[a] << " ";
}
cout << endl;
cout << min << endl;
vec.clear();
arr1.clear();
}
return 0;
}
Problem link: https://www.codechef.com/MARCH20B/problems/CHPINTU .
Can anyone tell me why is this happening? How can I overcome this problem?
Thank you.

N could be smaller than M, in that case this could lead to segmentation errors:
for(int i=1;i<=M;i++){
if((vec[arr1[i]])<min && (vec[arr1[i]])!=0){
min = vec[arr1[i]];
}
}
You should be instead looking over vec[i] instead of vec[arr1[i]]
Nit: this would still give you WA(Wrong Answer) but will not result in SIGSEGV, one of the errors I can find is only initializing min at start, instead of initializing it with INT_MAX in the loop over T

Related

keeping/removing a reduntant cout in my cpp program changes my answer

#include <iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t-->0){
int n;
int a[n];
int max=0;
cin >> n;
for(int i=0; i<n; i++){
cin >> a[i];
}
max=a[0]+a[1]+a[2];
for(int i=3; i<n; i++){
if((a[i]+a[i-1]+a[i-2])>max){
max=a[i]+a[i-1]+a[i-2];
}
}
// cout << "max = " << max << endl;
if((a[n-1]+a[n-2]+a[0])>max)
{
max=(a[n-1]+a[n-2]+a[0]);
}
if((a[n-1]+a[1]+a[0])>max)
{
max=(a[n-1]+a[1]+a[0]);
}
cout << max <<endl;
}
return 0;
}
This is a simple cpp program for this problem : https://www.codechef.com/COG2020/problems/COG2002#.
When I am adding a print statement, which is reduntant then it gives correct answer. And when I remove it, then I get a wrong answer.
My input is :
2
3
1 2 2
3
1 2 2
Please tell what could be the possible reasons. I have searched google for any explanations but couldn't find any :(

Finding Max, Min and Mode in c++

So basically I am trying to write a program which accepts an array of integers and then outputs the Max Min and smallest Mode and how many times it occurs.
I have been able to find both max and min and mode, but instead of the smallest mode my code outputs the one that occurs first. And i am not sure how to handle an input with more than one mode.
Below i’ll post my code:
#include
using namespace std;
int main() {
int p,max,min,mode;
cout << "Enter the number of postive integers:"<< endl;
cin >> p;
int pos[p];
cout << "Now enter postive integers!" <<endl;
for(int i=0; i<p; i++) {
cout << "Positive integer " << i+1 << ":";
cin >> pos[i]; }
max =pos[0];
min =pos[0];
for( int i=0; i<p; i++){
if(pos[i]> max) max=pos[i];
if(pos[i]< min) min=pos[i];
}
cout << "Max=" << max << endl;
cout << "Min=" << min << mode= pos[0];
int count[20];
int t=0;
for(int c=0;c<p; c++)
{
for(int d=0;d<p;d++)
{
if(pos[c]==pos[d])
{
count[c]++;
t++;
}
}
int modepos, maxno=count[0];
for(int e=1;e<p;e++)
{
if(maxno<count[e])
{
maxno=count[e];
modepos=e;
}
}
mode=pos[modepos];
if(t==1) {
cout << "There is no positive integer occuring more
than once." << endl;
}
else {
cout <<"The most occuring positive integer is:"<< mode;
cout << "\nIt occurs " << t << " times." << endl;
} return 0; }
there may be simpler and better ways to code this but since i’m a beginner and have only learned loops/conditionals/arrays/variable declaration etc I can only use them in the program, any help will be appreciated.
Do you learn about std::map? The algorithm for counting how many times of an element on an array is very simple with std::map
std::map<long, long > mapFreq;// first: for store value of array pos, second for store value's counter
mapFreq.insert(std::pair<long, long>(pos[0], 1));
for(int i = 1; i < dsize; i++)
{
auto &it = mapFreq.find(pos[i]);
if(it != mapFreq.end())
{
it->second++;
}
else
{
mapFreq.insert(std::pair<long, long>(pos[i], 1));
}
}
Then you can loop through map Freq for what you need:
int number, counter;
for(auto it : mapFreq)
{
if(it.second < counter)
{
number = it.first;
counter = it.second;
}
}
Maybe you can try doing this:
#include <iostream>
#include <algorithm> //for sorting
using namespace std;
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[n];
sort(a,a+n);
int b[a[n-1]];
for(int i=0;i<a[n-1];i++) b[i]=0;
for(int i=0;i<n;i++) b[a[i]]++;
cout<<"Largest number = "<<a[n-1]<<endl;
cout<<"Smallest number = "<<a[0]<<endl;
int rep=0;//repetition
int mode=0;
for (int i=0;i<a[n-1];i++){
if(b[i]>rep){
rep=b[i];// set times of repetition
mode=i;// set new mode
}
}
cout<<"Mode = "<<mode<<endl;
}

Why I can't find correct output of set theory?

I want to print cardinality of a set using array or vector and membership of an element.
I am able to find cardinality but but can't find membership in same program.
Here is my code..
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v;
int ch;
int j;
for(int i=1; cin>>j; i++){
v.push_back(j);
}
cout << v.size();
cout << " enter element you want to find whether it is a memebr or not: ";
cin >> ch;
for(int i=0; i < v.size(); i++){
if(v[i] == ch){
cout << "element found";
break;
}
}
return 0;
}
See what you did is taking an input during runtime inside a loop that too at condition validation place.Since to terminate this you gave a char. It throws an exception for that.So your program is not working as expected. Dont ever do that. Always ask for the size and then iterate the loop for that many number of times. Here is the code which is working :
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v;
int ch;
int j,temp;
int size;
cin>>size;
for(int i=1; i<=size; i++){
cin>>temp;
v.push_back(temp);
}
cout << " enter element you want to find whether it is a memebr or not: ";
cin >> ch;
for(int i=0; i < v.size(); i++){
if(v[i] == ch){
cout << "element found";
break;
}
}
cout<<v.size();
return 0;
}
Hope this would help you :)
Try this:
#include <iostream>
using namespace std;
#include<vector>
#include<stdlib.h>
int main(){
vector<int> v;
int ch;
int j;
cout<<"Enter the size of set:"<<endl;
int n;
cin>>n;
for(int i=0; i<n ; i++){
cin>>j;
v.push_back(j);
}
cout << v.size()<<endl;
cout << " enter element you want to find whether it is a memebr or not: ";
cin >> ch;
for(int i=0; i < v.size(); i++){
if(v[i] == ch){
cout <<endl<< "element found";
exit(0);
}
}
cout<<endl<<"Element not found.";
return 0;
}

Pair runtime fault from std

#include<iostream>
#include<set>
using namespace std;
main(){
int n,m;
set<int> number;
int num;
int query[m];
for(size_t i=0;i<n;i++){
cin >> num;
number.insert(num);
}
for(size_t j=0;j<m;j++){
cin >> query[j];
}
for(int l=0;l<m;l++){
for(auto k:number){
if(number.find(query[l]-k)!=number.end()){
cout << "YES" << endl;
break;
}else{
cout << "NO" << endl;
break;
}
}
} }
Why my code can't run ?
When I compile and I run it, the execution says this:
How can I run this code?
#include <iostream>
#include <set>
using namespace std;
int main(){
int n,m;
set<int> number;
int num;
cin >> m >> n;
int query[m];
for(size_t i=0;i<n;i++){
cin >> num;
number.insert(num);
}
for(size_t j=0;j<m;j++){
cin >> query[j];
}
for(int l=0;l<m;l++){
for(auto k:number){
if(number.find(query[l]-k)!=number.end()){
cout << "YES" << endl;
break;
} else {
cout << "NO" << endl;
break;
}
}
}
}
This code allocates the array "query" with m (inserted by user) elements on the stack at run time. The new C++ compilers provide this capability to allocate the memory on the stack itself at run time for local variables.

Simple Array Sum in c++

I'm beginner in C++, and I've got a question about a simple sum code in c++.
Here is my code :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
int sum;
int arr_i = 0;
cin >> n;
vector<int> arr(n);
while (arr_i != n)
{
cin >> arr[arr_i];
sum += arr[arr_i];
//cout << sum << endl;
if (arr_i == n - 1)
cout << sum;
arr_i++;
}
return 0;
}
The output doesn't print the correct answer without "cout << sum" before the if condition.
How can I solve this problem ?
You forget to initialize sum to 0.
int sum = 0;
As the previous post mentioned, sum was not initialized to 0. In terms of good practices and styles its a good idea to initialize any variables that are modified within a loop right before the loop body so that someone reading the code can easily grasp the context of your variables.
int main()
{
int n;
int sum;
int arr_i;
cin >> n;
vector<int> arr(n);
sum = 0;
arr_i = 0;
while (arr_i != n)
{
cin >> arr[arr_i];
sum += arr[arr_i];
//cout << sum << endl;
if (arr_i == n - 1)
cout << sum;
arr_i++;
}
return 0;
}
or as I prefer a "for" loop...
int main()
{
int n;
int sum;
int arr_i;
cin >> n;
vector<int> arr(n);
for (sum = 0, arr_i = 0; arr_i != n; arr_i++)
{
cin >> arr[arr_i];
sum += arr[arr_i];
//cout << sum << endl;
if (arr_i == n - 1)
cout << sum;
}
return 0;
}