# Sorting an array

• 04-23-2005
OnionKnight
Sorting an array
I have a problem with one of the tasks I'm supposed to do which is writing a program that asks for a bunch of integers and put them in an array. (in this case, 10)
Also after each read the array should be sorted so that the lowest numbers comes first and largest last.

The problem I'm having is that when I input a large number like 654 or 2353 it stops accepting low numbers such as 2, 3, 21 and all negative numbers; they turn into some huge number or a zero and the array stops sorting itself after that.

Also, I'm only using iostream.h instead of iostream since the book uses it so don't bother pointing that out.
Code:

```#include <iostream.h> int main() {         int tal[10], i, intal;         for (i = 0; i <= 9; i++) {                 cout << "Skriv ett tal: ";                 cin >> intal;                 if (i == 0) tal[0] = intal;                 else if (i == 1) {                         if (intal < tal[0]) {                                 tal[1] = tal[0];                                 tal[0] = intal;                         }                         else tal[1] = intal;                 }                 else if (intal > tal[i - 1] && tal[i - 2]) tal[i] = intal;                 else for (int i2 = 0; i2 <= i; i2++) {                         if (intal > tal[i2] && intal <= tal[i2 + 1]) {                                 for (int i3 = i; i3 > i2; i3--) {                                         tal[i3 + 1] = tal[i3];                                 }                                 tal[i2 + 1] = intal;                         }                 }                 for (int ix = 0; ix <= i; ix++)        // debugging stuff                         cout << tal[ix] << " ";        //                 cout << endl;                        //         }         for (i = 0; i <= 9; i++)                 cout << tal[i] << " ";         return 0; }```
• 04-23-2005
Salem
How about splitting the tasks up a bit into more manageable blocks
Code:

```int main() {     int tal[10];     input( tal, 10 );     sort( tal, 10 );     output( tal, 10 ); }```
I suggest you get input and output working properly before tackling sort.
• 04-23-2005
OnionKnight
I wanted to do this, the code is really ugly, but it's a pretty early chapter so functions aren't covered yet.

edit:
Code:

`else if (intal > tal[i - 1] && tal[i - 2]) tal[i] = intal;`
Should have a "intal > " before the tal[i - 2] but I also noticed that the whole thing was redundant so I just changed it to
Code:

`else if (intal > tal[i - 1]) tal[i] = intal;`
This made the list start working again but small numbers still get weird. The program gets an integer with cin, applies it to an array of the same type and uses it to compare with other integers. The program doesn't do any funny stuff so how come it gets so screwed up?
• 04-24-2005
treenef
Code:

``` #include <stdio.h> #include <iostream.h> int TreeNef(int T_ree_Ne_f[],int); int Tree_Nef(int T_ree_Ne_f[],int,int); int Tree_Ne_f(int [],int); int main() {   int Tt_ree_Ne_f[81];   for (int a=0; a<10; a++)   {     cout<<""<<endl;     cout<<""<<endl;     cout<<"Shriv et tal:";     cin>>Tt_ree_Ne_f[a];     // call the function     TreeNef(Tt_ree_Ne_f,a+1);     // call the function     Tree_Ne_f(Tt_ree_Ne_f,a+1);     }       int tree;   cin>>tree;   return 0; } /*===================================   Function definition TreeNef   ===================================*/ int TreeNef(int T_ree_Ne_f[], int Tt_ree_Ne_f_size) {   Tree_Nef(T_ree_Ne_f, 0, Tt_ree_Ne_f_size - 1); } int Tree_Nef(int T_ree_Ne_f[], int left, int treen) {   int Tt_ree_NeE_f, Tt_ree_NeEf, r_hold;   Tt_ree_NeEf = left;   r_hold = treen;   Tt_ree_NeE_f = T_ree_Ne_f[left];   while (left < treen)   {     while ((T_ree_Ne_f[treen] >= Tt_ree_NeE_f) && (left < treen))       treen--;     if (left != treen)     {       T_ree_Ne_f[left] = T_ree_Ne_f[treen];       left++;     }     while ((T_ree_Ne_f[left] <= Tt_ree_NeE_f) && (left < treen))       left++;     if (left != treen)     {       T_ree_Ne_f[treen] = T_ree_Ne_f[left];       treen--;     }   }   T_ree_Ne_f[left] = Tt_ree_NeE_f;   Tt_ree_NeE_f = left;   left = Tt_ree_NeEf;   treen = r_hold;   if (left < Tt_ree_NeE_f)     Tree_Nef(T_ree_Ne_f, left, Tt_ree_NeE_f-1);   if (treen > Tt_ree_NeE_f)     Tree_Nef(T_ree_Ne_f, Tt_ree_NeE_f+1, treen); } /*=============================   Function defintion Tree_Ne_f   =============================*/ int Tree_Ne_f(int T_ree_Ne_f[],int size) {     for (int b=0; b<size; b++)       {       cout<<T_ree_Ne_f[b]<<"  ";       } }```
:confused:
• 04-24-2005
OnionKnight
I see what you did there. Might work if I inputted directly to the array and then just moved to the left until it hit a number lower than itself.

edit: It works now! And the code got a little shorter:
Code:

```#include <iostream.h> #define ANTAL 9 int main() {         int tal[ANTAL + 1], i;         for (i = 0; i <= ANTAL; i++) {                 cout << "Skriv ett tal: ";                 cin >> tal[i];                 for (int z = i; z; z--)                         if (tal[z - 1] > tal[z]) {                                 int temp = tal[z - 1];                                 tal [z - 1] = tal[z];                                 tal[z] = temp;                         }                         else                                 break;         }         for (i = 0; i <= ANTAL; i++)                 cout << tal[i] << " ";         return 0; }```