its not sorting

This is a discussion on its not sorting within the C++ Programming forums, part of the General Programming Boards category; help please i have a program that i made its ascending and descending sorting. i used a template but its ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    its not sorting

    help please i have a program that i made its ascending and descending sorting. i used a template but its not descending can someone help me please. i know what my problem is but i dont know how to fix it. the problem is that this function fun(k,m,'d'); is not passing the parameter 'd' to the template. why is that


    Code:
    #include <iostream> 
    
    using namespace std; 
    
    template <typename mytype>
    
    void fun2(mytype a[], int n)
    {  
    	int i;
    	for(i=0;i<n;++i)
    	  cout<<a[i]<<" ";
    	cout<<endl;
    }
    
    template <typename mytype>
    
    void fun (mytype a[], int b, char c='a')
    {
        mytype temp;
        int i, j;
        
           if(c='a')
           {
              for(i=0;i<b-1;i++) 
              {                 
                 for(j=i+1;j<b;j++) 
                 {
                    if(a[i] > a[j])    //ascending
                   {
                   temp=a[i];
                   a[i]=a[j];
                   a[j]=temp;                  
                  }   
               }
            }
          }
         else if (c='d')
         {
             for(i=0;i<b-1;i++) 
             {                 
                  for(j=i+1;j<b;j++) 
                  { 
                    if(a[i] < a[j])   //descending
                    {
                    temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;                  
                   }   
                 }
             }
         }
       
    }
    
    
    int main () 
    { 
    	char x='t',y='a',z='s';
    	int n,m,tony,i;
    	cout<<"how big do you want your array #1";
    	cin>>n;
    	cout<<"\nhow big do you want your array #2";
    	cin>>m;
    	int *u=new int[n];
    	double *k=new double[m];
    	cout<<"\n";
    	for(i=0;i<n;i++)
    	{
    		cout<<"Enter the data (array 1): ";
    	    cin>>u[i];
    	}
    	for(i=0;i<m;i++)
    	{
    	cout<<"Enter the data (array 2): ";
    	cin>>k[i];
    	}
    	
    	fun(u,n); 
    	fun(k,m,'d'); // d is not overwriting the template. meaning is not          passing the parameter d
    
        fun2(u, n);
        fun2(k, m);
        
    
        cin.sync();
        cin.get();
    
              return 0; 
    }
    Last edited by mouse666666; 10-11-2010 at 01:18 PM.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Good old = vs ==.

    Code:
    	if(c='a')
    	{
         for(i=0;i<b-1;i++) 
         {                 
            for(j=i+1;j<b;j++) 
            {
                if(a[i] > a[j])    ascending
               {
                 temp=a[i];
                 a[i]=a[j];
                 a[j]=temp;                  
               }   
            }
    	  }
    	}
    	else if (c='d')
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    wow it work. but i dont understand why 'd' was not being pass to the function

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You were not comparing c to a character. You were assigning 'a' to it which evaluates to true and the else part will never be taken.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving sorting to fuction
    By Passa in forum C Programming
    Replies: 8
    Last Post: 09-16-2010, 06:46 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 03:14 AM
  3. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 06:50 PM
  4. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 09:41 PM
  5. selection sorting
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 10-13-2001, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21