Дан фрагмент кода:
Код:
public void Sort()
{
double[] b = new double[n];
int aIndex = 0;
for (int i = 0; i < n; i++)
if (Math.Abs(a[i])<=1.0)
{
b[aIndex] = a[i];
aIndex++;
}
for (int i = 0; i < n; i++)
if (a[i]>1.0)
{
b[aIndex] = a[i];
aIndex++;
}
foreach (double x in b)
Console.Write("\t" + x);
Console.Write("\n");
}
Превратить массив таким образом , чтобы сначала располагались все элементы , целая часть которых не превышает единицу , а затем - все остальные . Это задание я сделал. НО , массив то не сортируеться. К примеру вот результат сортировки : -1 0 0,1 -0,3 3 2 . Помогите корректно по возростанию отсортировать массив. Вот еще весь код:
Код:
using System;
using System.Collections.Generic;
using System.Text;
namespace ArrayWork
{
public class Array
{
int n;
public int size1;
int size2;
public double[] a;
public double[] b;
bool ErrFlag;
int aIndex;
public void Field()
{
Console.WriteLine("Введiть межi масиву: ");
Console.Write("Перша межа:");
size1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Друга межа:");
size2 = Convert.ToInt32(Console.ReadLine());
n = size2 - size1 + 1;
}
public void ScanArray()
{
a = new double[n];
int x = size1;
Console.WriteLine("Введiть массив: ");
for (int i = 0; i < n; i++)
{
Console.Write("a[" + x + "]=");
a[i] = Convert.ToDouble(Console.ReadLine());
x++;
}
foreach (double z in a)
Console.Write("\t" + z);
Console.Write("\n");
}
public double this[int index]
{
get
{
if (ok(index))
{
ErrFlag = false;
return a[index - size1];
}
else
{
ErrFlag = true;
return 0;
}
}
set
{
if (ok(index))
{
a[index] = value;
ErrFlag = false;
}
else ErrFlag = true;
}
}
private bool ok(int index)
{
if (index >= size1 & index <= size2) return true;
else
return false;
}
public void Dodatnih()
{
int s = 0;
for(int i=0;i<n;i++)
{
if(a[i]>0)
{
s++;
}
}
Console.WriteLine("Kilkist:{0}",s);
}
public void Summ()
{
int j = 0;
double s = 0;
for (int i = 0; i < n; i++)
{
if (a[i] == 0)
{
j = i;
}
}
for (int i = j; i < n; i++)
s += a[i];
Console.WriteLine("Summa={0}\t",s);
}
public void Sort()
{
double[] b = new double[n];
int aIndex = 0;
for (int i = 0; i < n; i++)
if (Math.Abs(a[i])<=1.0)
{
b[aIndex] = a[i];
aIndex++;
}
for (int i = 0; i < n; i++)
if (a[i]>1.0)
{
b[aIndex] = a[i];
aIndex++;
}
foreach (double x in b)
Console.Write("\t" + x);
Console.Write("\n");
}
public void ElementOutput()
{
double x;
Console.Write("Введiть номер елемента для виведення: ");
aIndex = Convert.ToInt32(Console.ReadLine());
x = this[aIndex];
if (this.ErrFlag)
Console.WriteLine("Ви вказали не правильний номер елемента");
else
Console.WriteLine("a[" + aIndex + "]=" + x);
}
}
public class Program
{
static void Main(string[] args)
{
Array a = new Array();;
a.Field();
a.ScanArray();
a.ElementOutput ();
a.Dodatnih();
a.Summ();
a.Sort();
Console.ReadLine();
}
}
}