C# basic

Last post 05-08-2008 2:35 PM by bullpit. 15 replies.

Sort Posts:

  • C# basic

    05-08-2008, 10:44 AM
    • Loading...
    • myspam
    • Joined on 05-02-2008, 12:02 PM
    • Posts 12

     hi everyone

                           can you please help me

    here is my question:

                                 i want to get 10 names as well as 10 ratings from user...i want to sort an array using ratings as well as i want to sort it in an decending order...

    example:

    my output will be

    enter the name x

    enter the name x

    enter the name x

    enter the name x

    enter the name x

    enter the name x

    enter the name x

    enter the name x

    enter the name x

    enter the name x

     

    enter the ratings 5 star

    enter the ratings 2 star

    enter the ratings 5 star

    enter the ratings 3 star

    enter the ratings 6 star

    enter the ratings 5 star

    enter the ratings 5 star

    enter the ratings 5 star

    enter the ratings 4 star

    enter the ratings 5 star

     

    ascending order

    x  2star 

    x  3star 

    x 4 star

    x 5 star

     x 5 star

    x 5 star 

    x  5 star

    x 5star

    x 5star

     

    decending order

    x 5 star

     x 5 star

    x 5 star 

    x  5 star

    x 5star

    x 5star

    x 4 star

    x  3star

    x  2star

     


     Thanks


     




     

     

      

    Filed under:
  • Re: C# basic

    05-08-2008, 10:56 AM
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 3:59 PM
    • Posts 2,708

    We will help you but you should also remember to mark the posts that answered your question as Answer.

    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
  • Re: C# basic

    05-08-2008, 11:01 AM
    • Loading...
    • myspam
    • Joined on 05-02-2008, 12:02 PM
    • Posts 12

     yes sure

    thanks 

  • Re: C# basic

    05-08-2008, 11:11 AM
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 3:59 PM
    • Posts 2,708

    Did you try anything?

    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
  • Re: C# basic

    05-08-2008, 11:35 AM
    • Loading...
    • myspam
    • Joined on 05-02-2008, 12:02 PM
    • Posts 12

     yes

     but i am not sure about this..because now only i am studying c#

    using System;

    class TDMArray
    {
        public static void Main()
        {
            string[,] mymatrix;
            mymatrix = new string[10, 10];
     

            for (int i=0; i<=10; i++)
            {
                for (int j=0; j<=10; j++)
                {
                    Console.Write("Please enter the Names");
                    mymatrix[i,j] = Console.ReadLine();


                }
                //Console.Write("please enter the ratings");(this is wrong)
                //mymatrix[j , i ] = Console.ReadLine();(this is wrong)





            }
            Array.Sort(mymatrix);
            foreach (String person in mymatrix)
            {
                // i dont know how to write
            }
        }
    }
             
           


    Filed under:
  • Re: C# basic

    05-08-2008, 11:56 AM
    • Loading...
    • myspam
    • Joined on 05-02-2008, 12:02 PM
    • Posts 12

    please help me... 

  • Re: C# basic

    05-08-2008, 12:10 PM
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 3:59 PM
    • Posts 2,708

    This uses a heap sort. It might be take some time for you to figure out how heap sort works but its worth it. If you can, read about it and then see how it is implemented here.

         static void Main(string[] args)
            {
                string[,] name = new string[10,2];
                for (int i = 0; i <= 9; i++)
                {
                    Console.Write("Please enter a name ");
                    name[i,0] = Console.ReadLine();
                    Console.Write("Please enter a rank ");
                    name[i,1] = Console.ReadLine();
                }
    
                Console.WriteLine(name.Length.ToString());
                sortArray(name, name.Length/2 - 1);
    
                for (int i = 0; i <= 9; i++)
                {
                    Console.WriteLine(name[i,0] + " " + name[i, 1]);
                }
       
                   Console.Read();
            }
    
            public static void sortArray(string[,] a, int x)
            {
                int i;
                string temp;
                string strTemp;
    
                for (i = (x / 2) - 1; i >= 0; i--)
                {
                    siftDown(a, i, x);
                }
    
                for (i = x - 1; i >= 1; i--)
                {
                    //swapping values at indices 0 & 1
                    temp = a[0, 1];
                    a[0, 1] = a[i, 1];
                    a[i, 1] = temp;
    
                    strTemp = a[0, 0];
                    a[0, 0] = a[i, 0];
                    a[i, 0] = strTemp;
    
                    siftDown(a, 0, i - 1);
                }
            }
    
            /// <summary>
            /// This method is used my sortArray() 
            /// </summary>
            /// <param name="a"></param>
            /// <param name="root"></param>
            /// <param name="bottom"></param>
            public static void siftDown(string[,] a, int root, int bottom)
            {
                bool done = false;
                int maxChild;
                string temp;
                string strTemp;
    
                while ((root * 2 <= bottom) && (!done))
                {
                    if (root * 2 == bottom)
                        maxChild = root * 2;
                    else if (Convert.ToDouble(a[root * 2, 1]) < Convert.ToDouble(a[root * 2 + 1, 1]))
                        maxChild = root * 2;
                    else
                        maxChild = root * 2 + 1;
    
    
                    if (Convert.ToDouble(a[root, 1]) > Convert.ToDouble(a[maxChild, 1]))
                    {
                        //swapping values at indices 0 & 1
                        temp = a[root, 1];
                        a[root, 1] = a[maxChild, 1];
                        a[maxChild, 1] = temp;
    
                        strTemp = a[root, 0];
                        a[root, 0] = a[maxChild, 0];
                        a[maxChild, 0] = strTemp;
    
                        root = maxChild;
                    }
                    else
                    {
                        done = true;
                    }
                }
            }
    
       
     
    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
  • Re: C# basic

    05-08-2008, 12:20 PM
    • Loading...
    • myspam
    • Joined on 05-02-2008, 12:02 PM
    • Posts 12

     thank you so much

    is there any other  simple way to solve this problem 

  • Re: C# basic

    05-08-2008, 12:48 PM
    • Loading...
    • myspam
    • Joined on 05-02-2008, 12:02 PM
    • Posts 12

     thanks alot

    i more doubt..can you please tell me ..how to sort an twodimensional array in very simple manner

    for example

    using System;

    class TDMArray
    {
        public static void Main()
        {
            string[,] mymatrix;
            mymatrix = new string[10, 10];


            for (int i = 0; i <= 9; i++)
            {


                Console.Write("Please enter the Names");
                mymatrix[i, 0] = Console.ReadLine();
                Console.Write("please enter the ratings");
                mymatrix[i, 1] = Console.ReadLine();
            }
        }
    }

    after this i just want to sort an array using ratings,but both names and ratingscan be displayed in outputs..

    please help me





          
             
           

     

     

  • Re: C# basic

    05-08-2008, 1:12 PM

    I think the problem here is that you're not really trying to sort a matrix, you're trying to sort an array of tupels. I'd suggest making a class "Item" to hold the Name => Rating mapping.  Then your problem becomes sorting a one dimensional array of Items.  The framework defines a standard interface for things which can be compared : IComparable<T> which your Item class should probably implement. 

  • Re: C# basic

    05-08-2008, 1:19 PM
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 3:59 PM
    • Posts 2,708

    Heres a simpler Bubble Sort algorithm. The good thing is that its simple, the bad is that when you use this with a large collection, the performance is incredibly slow. 

    // You call the sort() function like this
    sort(name, name.Length/2);
    
    // Bubble Sort Algorithm
            public static void sort(string[,] a, int x)
            {
                int i;
                int j;
                string temp;            
                
                for (i = (x - 1); i >= 0; i--)
                {
                    for (j = 1; j <= i; j++)
                    {                    
                        if (Convert.ToInt32(a[(j - 1), 1]) < Convert.ToInt32(a[j, 1]))
                        {
                            temp = a[j - 1 ,0];
                            a[j - 1, 0] = a[j ,0];
                            a[j ,0] = temp;
    
                            temp = a[j - 1 ,1];
                            a[j - 1, 1] = a[j, 1];
                            a[j, 1] = temp;
                        }
                    }
                }
            }
     
    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
  • Re: C# basic

    05-08-2008, 1:26 PM
    • Loading...
    • myspam
    • Joined on 05-02-2008, 12:02 PM
    • Posts 12

     thanks everyone

  • Re: C# basic

    05-08-2008, 1:48 PM
    • Loading...
    • myspam
    • Joined on 05-02-2008, 12:02 PM
    • Posts 12

    i got an error msg 

  • Re: C# basic

    05-08-2008, 1:49 PM
    • Loading...
    • bullpit
    • Joined on 06-29-2006, 3:59 PM
    • Posts 2,708

    You have to give more than tha to figure out what the problem is.

    [bullpit^-^]
    If you can read this, you are too close to the screen!!!
  • Re: C# basic

    05-08-2008, 1:57 PM
    • Loading...
    • myspam
    • Joined on 05-02-2008, 12:02 PM
    • Posts 12

    here is my code

    using System;

    class TDMArray
    {
        public static void Main()
        {
            string[,] name;
            name = new string[10, 10];


            for (int i = 0; i <= 9; i++)
            {


                Console.Write("Please enter the Names");
              name[i, 0] = Console.ReadLine();
                Console.Write("please enter the ratings");
                name[i, 1] = Console.ReadLine();
            }

           
            sort(name, name.Length / 2);

        }
        public static void sort(string[,] a, int x)
        {
            int i;
            int j;
            string temp;

            for (i = (x - 1); i >= 0; i--)
            {
                for (j = 1; j <= i; j++)
                {
                    if (Convert.ToInt32(a[(j - 1), 1]) < Convert.ToInt32(a[j, 1]))
                    {
                        temp = a[j - 1, 0];
                        a[j - 1, 0] = a[j, 0];
                        a[j, 0] = temp;

                        temp = a[j - 1, 1];
                        a[j - 1, 1] = a[j, 1];
                        a[j, 1] = temp;
                    }
                }
            }
        }
    }

    and my problem is

    Formal Exception was unhandled

    input string was not in a correct format..

     
     








          
             
           

     

Page 1 of 2 (16 items) 1 2 Next >