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

#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 :(

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

User input into array. I am confused with output

Most of the components for the array are in place.
I am however wondering what code is missing for the output to match what I am trying to do.
I tried searching for similar array coding. I would like to call the function and for the user to input numbers up to 20 different inputs.
#define size 20
using namespace std;
int i;
void Input(int student[]) {
for(int i = 0; i < size; i++)
cout << "Enter The Marks of Subject 2 of student no " << i + 1 << " ";
cin >> student[i];
}
void display(int student[]) {
for(int i = 0; i < size; i++)
cout << student[i];
}
int main() {
int student[size];
Input(student );
display(student);
return 0;
In your Input function:
void Input(int student[]) {
for(int i = 0; i < size; i++)
cout << "Enter The Marks of Subject 2 of student no " << i + 1 << " ";
cin >> student[i];
}
You not using brackets, so the cin >> student[i]; is outside of the loop. The i from the for loop is no longer in scope, so you are using the i here:
int i;
Which is never given a value, which leads to undefined behavior. Add brackets:
void Input(int student[]) {
for(int i = 0; i < size; i++) {
cout << "Enter The Marks of Subject 2 of student no " << i + 1 << " ";
cin >> student[i];
}
}

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;
}