Arrays in ascending order

closed account (9G3v5Di1)
Write a program that accepts five (5) integers from the user, sort in ascending order and display the sorted array in one line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <limits>
#include <cstring>
#include <float.h>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <istream>
#include "_pause.h"

using namespace std;

int main() {
	int n[5];

	for(int i=0; i <5; i++){
		cout << "Enter a number[" << i+1 << "]: ";
		cin >> n[i]; 
	}
	cout << endl;
        
        // from here
	for(int i = 0; i<5; i++){
		for(int t= i; t<5; t++){
			if(n[i] > n[t]){
				int temp = n[i];
				n[i] = n[t];
				n[t] = temp;
			}
		}
	}
        // to here

	cout << "The sorted numbers are: " << n[0] << ", " << n[1] << ", "
	<< n[2] << ", " << n[3] << ", " << n[4] << ".";

	cout << endl;

	std::cin.get();
	_pause();
	return 0;
}


Someone explain to me line by line because I coudln't understand the explanation given in the module I am reading *see comments inside codes*
You saw an explanation, but did not understand it. Now you ask for explanation.
What if we happen to write the same text as is in that module?

Lets try the other way: You explain first everything that you can.


A trivia bit though:
Lines 25-27: http://www.cplusplus.com/reference/utility/swap/
It sorts by comparing every pair of positions in the array (i ranging from start to finish, t ranging from i(+1) to end - see comment below) making sure that that pair of elements are in the correct order. It's not a massively efficient sorting method.

Line 24 - 28 compare the ith and tth elements and swap them if they are in the wrong order. (Lines 25-27 are a standard way of swapping values.)

You will have one pointless check (an element with itself) unless you change line 23 to
for(int t= i+1; t<5; t++){
Similarly, to get any later element to swap with, you can reduce the scan on line 22 to
for(int i = 0; i<4; i++){

A couple of other points:
- try to avoid "magic numbers"; 5 is so hard-coded here that it's very complex to change the number of elements; do it for a general n, and set n=5 at the start: this will be the only place that you have to change anything;
- you don't need most of those includes at the start;also, try to avoid non-standard stuff as well (like _pause()); try fixing it so that it runs in cpp-shell.
Last edited on
closed account (9G3v5Di1)
I will study about the swapping.. I get your point with i ranging from start and t = i +1. But what do you mean by comparing pair of positions in the array.. for example the user input was
 {5, 1, 8, 613, 23}
I'm very sorry I couldn't get it by one explanation... btw I go to bed now I'll be back maybe at 10am gmt+8 or in 12hrs from now.. really thankful I found this site
Last edited on
goldwinbryanr wrote:
what do you mean by comparing pair of positions in the array


The line
if(n[i] > n[t]){
compares the ith element of the array (n[i]) with the tth element (n[t]). (Note that C++ and related languages count from 0, not 1, for good, but not easily explained, reasons).

If this statement is true ... then these particular elements are in the wrong order ... and the next block of lines (surrounded by curly braces and indicated by the indentation) will carry out the code to swap them.

Always looks brighter in the morning. (Apart from my bank balance.)


For your particular example (5,1,8,613,23) ...
outer loop i=0 (i.e. compare 0th element (the start!) with later ones):
t=1: 5 and 1 are wrong way round, so swap to 1,5,8,613,23
t=2: 1 and 8 are ok
t=3: 1 and 613 are ok
t=4: 1 and 23 are ok
At the end of the i=0 loop, the 0th element is correct (1) and will be ignored henceforth. Currently 1,5,8,613,23

Next outer loop i=1
t=2: 5 and 8 are ok
t=3: 5 and 613 are ok
t=4: 5 and 23 are ok
Currently 1,5 are finalised and current status is 1,5,8,613,23

Next outer loop i=2
t=3 then 4: the element 8 is correctly positioned with respect to both of these, so no changes
Currently 1,5,8 are finalised and current status is 1,5,8,613,23

Final outer loop i=3
t=4: 613 and 23 are the wrong way round, so swap
Finishes with 1,5,8,23,613
Last edited on
Lets do it with a 4-element array example too: 4,3,2,1

i=0
t=1 3,4,2,1
t=2 2,4,3,1
t=3 1,4,3,2

i=1
t=2 1,3,4,2
t=3 1,2,4,3

i=2
t=3 1,2,3,4

Every single case did require a swap. A "worst case".
closed account (9G3v5Di1)
these guys are hilarious.. I'm getting there
closed account (9G3v5Di1)
Okay so how does the t increments without incrementing the i?

Does it goes like this,

Assume this is a 4-element array with {4,3,2,1}

i=0
t= 1 3,4,2,1
then the 3 goes back to the condition of the if statement with the t being incremented?

how does t being processed without passing by i=1? What I know about loops is that you repeat allover so the i will be affected too.. I hope you guys get my point/question
Last edited on
I hope you guys get my point/question

Not really. What you do to t has no effect on i.

Look at both examples. They carefully state what values i and t take as you go through outer and inner loops. They make a point: they aren't hilarious.
Last edited on
Lets look at the outer loop. I did rename the i to x and moved the statements in the loop's body into a function:
1
2
3
for ( int x = 0; x < 5; x++ ) {
  something( n, x );
}

The loop calls something() for 5 times and gives each time different x.

What was inside the loop?
1
2
3
4
5
6
7
8
void something( int* n, int i )
{
  for ( int t=i; t < 5; t++ ) {
    if ( n[i] > n[t] ) {
      std::swap( n[i], n[t] );
    }
  }
}

Every time this function is called, the i will have some value.
In the loop the t increments from i to 4.
The i does not change in this function.


In my previous example post I did skip the (i=4) iteration of the outer loop and the t=i iterations of the inner loop, because they ... that was already explained by lastchance.
closed account (9G3v5Di1)
IIIIIIIIIIIIII absolutely got ittttt thank you so much for both of you, lastchance and keskiverto
Topic archived. No new replies allowed.