# Exercise 6 sort numbers inputted into the program

• (5 Pages)
• 1
• 2
• 3
• Last »

## 68 Replies - 3309 Views - Last Post: Yesterday, 12:15 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=414869&amp;s=ebfc900869c1636d2b7060e735e0f98f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 albert003

Reputation: 24
• Posts: 669
• Joined: 15-December 14

# Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 08:11 PM

I was sick with the flu so I took a break from my other program to finish chapter 3 of my C++ book. I realise I still need to use a separate return function to show the results. I'm just doing it this way because I couldn't think straight when I was sick.

This is what I'm suppose to do: (In exercise 6 of chapter 3)
6. 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 figure I'd use different if statements with different possible results the user inputted into the program. The example I'm showing is just the first possible result. I know I can use the C++ 98 function swap to switch around the numbers, but I thought it would be better for me to work on the logic. Now, my question is when my if statement doesn't have the curly braces, it works. But when I have them in the program, it doesn't work. Why?.

This is the program...
```#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
//page 85 exercices

int main()
{
double n1,n2,n3,temp;
std::cout << "Enter 3 numbers and the program" <<std::endl;
std::cout << "will organise them in descending order.\n" <<std::endl;
std::cin >>n1 >>n2 >>n3;
if(n1 < n2 || n2 < n3)
temp = n1;
n1 = n2;
n2 = temp;
temp = n3;
n3 = temp;
n3 = n2;
n2 = temp;
std::cout <<n1<<","<<n2<<","<<n3<<std::endl;

}
```

Is This A Good Question/Topic? 0

## Replies To: Exercise 6 sort numbers inputted into the program

### #2 Skydiver

• Code herder

Reputation: 6768
• Posts: 23,073
• Joined: 05-May 12

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 08:20 PM

Where were you putting the curly braces?

Recall that in C++ indentation means nothing to the compiler. So even though you indented lines 15-22, only line 15 is considered part of the if statement on line 14.

### #3 albert003

Reputation: 24
• Posts: 669
• Joined: 15-December 14

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 08:32 PM

This where I put them...

```#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
//page 85 exercices

int main()
{
double n1,n2,n3,temp;
std::cout << "Enter 3 numbers and the program" <<std::endl;
std::cout << "will organise them in descending order.\n" <<std::endl;
std::cin >>n1 >>n2 >>n3;
if(n1 < n2 || n2 < n3)
{
temp = n1;
n1 = n2;
n2 = temp;
temp = n3;
n3 = temp;
n3 = n2;
n2 = temp;
std::cout <<n1<<","<<n2<<","<<n3<<std::endl;
}

}
```

I spent two days on a flow chart working on it and made a separate program working on the logic line per line. So I'm certain it works,

Interesting that although it only reads line 15 it organizes the numbers correctly. Could you explain to me why?.

This post has been edited by albert003: 11 February 2019 - 08:32 PM

### #4 albert003

Reputation: 24
• Posts: 669
• Joined: 15-December 14

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 08:45 PM

Disregard my post, I figured out what happened and why it didn't work. I had the wrong operator in the if statement.

Line 14 was if(n1 < n2 || n2 < n3)
And its suppose to be
if(n1 > n2 || n2 < n3)

Its suppose to be:

```#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
//page 85 exercices

int main()
{
double n1,n2,n3,temp;
std::cout << "Enter 3 numbers and the program" <<std::endl;
std::cout << "will organise them in descending order.\n" <<std::endl;
std::cin >>n1 >>n2 >>n3;
if(n1 > n2 || n2 < n3)
{
temp = n1;
n1 = n2;
n2 = temp;
temp = n3;
n3 = temp;
n3 = n2;
n2 = temp;
std::cout <<n1<<","<<n2<<","<<n3<<std::endl;
}
}
```

This post has been edited by albert003: 11 February 2019 - 08:46 PM

### #5 Skydiver

• Code herder

Reputation: 6768
• Posts: 23,073
• Joined: 05-May 12

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 08:47 PM

In your code for post #1, lines 16-22 will always execute and print something out giving you the illusion that your own is working. In the code for post #3, lines 16-23 will only execute when the first number is less than the second, or if the second is less than the third.

We have told you before. Use your debugger. Step through the code with a debugger to see what is being executed, and what the values of the variables are.

### #6 Skydiver

• Code herder

Reputation: 6768
• Posts: 23,073
• Joined: 05-May 12

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 08:53 PM

Try your code with the input: 7 5 3

### #7 albert003

Reputation: 24
• Posts: 669
• Joined: 15-December 14

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 09:07 PM

I completely forgot about the debugger. When I was sick I forgot to use it. Ill tattoo it on my forehead so I won't forget it next time.

I was actually going to use four if statements in the program. The first if statement would be used if the user keyed in for example 33 3 11. Which is the if statement I have in the program.

The next if statement would be if the user keyed in numbers that descended (For example 1 2 3).

That was the easiest way for me to make the program without using sort() or any other c++ functions. Theres probably an easier way, but I honestly couldn't figure it out.

This post has been edited by albert003: 11 February 2019 - 09:07 PM

### #8 jimblumberg

Reputation: 5678
• Posts: 17,446
• Joined: 25-December 09

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 09:12 PM

What book are you using?

Jim

### #9 albert003

Reputation: 24
• Posts: 669
• Joined: 15-December 14

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 09:18 PM

Jim

Bjarne Stroustrup

Programming
Principles and Practice Using C++.

### #10 jimblumberg

Reputation: 5678
• Posts: 17,446
• Joined: 25-December 09

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 09:23 PM

Okay then maybe you need re-read the exercise. You're not even using the correct data types.

Also why descending order? The example in the book shows ascending order.

Jim

### #11 ndc85430

• I think you'll find it's "Dr"

Reputation: 972
• Posts: 3,832
• Joined: 13-June 14

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 10:47 PM

albert003, on 12 February 2019 - 04:07 AM, said:

That was the easiest way for me to make the program without using sort() or any other c++ functions. Theres probably an easier way, but I honestly couldn't figure it out.

The problem with this approach is that it doesn't scale. Today, it might just be 4 values, but tomorrow it could be 10 or 100. Instead of thinking about the specific example, how would you solve this for the general case? That's the question that sorting algorithms answer, so you might want to look into them if you want to understand the ways to do it and how to do it efficiently.

### #12 jimblumberg

Reputation: 5678
• Posts: 17,446
• Joined: 25-December 09

## Re: Exercise 6 sort numbers inputted into the program

Posted 11 February 2019 - 10:58 PM

Quote

Today, it might just be 4 values, but tomorrow it could be 10 or 100.

Actually today it is just 3 integer values. But this chapter of the book is basically trying to familiarize the reader with the different types of variables and the different kinds of operators so a very rudimentary approach is probably a good start. And this OP really really needs to understand the basics before moving on, look no farther than the attempted logic of the program for an example.

By the way Albert you may want to start with just two variables before you move on up to three variables. Get your logic to work for two variables and three should be much easier.

Jim

### #13 albert003

Reputation: 24
• Posts: 669
• Joined: 15-December 14

## Re: Exercise 6 sort numbers inputted into the program

Posted 12 February 2019 - 07:33 PM

ndc85430
You made a good point in your post. I am taking my time learning C++ and doing what the book tells me to do (for now).

I foolishly made a huge mistake earlier while learning C++ try to run before I could walk. I have an incomplete program which I still need to finish (Which I will once I get further along with the C++ book). So to answer your question, I'd use a similar program to what I used in the pancake program. A vector to store the information and a bool to organize the integers.

Jim, Yep, I goofed and used double instead of using the int type. Two integers was easy, it was when I had three that I just couldn't wrap my head around it. This is how I solved it for two integers. I don't know why, but making the program sort two integers was easier than making it wort three.

```#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
//page 85 exercices

int main()
{
int n1,n2;
int temp;
std::cout << "Enter 3 numbers and the program" <<std::endl;
std::cout << "will organise them in descending order.\n" <<std::endl;
std::cin >>n1 >>n2;
if(n1 < n2)
{
std::cout <<n1 <<","<<n2<<std::endl;
}
else if(n1 > n2)
{
temp = n1;
n1 = n2;
n2 = temp;
std::cout <<n1 <<","<<n2<<std::endl;
}
else if(n1 == n2)
{
std::cout <<n1<<","<<n2<<std::endl;
}
}
```

This post has been edited by albert003: 12 February 2019 - 07:35 PM

### #14 Skydiver

• Code herder

Reputation: 6768
• Posts: 23,073
• Joined: 05-May 12

## Re: Exercise 6 sort numbers inputted into the program

Posted 12 February 2019 - 08:05 PM

You said you'd been flowcharting this for a little while, can you share your flowchart with us?

### #15 albert003

Reputation: 24
• Posts: 669
• Joined: 15-December 14

## Re: Exercise 6 sort numbers inputted into the program

Posted 12 February 2019 - 08:21 PM

Two minutes after I put my post, it dawned on me how to make it work.

skydiver I just realized I made a huge mistake I made in my initial post. You can't have a return function return more than one thing. I blame my lack of thinking on the fever I had when I was sick with the flu.

This is how I originally had it
```int results(int n1,int n2,int n3);
main
int n1,n2,n3
int total;
total = Results(n1,n2,n3);
std::cout << total <<std::endl;

int results(int n1,int n2,int n3)
int temp;
```

//logic to organize the integers inputted into the program

This is how I solved the integer input program.

I still have to finish it off. My shift is over and I have to put my stuff away. I'll finish it off tonight at home.
```#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
//page 85 exercices

int main()
{
int n1,n2,n3;
int temp;
std::cout << "Enter 3 numbers and the program" <<std::endl;
std::cout << "will organise them in descending order.\n" <<std::endl;
std::cin >>n1 >>n2  >>n3;;
if(n1 < n2 && n2 < n3)
{
std::cout << n1 <<","<<n2 <<","<<n3<<std::endl;
}
else if(n1 > n3 && n2 > n3)//3 2
{
temp = n1;
n1 = n3;
n3 = temp;
std::cout << n1 <<","<<n2 <<","<<n3<<std::endl;
}
else if(n1 == n2 && n2 == n3)
{
std::cout << n1 <<","<<n2 <<","<<n3 <<std::endl;
}

}
```