Write a program in C# Sharp to find the second smallest element in an array ?

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to find the second smallest element in an array?

Output:

Find the second smallest element in an array :                                                                
-----------------------------------------                                                                     
Input the size of array : 5                                                                                   
Input 5 elements in the array (value must be <9999>:                                                          
element - 0 : 2                                                                                               
element - 1 : 4                                                                                               
element - 2 : 6                                                                                               
element - 3 : 8                                                                                               
element - 4 : 10                                                                                              
The Second smallest element in the array is :  4


الأجوبة

using System;  
public class Exercise17 
{  
    public static void Main() 
{
    int n,i,j=0,sml,sml2nd;
  	int[] arr1 = new int[50]; 
       Console.Write("\n\nFind the second smallest element in an array :\n");
       Console.Write("-----------------------------------------\n");   
       Console.Write("Input the size of array : ");
	   n = Convert.ToInt32(Console.ReadLine()); 
    /* Stored values into the array*/
       Console.Write("Input {0} elements in the array (value must be <9999):\n",n);
       for(i=0;i<n;i++)
            {
	      Console.Write("element - {0} : ",i);
		  arr1[i] = Convert.ToInt32(Console.ReadLine()); 
	    }
/* find location of the smallest element in the array */		
   sml=0;
  for(i=0;i<n;i++)
  {
      if(sml>arr1[i])
	  {
           sml=arr1[i];
           j = i;
      }
  }
/* ignore the smallest element and find the 2nd smallest element in the array */		
   sml2nd=99999;
  for(i=0;i<n;i++)
  {
     if(i==j)
        {
          i++;  /* ignoring the smallest element */
		  i--;
        }
      else
        {
          if(sml2nd>arr1[i])
	     {
               sml2nd=arr1[i];
             }
        }
  }
  Console.Write("The Second smallest element in the array is :  {0} \n\n", sml2nd);
  }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...