-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
187 lines (171 loc) · 9.39 KB
/
Copy pathProgram.cs
File metadata and controls
187 lines (171 loc) · 9.39 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
186
187
using System;
// This is the main namespace for our application. Think of it as a container
// that helps organize our code and prevent naming conflicts with other code.
namespace ArrayManipulation
{
// This is the main class where our program execution begins. In C#,
// all executable code resides within classes.
class Program
{
// This is the entry point of our console application. The 'Main' method
// is where the program starts running. 'static' means it belongs to the
// Program class itself, not a specific instance of it, and 'void' means
// it doesn't return any value. 'string[] args' allows us to pass command-line
// arguments to the application, though we won't be using them in this exercise.
static void Main(string[] args)
{
Console.WriteLine("-------------------Array Manipulation App!-------------");
Console.WriteLine("----------------Yasirm Maken QTS Doha Intern!----------\n");
// 1. Array Initialization and Population:
// Declare an integer array named 'numbers' with a fixed size of 10 elements.
int[] numbers = new int[10];
// This for loop will iterate 10 times, once for each element in the array.
for (int i = 0; i < numbers.Length; i++)
{
// We use a do-while loop to ensure the user enters a valid integer.
int number;
bool isValidInput;
do
{
// Prompt the user to enter an integer value for the current array element.
Console.Write($"Enter integer value for index {i}: ");
// Try to parse the user's input (which is initially a string from Console.ReadLine())
// into an integer. 'out number' means if the parsing is successful, the parsed
// integer will be stored in the 'number' variable. 'int.TryParse()' returns
// true if the parsing is successful, and false otherwise.
isValidInput = int.TryParse(Console.ReadLine(), out number);
// If the input was not a valid integer, display an error message to the user.
if (!isValidInput)
{
Console.WriteLine("Invalid input. Please enter a whole number.");
}
// The loop continues as long as 'isValidInput' is false, meaning the user
// needs to re-enter the value.
} while (!isValidInput);
// Once a valid integer is entered, store it in the current element of the 'numbers' array.
numbers[i] = number;
}
// Output a blank line to improve readability in the console.
Console.WriteLine();
// 2. Basic Array Statistics:
// Call the CalculateAverage method, passing the 'numbers' array as an argument,
// and store the returned average in the 'average' variable (which is a double).
double average = CalculateAverage(numbers);
// Call the FindMax method to find the maximum value in the 'numbers' array
// and store it in the 'max' variable (which is an integer).
int max = FindMax(numbers);
// Call the FindMin method to find the minimum value in the 'numbers' array
// and store it in the 'min' variable (which is an integer).
int min = FindMin(numbers);
// Print the calculated average, maximum, and minimum values to the console.
// We use string interpolation (the $ before the string) to easily embed variable
// values within the string. ":F2" formats the average to two decimal places.
Console.WriteLine($"Average: {average:F2}");
Console.WriteLine($"Maximum: {max}");
Console.WriteLine($"Minimum: {min}");
// Output another blank line for better formatting.
Console.WriteLine();
// 3. Array Sorting (Bubble Sort):
// Create a copy of the original array to avoid modifying it before printing.
int[] numbersToSort = (int[])numbers.Clone();
// Call the SortArrayAscending method to sort the 'numbersToSort' array in ascending order.
SortArrayAscending(numbersToSort);
// Print a message indicating that the array has been sorted.
Console.WriteLine("Sorted Array (Ascending):");
// Iterate through the sorted array and print each element followed by a space.
foreach (int num in numbersToSort)
{
Console.Write(num + " ");
}
// Print a newline character to move the cursor to the next line after printing the array.
Console.WriteLine();
// Keep the console window open until the user presses a key. This is useful
// so you can see the output before the window closes automatically.
Console.ReadKey();
}
// This is a public static method that calculates the average of the integers
// in the input array. 'public' means it can be accessed from anywhere, 'static'
// means it belongs to the Program class, and 'double' indicates the return type.
// It takes an integer array 'numbers' as input.
public static double CalculateAverage(int[] numbers)
{
// Initialize a variable 'sum' to 0 to store the sum of the array elements.
int sum = 0;
// Iterate through each 'number' in the 'numbers' array using a foreach loop.
foreach (int number in numbers)
{
// Add the current 'number' to the 'sum'.
sum += number;
}
// Calculate the average by dividing the 'sum' by the number of elements in the array
// (numbers.Length). We cast 'sum' to a double to ensure floating-point division.
return (double)sum / numbers.Length;
}
// This is a public static method that finds the maximum value in the input array.
public static int FindMax(int[] numbers)
{
// Assume the first element of the array is the maximum initially.
int max = numbers[0];
// Iterate through the array starting from the second element (index 1).
for (int i = 1; i < numbers.Length; i++)
{
// If the current element 'numbers[i]' is greater than the current 'max',
// update 'max' to the value of the current element.
if (numbers[i] > max)
{
max = numbers[i];
}
}
// After iterating through the entire array, 'max' will hold the largest value.
return max;
}
// This is a public static method that finds the minimum value in the input array.
public static int FindMin(int[] numbers)
{
// Assume the first element of the array is the minimum initially.
int min = numbers[0];
// Iterate through the array starting from the second element (index 1).
for (int i = 1; i < numbers.Length; i++)
{
// If the current element 'numbers[i]' is less than the current 'min',
// update 'min' to the value of the current element.
if (numbers[i] < min)
{
min = numbers[i];
}
}
// After iterating through the entire array, 'min' will hold the smallest value.
return min;
}
// This is a public static method that sorts the input array in ascending order
// using the Bubble Sort algorithm.
public static void SortArrayAscending(int[] numbers)
{
// 'n' stores the length of the array.
int n = numbers.Length;
// The outer loop iterates 'n-1' times. In each pass, the largest unsorted
// element "bubbles up" to its correct position at the end of the unsorted part.
for (int i = 0; i < n - 1; i++)
{
// The inner loop iterates from the beginning of the unsorted part up to
// the 'n-i-1'th element. We don't need to compare the already sorted elements
// at the end of the array.
for (int j = 0; j < n - i - 1; j++)
{
// Compare adjacent elements.
if (numbers[j] > numbers[j + 1])
{
// If the current element is greater than the next element, swap them.
// We use a temporary variable 'temp' to facilitate the swapping.
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
}
// After the outer loop completes, the array will be sorted in ascending order.
// Note that arrays are reference types, so the changes made to the 'numbers'
// array within this method will be reflected in the calling code.
}
}
}