A sample program for giving ascending and descending order of numbers using out parameter and methods returning arrays.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Sorting s = new Sorting();
int[] num = new int[10]{-2,10,8,5,2,3,1,9,-1,0};
int[] desc = new int[10];
int[] final = s.Sort(num, out
desc);
Console.WriteLine("The
numbers are : ");
for (int i = 0; i
< num.Length; i++)
{
Console.Write(num[i] + " ");
}
Console.WriteLine("\nThe
ascending order is : ");
for (int
i=0;i
{
Console.Write(final[i] + " ");
}
Console.ReadLine();
Console.WriteLine("The
descending order is : ");
for (int i = 0; i
< desc.Length; i++)
{
Console.Write(desc[i] + " ");
}
Console.ReadLine();
}
}
class Sorting
{
public int[] Sort(int[] numbers, out int[] descending)
{
int[] localcopy = numbers;
int[] ascending = numbers;
descending = new int[10];
int temp = ascending[0];
int first = 0;
int second = 0;
for (int j = 0; j
< localcopy.Length; j++)
{
for (int
i = 0; i < localcopy.Length - 1; i++)
{
first = ascending[i];
second = ascending[i + 1];
if (first > second)
{
ascending[i] = second;
ascending[i + 1] =
first;
}
}
}
for (int i =
localcopy.Length -1,j=0; i >=0; i--,j++)
{
descending[i] = ascending[j];
}
return ascending;
}
}
}
Output :
No comments:
Post a Comment