Advertisement
OldBeliver

dinamicArray

Mar 18th, 2021
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.12 KB | None | 0 0
  1.             string userInput = "";
  2.             int[] array = new int[1];
  3.             int[] tempArray;
  4.             int numeric;
  5.             int sum = 0;
  6.             bool exit = true;
  7.  
  8.             while (exit)
  9.             {
  10.                 userInput = Console.ReadLine();
  11.                 if (userInput == "exit")
  12.                 {
  13.                     exit = false;
  14.                 }
  15.                 else if (userInput == "sum")
  16.                 {
  17.                     for (int i = 0; i < array.Length; i++)
  18.                     {
  19.                         sum += array[i];
  20.                     }
  21.                     Console.WriteLine(sum);
  22.                     sum = 0;
  23.                 }
  24.                 else
  25.                 {
  26.                     numeric = Convert.ToInt32(userInput);
  27.                     tempArray = new int[array.Length + 1];
  28.                     for (int i = 0; i < array.Length; i++)
  29.                     {
  30.                         tempArray[i] = array[i];
  31.                     }
  32.                     tempArray[array.Length - 1] = numeric;
  33.                     array = tempArray;
  34.                 }
  35.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement