Advertisement
haquaa

Untitled

May 26th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 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.  
  7. namespace _2015_C_sharp_Exam
  8. {
  9.     class Program
  10.     {
  11.  
  12.         /*
  13.             arrays are passed to methods  by reference that
  14.             means that the actual values in the array will
  15.             change, not just in the method, so I created a new
  16.             array called copy, and copied the values of the
  17.             array to it, because I don't want the default values
  18.             of the array to change because it will be used later
  19.             in another method ( SubtractTen() )
  20.          */
  21.         static void MultiblyByFive(int[] arr)
  22.         {
  23.             int[] copy = new int[arr.Length];
  24.  
  25.             for (int i = 0; i < arr.Length; i++)
  26.             {
  27.                 Console.Write(arr[i] + " ");
  28.                 copy[i] = arr[i];
  29.             }
  30.  
  31.             for (int i = 0; i < copy.Length; i++)
  32.             {
  33.                 copy[i] *= 5; //arr[i] = arr[i] * 5;
  34.             }
  35.  
  36.             Console.WriteLine();
  37.  
  38.             for (int i = 0; i < arr.Length; i++)
  39.             {
  40.                 Console.Write(copy[i] + " ");
  41.             }
  42.  
  43.             Console.WriteLine();
  44.         }
  45.  
  46.         static void SubtractTen(int[] arr)
  47.         {
  48.  
  49.             Console.WriteLine(arr[2]);
  50.  
  51.             arr[2] -= 10;
  52.  
  53.             Console.WriteLine(arr[2]);
  54.         }
  55.  
  56.         static void Main(string[] args)
  57.         {
  58.             //Question 3 - b
  59.  
  60.             Console.Write("Enter the size of the array : ");
  61.  
  62.             int size = int.Parse(Console.ReadLine());
  63.  
  64.             int[] arr = new int[size];
  65.  
  66.             Console.WriteLine("Enter the array elements : ");
  67.  
  68.             for (int i = 0; i < size; i++)
  69.             {
  70.                 arr[i] = int.Parse(Console.ReadLine());
  71.             }
  72.  
  73.             Console.WriteLine("Default array and after multiblying by five : ");
  74.  
  75.             MultiblyByFive(arr);
  76.  
  77.             Console.WriteLine("Default third element value and after subtracting 10 :");
  78.  
  79.             SubtractTen(arr);
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement