vendredi 23 août 2019

Having a confusion in these two source codes to arrange integers in numerical sequence

In the Book Programming Principles and Practices, Bjarne Stroustrup gives a question in exercises:

Q 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 user enters values 10,4,6, The output should be 4,6,10. If the two values are same they should just be ordered together. So, the input 4,5,4 should give 4,4,5.

I tried the same and wrote a very simple source code for the problem.

https://imgur.com/a/rBKY8fV

However, Stroustrup suggests the following approach

http://www.stroustrup.com/Programming/Solutions/Ch3/e3-6.cpp

Both the source codes deliver the same output, that is list the number according to Numeric order and if values are same they are listed after each other.

Is my approach right? Does it have any type-errors as such? Is it better to do it the Stroustrup way and if so why? Or are both the codes same as such?

Sorry I am a beginner level C++ programmer.

Aucun commentaire:

Enregistrer un commentaire