Hi, i am new to this forum and i am just learning c++. Can somebody help me with sorting numbers. I have:

I have 3 ints(int1,int2,int3), each spanning from 5 to 17, now i want to compare int1 and int2 and type out all numbers from lowest to highest(5,6,7,8...), than i want to compare also int2 and int3 and type out all numbers from highest to lowest(17,16,15,14..)

Recommended Answers

All 10 Replies

The easiest sort to write is the bubble sort:

http://en.wikipedia.org/wiki/Bubble_sort


>> I have 3 ints(int1,int2,int3), each spanning from 5 to 17, now i want to compare int1 and int2 and type out all numbers from lowest to highest(5,6,7,8...), than i want to compare also int2 and int3 and type out all numbers from highest to lowest(17,16,15,14..)


This is a bit confusing to me. You say you have three integers, yet in your list you have four, and perhaps more, given the ...

What precisely are you trying to do?

  1. Sort three numbers? As mentioned, use a bubble sort. It's easy to write.
  2. More confusing to me is the second part. Suppose your three numbers are 7, 10, 14. What should the input and output be? 7,8,9,10? 10,11,12,13,14? Everything from 5 to 17?

So nobody knows ???

So nobody knows ???

"Nobody knows" what you are asking.

>> "Nobody knows" what you are asking.

Precisely. In my response, I gave a couple of possible interpretations of what you were asking and noted that those were just guesses, which means you need to clarify.

What don't you understand ?

A guess at what you want :

int n1 = 10;
int n2 = 5;
int n3 = 12;

int min1 = std::min(n1,n2);
int max1 = std::max(n1,n2);
for(int i = min1; i < max1; ++i) cout << i << " ";

int max2 = std::max(n2,n3);
int min2 = std::min(n2,n3);

cout << endl;

for(int j = max2; j >= min2; --j) cout << j << " ";

>> type out all numbers from lowest to highest(5,6,7,8...)

"All numbers" -- All WHAT numbers? All the numbers from 5 to 17? All the numbers from 5 to the smallest number? All the numbers from the smallest number to the middle number?

>> A guess at what you want

That's as good a guess as any.


To the OP:

That's at least three people who find your problem statement ambiguous.

Re-state the problem completely from the beginning. Leave out the ... part. Take firstPerson's input(5,10,12). What should the output be for that? Was firstPerson's output correct?

A guess at what you want :

int n1 = 10;
int n2 = 5;
int n3 = 12;

int min1 = std::min(n1,n2);
int max1 = std::max(n1,n2);
for(int i = min1; i < max1; ++i) cout << i << " ";

int max2 = std::max(n2,n3);
int min2 = std::min(n2,n3);

cout << endl;

for(int j = max2; j >= min2; --j) cout << j << " ";

Thank you.
This is a good example that got me going, i looked other sites and now i understand how it works. But i tried min and max and can't get it to work with for loop. Do you know how i can i compare loop with min and max ?

here is what i try to figure out:
for(int int1 = 5; int1 < 17; ++int1)
{
int int2 = int1;
int min1 = std::min(int1,int2);
int max1 = std::max(int1,int2);
cout<<max1<<" "; //doesn't finds max, instead all numbers
}

please help

I'm not sure what exactly you are trying to do. Are you trying to find the max numbers from say {5,6,...17} ? Can you explain your problem a little better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.