Hi guys (and gals)

I'm pretty new to c++ and programming, but I am keen as hell

I bought a boot to start learning by Bjarne Stroustrup - Programming - Principles and Practice using C++

I am up to Chapter 3 and doing the 'exercises', only I am stuck on one of them and I do not want to move on until I have solved it. It seems like it should be really easy, only I can not get my head around it.

If somebody could give me a push in the right direction on how to solve it, I would be very thankful.

This is it:

Write a program that prompts the user to enter three integer values and then outputs the values in numerical sequence separated by commas. So, if the user enters the values 10 4 6, the output should be 4, 6, 10. If two values are the same, they should just be ordered together. So, the input 4 5 4 should give 4, 4, 5.


I am flying through the book pretty quickly, but I can not get my head around this one (I know it will be simple as hell to you guys)

Thank you in advance.

can you use vectors?
that would be my first port of call.
then either use sort or write (what i think is called) a bubblesort.

can you use vectors?
that would be my first port of call.
then either use sort or write (what i think is called) a bubblesort.

Hi frogboy

No, I presume not as vectors come much later in the book.

I presume I would have to do it using if statements. Just cannot get my head around it.

Pete

Hi frogboy

No, I presume not as vectors come much later in the book.

I presume I would have to do it using if statements. Just cannot get my head around it.

Pete

A simple array will do you fine. Or with only three numbers, don't even bother. Anything more than three variables and you'll want to do some sorting algorithm with swaps, loops, etc., but with three numbers, you only have six possible orderings. A nice easy brute-force, though not particularly elegant solution is:

if(num1 <= num2 && num2 <= num3)
{
    cout << num1 << ", " << num2 << ", " << num3 << "\n";
}
else if(num1 <= num2 && num3 <= num2)
{
    cout << num1 << ", " << num3 << ", " << num2 << "\n";
}

There's two of the six possibilities. Add four more "else if" statements and you have it.

A simple array will do you fine. Or with only three numbers, don't even bother. Anything more than three variables and you'll want to do some sorting algorithm with swaps, loops, etc., but with three numbers, you only have six possible orderings. A nice easy brute-force, though not particularly elegant solution is:

if(num1 <= num2 && num2 <= num3)
{
    cout << num1 << ", " << num2 << ", " << num3 << "\n";
}
else if(num1 <= num2 && num3 <= num2)
{
    cout << num1 << ", " << num3 << ", " << num2 << "\n";
}

There's two of the six possibilities. Add four more "else if" statements and you have it.

Vernon, you are a superstar :)

It was the else if statements, i was missing..

I created 4 more of them and it worked a charm.

Thank you, you have been a great help.

Great forum

>> Vernon, you are a superstar :)

>> I created 4 more of them and it worked a charm.


Thanks, for the compliment, glad it worked, but it shouldn't have!

if(num1 <= num2 && num2 <= num3)
{
    cout << num1 << ", " << num2 << ", " << num3 << "\n";
}
else if(num1 <= num2 && num3 <= num2)
{
    cout << num1 << ", " << num3 << ", " << num2 << "\n";
}

YIKES!

num1 = 2, num2 = 3, num3 = 1 prints "2, 1, 3"

if(num1 <= num2 && num2 <= num3)
{
    cout << num1 << ", " << num2 << ", " << num3 << "\n";
}
else if(num1 <= num3 && num3 <= num2)
{
    cout << num1 << ", " << num3 << ", " << num2 << "\n";
}

Ah... Much better. :$

Hopefully that was just a typo. Yeah sure! This was marked "solved" and would have probably gone away unnoticed... maybe I'll just navigate away and... Oh what the hell! Submit!

Thank you for that :)

Missed it myself

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.