The largest and smallest number in the array

This works fine ... but when I use foreach instead of for , it does not work. I do not understand that for and foreach are the same.

 namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int[] array = new int[10]; Console.WriteLine("enter the array elements to b sorted"); for(int i=0;i<10;i++) { array[i] = Convert.ToInt32(Console.ReadLine()); } int smallest = array[0]; for(int i=0;i<10;i++) { if(array[i]<smallest) { smallest=array[i]; } } int largest = array[9]; for(int i=0;i<10;i++) { if (array[i] > largest) { largest = array[i]; } } Console.WriteLine("the smallest no is {0}", smallest); Console.WriteLine("the largest no is {0}", largest); Console.Read(); } } } 
+11
c #
source share
12 answers

Why aren't you using this?

 int[] array = { 12, 56, 89, 65, 61, 36, 45, 23 }; int max = array.Max(); int min = array.Min(); 
+29
source share

If you need to use foreach (for some reason) and don't want to use the bult-in functions, here is a code snippet:

 int minint = array[0]; int maxint = array[0]; foreach (int value in array) { if (value < minint) minint = value; if (value > maxint) maxint = value; } 
+10
source share
  static void PrintSmallestLargest(int[] arr) { if (arr.Length > 0) { int small = arr[0]; int large = arr[0]; for (int i = 0; i < arr.Length; i++) { if (large < arr[i]) { int tmp = large; large = arr[i]; arr[i] = large; } if (small > arr[i]) { int tmp = small; small = arr[i]; arr[i] = small; } } Console.WriteLine("Smallest is {0}", small); Console.WriteLine("Largest is {0}", large); } } 

This way you can have the smallest and largest number in one loop.

+4
source share

You (usually) cannot modify the collection that you execute when using foreach.

Although foreach seems to be similar to the perspective of the developer, they are very different from the perspective of implementation.

Foreach uses Iterator to access individual objects, and for does not know (or does not care) about the basic sequence of objects.

+2
source share

General extension method (gets the value of Min and Max at one iteration):

 public static class MyExtension { public static (T Min, T Max) MinMax<T>(this IEnumerable<T> source) where T : IComparable<T> { if (source == null) { throw new ArgumentNullException(nameof(source)); } T min = source.FirstOrDefault(); T max = source.FirstOrDefault(); foreach (T item in source) { if (item.CompareTo(min) == -1) { min = item; } if (item.CompareTo(max) == 1) { max = item; } } return (Min: min, Max: max); } } 

This code used C # 7 Tuple

+2
source share
 using System; namespace greatest { class Greatest { public static void Main(String[] args) { //get the number of elements Console.WriteLine("enter the number of elements"); int i; i=Convert.ToInt32(Console.ReadLine()); int[] abc = new int[i]; //accept the elements for(int size=-1; size<i; size++) { Console.WriteLine("enter the elements"); abc[size]=Convert.ToInt32(Console.ReadLine()); } //Greatest int max=abc.Max(); int min=abc.Min(); Console.WriteLine("the m", max); Console.WriteLine("the mi", min); Console.Read(); } } } 
+1
source share
 Int[] number ={1,2,3,4,5,6,7,8,9,10}; Int? Result = null; foreach(Int i in number) { If(!Result.HasValue || i< Result) { Result =i; } } Console.WriteLine(Result); } 
0
source share
  public int MinimumValue { get; private set; } public int MaxmimumValue { get; private set; } public void num() { int[] array = { 12, 56, 89, 65, 61, 36, 45, 23 }; MaxmimumValue = array[0]; MinimumValue = array[0]; foreach (int num in array) { if (num > MaxmimumValue) MaxmimumValue = num; if (num < MinimumValue) MinimumValue = num; } Console.WriteLine(MinimumValue); Console.WriteLine(MaxmimumValue); } 
0
source share

Here is the complete program below

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Threading; using System.Diagnostics; namespace oops3 { public class Demo { static void Main(string[] args) { Console.WriteLine("Enter the size of the array"); int x = Convert.ToInt32(Console.ReadLine()); int[] arr = new int[x]; Console.WriteLine("Enter the elements of the array"); for(int i=0;i<x;i++) { arr[i] = Convert.ToInt32(Console.ReadLine()); } int smallest = arr[0]; int Largest = arr[0]; for(int i=0;i<x;i++) { if(smallest>arr[i]) { smallest = arr[i]; } } for (int i = 0; i < x; i++) { if (Largest< arr[i]) { Largest = arr[i]; } } Console.WriteLine("The greater No in the array:" + Largest); Console.WriteLine("The smallest No in the array:" + smallest); Console.ReadLine(); } } } 
0
source share
 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Array_Small_and_lagest { class Program { static void Main(string[] args) { int[] array = new int[10]; Console.WriteLine("enter the array elements to b sorted"); for (int i = 0; i < 10; i++) { array[i] = Convert.ToInt32(Console.ReadLine()); } int smallest = array[0]; foreach (int i in array) { if (i < smallest) { smallest = i; } } int largest = array[9]; foreach (int i in array) { if (i > largest) { largest = i; } } Console.WriteLine("the smallest no is {0}", smallest); Console.WriteLine("the largest no is {0}", largest); Console.Read(); } } } 
0
source share

This is a long time. Maybe like this:

  public int smallestValue(int[] values) { int smallest = int.MaxValue; for (int i = 0; i < values.Length; i++) { smallest = (values[i] < smallest ? values[i] : smallest); } return smallest; } public static int largestvalue(int[] values) { int largest = int.MinValue; for (int i = 0; i < values.Length; i++) { largest = (values[i] > largest ? values[i] : largest); } return largest; } 
0
source share

*

No, for and foreach is not the same. There is some difference between that and for everyone here. I mention the difference:

*

  1. The for loop executes a statement or statement block several times until the specified expression evaluates to false.

You must specify the boundaries of the cycle (minimum or maximum).

 int j = 0; for (int i = 1; i <= 5; i++) { j = j + i ; } 

The foreach statement repeats a group of built-in statements for each element of an array or collection of objects. You do not need to specify the minimum or maximum boundaries of the loop.

 int j = 0; int[] myArr = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in myArr ) { j = j + i ; } 
  1. foreach : treats everything as a collection and reduces performance. foreach creates an instance of the counter (returned from GetEnumerator ()), and this enumerator also maintains state during the foreach loop. Then it re-calls the Next () object in the enumerator and runs your code for each object it returns.

Using the for loop , we iterate over the array in both directions, that is, from the index from 0 to 9 and from 9 to 0.

But using the for-each loop, iteration is only possible in the forward direction.

  1. In a variable declaration, foreach has five variable declarations (three int32 integers and two int32 arrays), while for has only three (two int32 integers and one int32 array).

When it goes into a loop, foreach copies the current array to a new one for the operation. Although it does not care about this part.

  1. For will use int to iterate

Foreach does not use an integer index. Instead, it is used in a collection - it returns each item in order.

0
source share

All Articles