Thread: Sorting an array

  1. #1
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555

    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;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    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?
    Last edited by OnionKnight; 04-23-2005 at 04:05 PM.

  4. #4
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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]<<"  ";
           }
    }

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    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;
    }
    Last edited by OnionKnight; 04-24-2005 at 03:42 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 03:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM