-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion_3.cs
74 lines (58 loc) · 1.92 KB
/
question_3.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
class Program
{
public void question_3(int[] int_main)
{
int int_displaced = 0;
int int_place = 0;
for (int x = 0; x < int_main.Length; x++)
{
int_place = x;
for (int y = x; y < int_main.Length; y++)
{
if (int_main[int_place] > int_main[y])
{
int_place = y;
}
}
if (int_place > x)
{
int_displaced = int_displaced + 1;
int_main[x] = int_main[x] + int_main[int_place];
int_main[int_place] = int_main[x] - int_main[int_place];
int_main[x] = int_main[x] - int_main[int_place];
}
}
Console.Write("The result array = [");
for (int x = 0; x < int_main.Length - 1 ; x++)
{
Console.Write(int_main[x]);
Console.Write(",");
}
Console.Write("{0}]", int_main[int_main.Length - 1]);
Console.WriteLine();
Console.WriteLine("The number of displaced = {0}", int_displaced);
}
static void Main()
{
string str_main = "";
int[] int_main = new int[1];
int int_loop = 0;
Console.Write("Please input the array = ");
str_main = Console.ReadLine();
for (int y = 1; y < str_main.Length - 1; y++)
{
if (str_main[y] != ',')
{
int_main[int_loop] = int_main[int_loop] * 10 + Convert.ToInt32(str_main[y]) - 48;
}
else
{
int_loop = int_loop + 1;
Array.Resize(ref int_main, int_main.Length + 1);
}
}
Program program = new Program();
program.question_3(int_main);
}
}