daily pastebin goal
37%
SHARE
TWEET

Chapter - 13 - forEach

A4L Dec 7th, 2018 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top