Pair runtime fault from std - c++

#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.

Related

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

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

after inputting the values how can i display the values under one bracket e.g "{6,7,8,9}

#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
}
return 0;
}
You can use std::cout like :
#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
if(i ==0)
std::cout<<"{" <<array[i];
else if(i == n-1)
std::cout<<","<<array[i]<<"}";
else
std::cout<<","<<array[i];
}
return 0;
}
#mystic's answer uses arrays, which works fine. You can also use vector. There are more advanced methods of iterating over a vector, but I have not included that here to keep it simple.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> intVector{};
int n;
int input;
cout << "No. of values : ";
cin >> n;
for (int i = 0; i < n; i++) {
cin >> input;
intVector.push_back(input);
}
// Print out the array
cout << "{";
for(int i = 0; i < intVector.size(); i++) {
cout << intVector[i];
// print out the comma, except for the last number
if(i < intVector.size() - 1) {
cout << ", ";
}
}
cout << "}" << endl;
return 0;
}
If you want to use an iterator for printing the array, you can replace the print loop with this code:
// Print out the array
cout << "{";
for(auto i=intVector.begin(); i!=intVector.end(); ++i) {
if (i != intVector.begin()) {
cout << ", ";
}
cout << *i;
}
cout << "}" << 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;
}

Prompt user to enter two 3x3 int arrays and check equivalence

Here is what I have come up with so far... I am so close with literally 1 error left for it to compile.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int SIZE = 3;
bool equals(const int m1[][SIZE], const int m2[][SIZE])
{
bool identical = true;
for (int i=0; i < SIZE && identical; i++ )
{
if (m1[i] != m2[i]){
identical = false;
}
}
return identical = true;
}
void printArray(const int m1[], int size)
{
for (int i = 0; i < size; i++)
cout << m1[i] << " ";
}
void printArray2(const int m2[], int size) //not sure if I need this 2nd
{ //void
for (int i = 0; i < size; i++)
cout << m2[i] << " ";
}
int main()
{
string input1, input2;
int const SIZE = 3;
double inputnumber;
int m2[SIZE];
int m1[SIZE];
cout << "Please enter the first array: " << endl;
getline(cin, input1);
stringstream ss (input1);
ss >> inputnumber;
cout << "Please enter the second array: " << endl;
getline(cin, input2);
stringstream si (input2);
si>>inputnumber;
for (int i=0; i< inputnumber ; ++i) {
ss >> m1[i];}
if (equals(m1, SIZE)){
cout << "The two arrays are identical ! ";
}
else{
cout << "The two arrays are NOT identical !";
}
cout << endl;
return 0;
}
Or at least I think that I am close... any help is much appreciated.
My current error is coming up on the IF statement in the main function. I could have more mistakes as I am very very new to C++. Like I said please help me out if you can.
If you just want code here it is , but if you want to practice I recommend you to implement error checking(incorrect format) in this code and to change the loops in my code into functions.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string inp;
cout<<"Please enter an array of numbers in the following form\n"
"(where N's are any numbers) :\n"
"[N N N\nN N N\nN N N"<<endl;
double m1[3][3],m2[3][3];
for(short i=0;i<3;i++){
cout<<"> ";
if(i==0) cout<<"[ ";
getline(cin,inp);
stringstream inps(inp);
for(short j=0;j<3;j++){
inps>>m1[i][j];
}
}// Get the first array
cout<<"Now enter the second array :\n";
for(short i=0;i<3;i++){
cout<<"> ";
if(i==0) cout<<"[ ";
getline(cin,inp);
stringstream inps(inp);
for(short j=0;j<3;j++){
inps>>m2[i][j];
}
}// Get the second one
bool not_equal=false;
for(short i=0; i<3 && !not_equal ;i++)
for(short j=0; j<3 && !not_equal ;j++)
if(m1[i][j]!=m2[i][j])
not_equal=true;// Test equality
if(not_equal)
cout<<"The two arrays you entered are not equal !!"<<endl;
else
cout<<"The two arrays you entered are EQUAL ."<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int size = 3;
int m2[size][size];
int m1[size][size];
cout << "Please enter the first array: " << endl;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cin >> m1[i][j];
}
}
cout << "Please enter the second array: " << endl;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cin >> m2[i][j];
}
}
int flag=1;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(m2[i][j]!=m1[i][j])
{
flag=0;
}
}
}
if(flag==1)
{
cout <<"SAME"<<endl;
}
else if(flag==0)
{
cout <<"NOT SAME"<<endl;
}
return 0;
}

C++ factorial program doing strange math

Very new to c++. Making this factorial program, and somehow I've scraped through the code and built something, but the value it returns is always 10 times the correct value. For example, factorial for 5 is computed at 1200 instead of 120, and factorial of 3 is computed at 60. Why the extra unit?
This is the code:
#include <iostream>
using namespace std;
int main() {
int i, j, num;
cout << "Compute factorial: " << endl;
cin >> num;
for (i=1; i<num; i++){
for (j=num; j>1; j--)
num *=(j-i);
cout << num;
}
return 0;
}
All you need is one loop. This way should be much simpler.
#include <iostream>
using namespace std;
int main() {
int i, j=1, num;
cout << "Compute factorial: " << endl;
cin >> num;
for (i=num; i>0; i--){
j *= i;
}
cout << j << endl;
}
for (i=1; i<num; i++){
num *=i;
}
This should be enough
I think you are not quite clear about for loop,and be careful with the change of num
try this piece of code, hope it will help
#include <iostream>
using namespace std;
int main() {
int i, j, num;
cout << "Compute factorial: " << endl;
cin >> num;
int output_num = 1;
for (i=1; i<=num; i++){
output_num *= i;
}
cout << output_num;
return 0;
}
Use this code :
#include<iostream>
using namespace std;
int main()
{
int num,fact=1;
cout<<" Enter the no. to find its factorial:";
cin>>num;
for(int a=1;a<=num;a++)
{
fact=fact*a;
}
cout<<"Factorial of a given Number is ="<<fact<<endl;
return 0;
}
Output :
using two loops for computing factorial is very complex..... and you do not need it.
Try this
#include <iostream>
using namespace std;
int main() {
int i,num;
cout << "Compute factorial: " << endl;
cin >> num;
for(i=num;i>1;--i)
num*=(i-1);
cout << num;
return 0;
}