Advertisement
jyoung12387

Arrays - Add to each value in an array

Feb 17th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2.  
  3. namespace random
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[] happiness = { 10, 15, 22, 30 };  //Initialize array of happiness values
  10.             ImproveHappiness(happiness);           //Call ImproveHappieness method to increase all values in array by 2
  11.  
  12.             foreach (int happy in happiness)       //Print new values to console.
  13.             {
  14.                 Console.WriteLine(happy);
  15.             }
  16.             Console.Read();
  17.  
  18.             //Output: 12, 17, 24, 32
  19.         }
  20.         // Method to increase each value in array by 2
  21.         static void ImproveHappiness(int [] happyvalue)
  22.         {
  23.             for (int i = 0; i < happyvalue.Length; i++)
  24.             {
  25.                 happyvalue[i] += 2;
  26.             }
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement