I was solving a problem on codechef:
https://www.codechef.com/NITWMA01/problems/QPALIN.It required to input m number of input cases after getting the value of m from the user. I always used to run a loop of:
while(m--)
{//input test cases}, but in this problem I don't know why the loop is running less than m times when I have to get input cases m times. I tried running the code with the sample input(with m having 6) but main() returned 0 after getting just 4 inputs(and printint respective outputs wherever necessary).
My code is as follows:
#include<iostream>
#include<cstdio>
#include<string.h>
using namespace std;
int main()
{
int n,m,op,x,l,r,i,j,xorpair=0;
char k,s[100000];
scanf("%d",&n);
scanf("%s",s);
scanf("%d",&m);
for(j=0;j<m;j++)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d",&x);
scanf("%c",&k);
s[x-1]=k;
}
else
{
xorpair=0;
scanf("%d%d",&l,&r);
for(i=l-1;i<r;i++)
{
xorpair^=s[i]-'0';
}
if(xorpair==0)
{
printf("YES\n");
}
else printf("NO\n");
}
}
return 0;
}
PS: I have replaced cin with scanf. Also I believe I was not able to properly convey what problem actually I am facing. So here's the test case that explains it more clearly:
(What it should be like)
Sample input:
7
abbacca
6
2 1 4
1 1 z
2 1 4
1 4 z
2 1 4
2 5 7
Sample output:
YES
NO
YES
YES
Following is the problem that I am facing:
When I run the program for the above input, this is what appears on the output screen
7
abbacca
6
2 1 4
YES
1 1 z
2 1 4
NO
1 4 z
Process returned 0 (0x0)
I am not able to enter inputs 6 times and after 4th input it returns 0.
scanf("%c" does not do what you seem to expect. It reads just the ' ' before the char you want. Then the next tries at reading op and x fail leaving op still equal to 1, so you do two of operation 1 for every once that you intended to.
Related
Hi there? Here is my code, what could be the errors because it is correct when I run it?Write a program that reads a positive integer n, and prints the first
n even numbers.
For example, one execution would look like this:
Please enter a positive integer: 3
2
4
6
File Name
evennumbers.cpp
Score
There are three tests each worth 2 points
Autograder Results
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Please enter a positive integer:";
cin>>n;
cout<<"These are all positive integers in between 1 to "<<n<<" "<<endl;
for(int i=1;i<=n;i++)
if(i%2==0)
cout<<i<<" "<<endl;
return 0;
}
Evaluating Test 1 Output (0.0/2.0)
Test Failed: 'these are all positive integers in between 1 to 3 2' != '246'
these are all positive integers in between 1 to 3 2
246
Evaluating Test 2 Output (0.0/2.0)
Test Failed: 'these are all positive integers in between 1 to 2 2' != '24'
these are all positive integers in between 1 to 2 2
24
Evaluating Test 3 Output (0.0/2.0)
Test Failed: 'these are all positive integers in between 1 to 4 2 4' != '2468'
these are all positive integers in between 1 to 4 2 4
2468
There are two problems with your code.
Your program is not doing what was asked. It is currently printing every even number between 1 and n; when the problem asks you to print all the first n even numbers. So if n is 3, you should print all the first three even numbers, that is, 2 4 6.
Considering your program is being run through an automatic checker, you should print only what is asked. So don't print these are all positives (...). Print only the even numbers you should print, given the input number. For instance, if the input is 3, print only 2 4 6. Don't print these are all positive integers between 1 to 3 2 4 6.
This question already has answers here:
What is a debugger and how can it help me diagnose problems?
(2 answers)
Good debugger tutorial for beginners [closed]
(1 answer)
Closed 2 years ago.
Hello i was having some funny with my code lately and i met strange output of double recursion. I was trying to understand it but im not so lucky to know the answer yet. Maybe u ll help me with tis problem . Why this program has so weird output? output : 4 3 2 1 1 2 1 1 3 2 1 1 2 1 1. I figure out that firstly it just do first somefunc call so printing 4321 , next somefunc has output like 1234. firs it decrement 1 so its 0 next decrement 2 by 1 so its 1 and then decrement then 3 decrement by 1 its 2 and 1 , decrement 4 by 1 untill 0 so its 321 its logically output will be 4321 2 1 3 2 1 but i dont understand the rest.
#include <iostream>
using namespace std;
void somefunc(int c)
{
if(c>=1)
{
cout <<c<<" ";
somefunc(c-1);
somefunc(c-1);
}
}
int main()
{
somefunc(4);
return 0;
}
I was trying to solve a problem where I had to sort so I used the standard library std::sort function but I get a wrong output in the 2nd test case:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,i;
cin>>n;
int arr[n-1];
for(i=1;i<=n-1;i++)
cin>>arr[i];
int size=sizeof(arr)/sizeof(arr[1]);
sort(arr,arr+size);
for(i=1;i<=n-1;i++)
cout<<arr[i]<<" ";
cout<<endl;
}
return 0;
}
I/P:
2
5
1 2 5 4
10
1 2 3 4 5 6 7 8 10
Expected O/P:
1 2 4 5
1 2 3 4 5 6 7 8 10
Actual O/P:
1 2 4 5
2 3 4 5 6 7 8 2013562 10
First of all, int arr[n-1]; is a variable length array. That's not actually part of C++, even though some compilers will tolerate it nevertheless. In most cases you can just use std::vector<int>(n-1); instead.
But look at this loop:
for(i=1;i<=n-1;i++)
cin>>arr[i];
You're starting at 1 and got all the way up to n-1, however your array goes from arr[0] to arr[n-2]. So you got undefined behavior because you're writing one past the size of the array, and you also don't write to the first position (leading to more undefined behavior when you try to sort with this uninitialized value still present).
Instead, the loop should be for(i=0;i<n-1;i++) The same applies to where you're printing it. You can then sort the vector this way:
sort(arr.begin(), arr.end());
Also note that by doing n-1 you're always reading in and handling one less value that the user inputs, I'm not sure if that's your intention. If you want that, you could also just decrease n by one after reading it in instead of writing n-1 in multiple places.
I am using scanf to take input for a graph. The input is as follows :
8
1 2
3 3 5 6
2 4 7
2 3 8
2 1 5
1 7
2 6 4
0
The first integer (8) is the number of vertices, followed by 8 line. The first integer in each is the number of outgoing edges from vertex 1 in the first line, vertex 2 in the second line and so on.
The function i have written is as follows :
void getInput() {
//init();
int numVertex; int numTest;
scanf("%d", &numVertex);
for(int i =1 ; i <= numVertex;i++) {
int ver,nC; vector<int> vList;
//fscanf(file,"%d", &ver);
scanf("%d", &nC);
for(int j=0;j<nC;j++) {
int temp ;
scanf("%d", &temp);
vList.push_back(temp);
}
props pr = {-1,-1 , vList};
graph.insert(make_pair(i, pr) );
}
}
However, the output for the last line of my input becomes weird and it basically repeats the last digit of its previous line multiple times. For the above input , the output I am getting :
1 : 2
2 : 3 5 6
3 : 4 7
4 : 3 8
5 : 1 5
6 : 7
7 : 6 4
8 : 4 4 // this is where it should give nothing
Can anyone tell me whats going wrong here? The exact same sequences of conversions, when I convert to taking input through file gives me the right output.
Can someone please point me to any error?
Do it like this:
if (scanf("%d", &nC) != EOF) {
for(int j=0;j<nC;j++) {
int temp ;
scanf("%d", &temp);
vList.push_back(temp);
}
props pr = {-1,-1 , vList};
graph.insert(make_pair(i, pr) );
}
This will check if the read was successful. The repetition of the last line of input is a well known issue and occurs because the last read attempt fails (because end of file has reached) and scanf returns the same result as its previous call.
What is props?
Whatever it is, doing C-style struct initialization on something containing an std::vector is asking for trouble - the results are undefined and almost surely not what you want.
Your input code seems OK though, despite lack of error-checking as noted in another answer.
When the following program is fead the following input (reading from cin):
1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1
The output is surprising:
1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1
#include<iostream>
using namespace std;
int main()
{
int arey[3][3];
int i,j;
for(j=0;j<=3;j++)
{
for(i=0;i<=3;i++)
{
cin>>arey[j][i];
}
}
arey[0][0]=1;
arey[3][3]=1;
i=0,j=0;
for(j=0;j<=3;j++)
{
for(i=0;i<=3;i++)
{
cout<<arey[j][i];
}
}
return 0;
}
Can someone explain what I should change to get the same output as the input?
Is the matrix 3x3 or 4x4?
you created 3x3 but the loops run for 4 elements and you also update [3][3]
Basically your indexes overflow and you overwrite a different cell in the matrix.
Update: cheecked your input, use: int arey[4][4];
Arrays use 0 based indices, so the valid range of indices for your
int arey[3][3];
are 0 <= i < 3 and 0 <= j < 3
So you need to change the condition in your for loops to be strictly < instead of <=
I really don't think I understand your question, but this is wrong:
int arey[3][3];
...
for(j=0;j<=3;j++) // <= invalid
...
array[3][3]=1; // out of bounds
arey is a 3*3 array. You can't access arey[3][?], that's out of bounds. The only valid indices are 0..2.
Once you've written past the bounds of your array, your program behavior becomes undefined.