0

I want to make a function to print ordered pairs between starting and ending number like. Input: Enter starting number 1 ending number 5 Output: (11)(12)(13)(14)(15) (22)(23)(24)(25) (33)(34)(35) (44)(45) (55) but my code gives different output i fix it

#include <bits/stdc++.h>
using namespace std;
void uniquePairs(int n) {
   for (int i = 1; i < n; ++i) {
      for (int j = i + 1; j < n; j++) {
         cout << "(" << i << "," << j << ")" << endl;
      }
   }
}
int main() {
   int n = 5;
   uniquePairs(n);
   return 0;
}
Hafiz
  • 1
  • 1
  • 1
    Can you share the output of your code? – Mukul Kumar Nov 23 '21 at 06:04
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to [edit] your question to improve them, like for example telling us the problems you have with the shown code. – Some programmer dude Nov 23 '21 at 06:05
  • 1
    Also please don't use so-called "competition" or "online judge" sites to learn programming and programming languages. Such sites are *not* teaching or learning resources. The examples of such sites are bad, and often invalid. Using such sites to learn are often more harmful than useful. Case in point: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Some programmer dude Nov 23 '21 at 06:07
  • How about `j = i` instead od `j = i + 1`? Also, `using namespace std;` is an antipattern; please don’t do that. – Andrej Podzimek Nov 23 '21 at 06:10

1 Answers1

1

You can modify your function to take 2 argument instead of 1 as shown below. First argument corresponds to the starting number and second corresponds to the ending number.

#include <iostream>
using namespace std;
//function uniquePairs takes 2 arguments now instead of just 1
void uniquePairs(int startingNumber, int endingNumber) {
    int k = 0;
   for (int i = startingNumber; i <= endingNumber; ++i) {
      for (int j = startingNumber + k; j<= endingNumber; j++) {
         cout << "(" << i << "," << j << ")" << endl;
      }
      ++k;
   }
}
int main() {
   int startingNumber, endingNumber;
   std::cout<<"Enter startingNumber: "<<std::endl;
   std::cin >> startingNumber;
   std::cout<<"Enter endingNumber: "<<std::endl;
   std::cin >> endingNumber;
   //call the function while passing the 2 input numbers
   uniquePairs(startingNumber,endingNumber);
   return 0;
}

The output of the above program for inputs startingNumber =1 and endingNumber = 5 is:

Enter startingNumber: 
1
Enter endingNumber: 
5
(1,1)
(1,2)
(1,3)
(1,4)
(1,5)
(2,2)
(2,3)
(2,4)
(2,5)
(3,3)
(3,4)
(3,5)
(4,4)
(4,5)
(5,5)

The output of the above program can be seen here. Also take a look at Why should I not #include <bits/stdc++.h>?.

Jason
  • 36,170
  • 5
  • 26
  • 60
  • @user4581301 Oh, i already changed it to `#include` in my original answer. But then i made some edit and again pasted the code from external site which had this `#include`. If you see the edit history of my answer you will see that i had `#include`. I have added a link to "why not to use this `bits/stdc++.h`" for OP now. – Jason Nov 23 '21 at 08:57