-1

i am getting the output for this is as 0112. Please help me in telling why am i getting that output when the the input is:- 4 1 2 2 4 5 6 6 10 and the output that i am expected to get is 2.

Tina and Rahul have 1 toy each. They are entering into an amusement park but it is not allowed to take the toys inside, so they have to keep it in the boxes provided by the park management. They want to keep the toys together in one box. There are N boxes in total, at this moment there are t i toys present in i t h box and the maximum capacity of the box is denoted by c i . Rahul and Tina want to know in how many boxes can they keep their toys such that both the toys are in the same box.

Input format The first line of the input contains integer N , denoting the count of boxes. Each of the next N lines contains two integers t i and c i denoting the number of toys present in the i t h box and the maximum capacity of that box.

'''

#include <bits/stdc++.h>
using namespace std;

int main()
{
  int n,x,y,i,count=0,z;
  cin>>n;
  for(i=0;i<n;i++)
  {
    cin>>x>>y;
    z=y-x;
    if(z>=2)
    {
      count++;
    }
    cout<<count;
    
  }
  
  return 0;
}

'''

Rachel Peters
  • 29
  • 1
  • 3

3 Answers3

0
 // put count outside the for loop.
 //because count will be printed with every execution of the for loop.
 // that's why you are getting that output.

for( ) {

}
cout << count << endl;
Jitu DeRaps
  • 144
  • 1
  • 9
  • 1
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: https://stackoverflow.com/help/how-to-answer . Good luck – nima Sep 30 '21 at 06:55
  • thanks, buddy next time I will keep it in mind. – Jitu DeRaps Sep 30 '21 at 10:44
  • 1
    No need to wait so long. You can always just [edit] your answer to raise your chances to help others and gain some upvotes... – Tomerikoo Sep 30 '21 at 13:51
0

You need to print the count after the for loop finishes because it prints with each pass of the for loop:

#include <iostream>

using namespace std;

int main()
{
  int n, x, y, z, count=0;

  cout << "Input value for n: ";
  cin >> n;

  for(int i = 0; i < n; i++)
  {
      cout << "\ninput value for x" << i + 1 << ": ";
      cin >> x;
  
      cout << "intput value for y" << i + 1 << ": ";
      cin >> y;
  
      z = y-x;
  
      if( z >= 2)
        count++;
  }

  cout << "\ncount = " << count;

  return 0;
}
BWallDev
  • 345
  • 3
  • 13
-1

See in for loop, either value will increment or decrement or it will remain the same but it continues to count with respect to the number of passes

However, if you count inside the for loop, it will print each pass of the loop. Therefore, you need to print the count outside of the for loop.

#include <bits/stdc++.h>

using namespace std;

int main()

{

 int N, t, c, z, count=0;

 cin >> N;

  for(int i = 0; i < N; i++)

  {
  
    cin >> t;

    cin >> c;

    z = c-t;

   if( z >= 2)
         { 
           count++;
         }
  }

 cout << count;

return 0;

}
  • ***Never*** recommend the use of `#include` in an answer on Stack Overflow; read [this](https://stackoverflow.com/q/31816095/10871073) and then replace that line with the relevant standard header(s). You could also *significantly* improve the spacing/formatting of your code. – Adrian Mole Sep 01 '22 at 20:03