SHARE
TWEET

Chapter - 15 - Methods (3) - TryItOut

A4L Dec 7th, 2018 71 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. /*Reversing an Array. Let’s make a program that uses methods to accomplish a task. Let’s take an
  7. array and reverse the contents of it. For example, if you have 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, it would
  8. become 10, 9, 8, 7, 6, 5, 4, 3, 2, 1.*/
  9. namespace Chapter___15___Method_TryItOut
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Console.WriteLine("Chapter - 15 - Methods - TryItOut");
  16.             int userInput = GetNumberFromUser();
  17.             int[] numbers = GenNumbers(userInput);
  18.             PrintNumbers(numbers);
  19.             int[] reversed = ReverseNumbers(numbers);
  20.             PrintNumbers(reversed);
  21.             Console.ReadKey();
  22.         }
  23.         /// <summary>
  24.         /// Takes a input from the user of a number between 1 and 30
  25.         /// </summary>
  26.         /// <returns>The user entered number.</returns>
  27.         static int GetNumberFromUser()
  28.         {
  29.             int usersNumber = 0;
  30.             while (usersNumber < 1 || usersNumber > 30)
  31.             {
  32.                 Console.Write("Enter a number between 1 and 30: ");
  33.                 string usersResponse = Console.ReadLine();
  34.                 usersNumber = Convert.ToInt32(usersResponse);
  35.             }
  36.             return usersNumber;
  37.         }
  38.         /// <summary>
  39.         /// Takes a input from the user of a number between 1 and 30
  40.         /// </summary>
  41.         /// <param name="numbers">The first number to multiply</param>
  42.         /// <param name="b">The second number to multiply</param>
  43.         /// <returns>The user entered number.</returns>
  44.         static int[] ReverseNumbers(int[] numbers)
  45.         {
  46.             int[] reversedNums = new int[numbers.Length];
  47.            
  48.             for (int i=0;i<numbers.Length;i++)
  49.             {
  50.                 reversedNums[numbers.Length -1 - i] = numbers[i];
  51.             }
  52.             return reversedNums;
  53.         }
  54.         static void PrintNumbers(int[] numbers)
  55.         {
  56.             Console.Write($"{numbers[0]}");
  57.             for (int i = 1; i < numbers.Length; i++)
  58.             {
  59.                 Console.Write($", {numbers[i]}");
  60.             }
  61.             Console.Write("\n");
  62.         }
  63.  
  64.         static int[] GenNumbers(int n)
  65.         {
  66.             int[] genN = new int[n];
  67.             for (int i=0; i<n;i++)
  68.             {
  69.                 genN[i] = i+1;
  70.             }
  71.             return genN;
  72.         }
  73.     }
  74. }
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