-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm
More file actions
185 lines (171 loc) · 5.02 KB
/
Algorithm
File metadata and controls
185 lines (171 loc) · 5.02 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
namespace Basic13
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Lets make some functions!");
// PrintNumbers();
// PrintOdds();
// PrintSum();
// LoopArray(new int[] {1,2,3,4,5});
// FindMax(new int[] {-3, 117, -7, 50, 26});
// GetAverage(new int[] {40, 15, -8, 0, 117});
// int[] result = OddArray();
// Console.WriteLine(result[127]);
// Console.WriteLine(GreaterThanY(new int[] {1,5,20,-2,117}, 5));
// int[] nums = SquareArrayValues(new int[] {4,2,10,117});
// EliminateNegatives(new int[] {-5,2,-2,117});
// MinMaxAverage(new int[] {5,117,-24,16});
// ShiftValues(new int[] {1,117,26,15,6});
// NumToString(new int[] {2,117,-15,6,-20});
}
public static void PrintNumbers()
{
for (int i = 1; i <= 255; i++)
{
Console.WriteLine(i);
}
}
public static void PrintOdds()
{
for (int i = 1; i <= 255; i++)
{
if (i % 2 != 0)
{
Console.WriteLine(i);
}
}
}
public static void PrintSum()
{
int sum = 0;
for (int i = 0; i <= 255; i++)
{
sum += i;
Console.WriteLine($"New number: {i} Sum: {sum}");
}
}
public static void LoopArray(int[] numbers)
{
foreach (int num in numbers)
{
Console.WriteLine(num);
}
}
public static void FindMax(int[] numbers)
{
int max = numbers[0];
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] > max)
{
max = numbers[i];
}
}
Console.WriteLine($"Max value = {max}");
}
public static void GetAverage(int[] numbers)
{
int total = 0;
for (int i = 0; i < numbers.Length; i++)
{
total += numbers[i];
}
int avg = total / numbers.Length;
Console.WriteLine($"Your average is: {avg}");
}
public static int[] OddArray()
{
int[] NewArray = new int[128];
int count = 0;
for (int i = 1; i <= 255; i+=2)
{
NewArray[count] = i;
count++;
}
return NewArray;
}
public static int GreaterThanY(int[] numbers, int y)
{
int total = 0;
foreach (int num in numbers)
{
if (num > y)
{
total++;
}
}
return total;
}
public static int[] SquareArrayValues(int[] numbers)
{
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] *= numbers[i];
Console.WriteLine(numbers[i]);
}
return numbers;
}
public static int[] EliminateNegatives(int[] numbers)
{
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] < 0)
{
numbers[i] = 0;
}
Console.WriteLine(numbers[i]);
}
return numbers;
}
public static void MinMaxAverage(int[] numbers)
{
int total = 0;
int max = numbers[0];
int min = numbers[0];
foreach (int num in numbers)
{
total += num;
if (num > max)
{
max = num;
}
else if (num < min)
{
min = num;
}
}
int avg = total / numbers.Length;
Console.WriteLine($"Max value: {max}");
Console.WriteLine($"Min value: {min}");
Console.WriteLine($"Average: {avg}");
}
public static void ShiftValues(int[] numbers)
{
for (int i = 0; i < numbers.Length-1; i++)
{
numbers[i] = numbers[i+1];
Console.WriteLine(numbers[i]);
}
numbers[numbers.Length-1] = 0;
Console.WriteLine(numbers[numbers.Length-1]);
}
public static object[] NumToString(int[] numbers)
{
object[] newArray = new object[numbers.Length];
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] < 0)
{
newArray[i] = "Dojo";
}
else
newArray[i] = numbers[i];
Console.WriteLine(newArray[i]);
}
return newArray;
}
}
}