Advertisement
A4L

Chapter - 13 - Arrays

A4L
Dec 7th, 2018
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 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. /*Copying an Array. Write code to create a copy of an array. First, start by creating an initial array. (You
  7. can use whatever type of data you want.) Let’s start with 10 items. Declare an array variable and
  8. assign it a new array with 10 items in it. Use the things we’ve discussed to put some values in the
  9. array.
  10. Now create a second array variable. Give it a new array with the same length as the first. Instead of
  11. using a number for this length, use the Length property to get the size of the original array.
  12. Use a loop to read values from the original array and place them in the new array. Also print out the
  13. contents of both arrays, to be sure everything copied correctly.*/
  14.  
  15. namespace Chapter___13___Arrays
  16. {
  17.     class Program
  18.     {
  19.         static void Main(string[] args)
  20.         {
  21.             //int[] arraySource = new int[10]{ 4, 51, -7, 13, -99, 15, -8, 45, 90, 16 };
  22.             int[] arraySource = new[] { 4, 51, -7, 13, -99, 15, -8, 45, 90, 16 };
  23.             int[] arrayCopy = new int[arraySource.Length];
  24.             for (int i = 0; i < arraySource.Length; i++)
  25.             {
  26.                 arrayCopy[i] = arraySource[i];
  27.             }
  28.             Console.WriteLine();
  29.             for (int i=0; i<arraySource.Length;i++)
  30.             {
  31.                 if (i >= 1)
  32.                 {
  33.                     Console.Write($" , {arraySource[i]}");
  34.                 }
  35.                 else { Console.Write($"Array Source = {arraySource[i]}"); }
  36.             }
  37.             Console.WriteLine();
  38.             for (int i = 0; i < arrayCopy.Length; i++)
  39.             {
  40.                 if (i >= 1)
  41.                 {
  42.                     Console.Write($" , {arrayCopy[i]}");
  43.                 }
  44.                 else { Console.Write($"Array Copy = {arrayCopy[i]}"); }
  45.             }
  46.             Console.WriteLine();
  47.  
  48.             Console.ReadKey();
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement