2013-03-04 45 views
-3

我只是很少理解如何使用if語句和「for循環」。此外,現在我必須這樣做sort without Array.Sort

將數組中的整數元素從最低(元素0)排序到最高(元素4)。不要使用先前存在的Array.Sort方法;編碼你自己的。

這是一個家庭作業問題,我甚至不知道從哪裏開始。有人能通過這個來引導我嗎?

class Program 
{ 
    static void Main(string[] args) 
    { 
     int i; 
     double power = 0, sum = 0; 
     int[] mArray = new int[5]; 


     Console.WriteLine("Please Enter Number Between 10 and 50 \nMake sure all of your Number entered correctly \notherwise you will need to enter everything again "); 


     for (i = 0; i < mArray.Length; i++) 
     { 
      Console.WriteLine("Please enter your Number."); 
      mArray[i] = Convert.ToInt32(Console.ReadLine()); 

      if (mArray[i] >= 50 || mArray[i] <= 10) 
      { 
       i--; 
       Console.WriteLine("Please enter numbers only between 10 and 50."); 

      } 

     } 

     for (i = 0; i < mArray.Length; i++) 
     { 
      sum = sum + (mArray[i]); 
     } 

     double mean = sum/mArray.Length; 

     for (i = 0; i < mArray.Length; i++) 
     { 
      power += Math.Pow((mArray[i] - mean), 2); 
     } 

     double rMean = power/(mArray.Length - 1); 

     Console.WriteLine("Mean {0}", mean); 
     Console.WriteLine("Variance {0}", rMean); 

     Console.WriteLine("Here is sorted numbers"); 




     Console.ReadKey(); 
    } 
} 
+4

Duplicate post - http://stackoverflow.com/questions/15193233/sort-the-integers-without-using-sort-method – 2013-03-04 03:59:28

回答