Advertisement
A4L

Chapter - 13 - forEach

A4L
Dec 7th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. /*Minimum and Averaging Revisited. Earlier in this chapter, I presented code to go through an array
  7. and find the minimum value contained in it. I also presented code to average the values in an array.
  8. Using that code as a starting point, rewrite them to use foreach loops instead.*/
  9. namespace Chapter___13___forEach
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int[] array = new int[] { 4, 51, -7, 13, -99, 15, -8, 45, 90 };
  16.             int smallestVal = Int32.MaxValue;
  17.             int avrage = 0;
  18.             foreach(int val in array)
  19.             {
  20.                 if (smallestVal > val) { smallestVal = val; }
  21.                 avrage = avrage + val;
  22.             }
  23.             Console.WriteLine($"The Smallest number in the array is : {smallestVal}");
  24.             Console.WriteLine($"The avrage value in the array is : {avrage/array.Length}");
  25.             Console.ReadKey();
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement