Creating a C# console application to find the lowest and highest value in an array
In this article we will show you how you can create an C# console application to find the lowest value and highest value in a given array.
Let's say that the given array is: {10,1,4,-3,7,5,6,9,8}.
1. Store the array values in an integer array
To store this array in C#, we need to declare an integer array and initialize it with the given values.
int[] array = new int[] {10,1,4,-3,7,5,6,9,8}; // This code will create an integer array and will initialize it with the given values.
2. Iterating through array values and showing them in console window
If we want to show the given array in console window, then we should use the for loop to iterate through array values and show them in console window, the for loop code for this will look like this:
for(int i=0; i < array.Length; i++) // this code will iterate through array elements
{
Console.Write("{0} ", array[i]); // this code will show the array values in console window
}
3. Declaring the variables to store the lowest and highest values
After declaring the array, initializing it and showing it in console window, we need to declare two variables to store the lowest and highest numbers in array. To do this we need to write the following code:
int lowestNumber = array[0]; // this code will create a variable and store it with the first element of the array
int highestNumber = array[0]; // this code will create a variable and store it with the first element of the array
4. Finding the lowest and highest value in array
After creating the variables to store the lowest and highest values, we need to use the for loop to iterate through each element of the given array and compare it with the actual values of lowestNumber and highestNumber variables, which currently are initialized with the first element of the array.
The following code will iterate through array elements and compare the values:
for(int i=1; i < array.Length; i++) // this code will iterate through array elements
{
if(array[i] < lowestNumber) // this code will compare the actual value of lowestNumber with the current index value of array
{
lowestNumber = array[i]; // this code will change the actual value of lowestNumber with the current index value of array, if the condition is true
}
else if(array[i] > highestNumber) // this code will compare the actual value of highestNumber with the current index value of array
{
highestNumber = array[i]; // this code will change the actual value of highestNumber with the current index value of array, if the condition is true
}
}
5. Showing the result on console window
After finding the lowest and highest value in the array we need to show them in console window.
To do this we need to add the following code:
Console.WriteLine("The lowest number in array is: {0}", smallestNumber); // this will show the lowest number in array
Console.WriteLine("The highest number in array is: {0}", biggestNumber); // this will show the highest number in array
6. Complete code of console application
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
int[] array = new int[] {10,1,4,-3,7,5,6,9,8};
Console.WriteLine("The given array is: ");
for(int i=0; i < array.Length; i++)
{
Console.Write("{0} ", array[i]);
}
Console.WriteLine("");
int lowestNumber = array[0];
int highestNumber = array[0];
for(int i=1; i < array.Length; i++)
{
if(array[i] < lowestNumber)
{
lowestNumber = array[i];
}
else if(array[i] > highestNumber)
{
highestNumber = array[i];
}
}
Console.WriteLine("Numri me i vogel ne varg eshte: {0}", lowestNumber);
Console.WriteLine("Numri me i madhe ne varg eshte: {0}", highestNumber);
Console.ReadLine();
}
}
The result
Tags:
Coding