Advertisement
leomovskii

Skillbox_5_7

Aug 16th, 2022
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Skillbox_5_7 : MonoBehaviour {
  6.  
  7.     private void Start() {
  8.         SummOfEvenIntegersInRange();
  9.         SummOfEvenIntegersInArray();
  10.         IndexOfNumberInArray();
  11.         ArraySelectionSort();
  12.     }
  13.  
  14.     private void SummOfEvenIntegersInRange() {
  15.         int min = 7, max = 21, summ = 0;
  16.         for (int i = min; i <= max; i++)
  17.             if (i % 2 == 0)
  18.                 summ += i;
  19.         Debug.Log("Summ of even integers in range: " + summ.ToString());
  20.     }
  21.  
  22.     private void SummOfEvenIntegersInArray() {
  23.         int[] array = new int[] { 81, 22, 13, 54, 10, 34, 15, 26, 71, 68 };
  24.         int summ = 0;
  25.         for (int i = 0; i < array.Length; i++)
  26.             if (array[i] % 2 == 0)
  27.                 summ += array[i];
  28.         Debug.Log("Summ of even integers in array: " + summ.ToString());
  29.     }
  30.  
  31.     private void IndexOfNumberInArray() {
  32.         int[] array = new int[] { 81, 22, 13, 34, 10, 34, 15, 26, 71, 68 };
  33.         int numberToFind = 34;
  34.         int index = IndexOf(numberToFind, array);
  35.         Debug.Log("Index of number in array: " + index.ToString());
  36.     }
  37.  
  38.     private int IndexOf(int number, int[] array) {
  39.         if (array == null || array.Length == 0)
  40.             return -1;
  41.         for (int i = 0; i < array.Length; i++)
  42.             if (array[i] == number)
  43.                 return i;
  44.         return -1;
  45.     }
  46.  
  47.     private void ArraySelectionSort() {
  48.         int[] array = new int[] { 6, 8, 3, 5, 9, 10, 7, 2, 4, 1 };
  49.         Sort(array);
  50.         Debug.Log("Sorted array: " + IntArrayToString(array));
  51.     }
  52.  
  53.     private void Sort(int[] array) {
  54.         for (int i = 0; i < array.Length - 1; i++) {
  55.             int smallest = i;
  56.             for (int j = i + 1; j < array.Length; j++)
  57.                 if (array[j] < array[smallest])
  58.                     smallest = j;
  59.             (array[i], array[smallest]) = (array[smallest], array[i]);
  60.         }
  61.     }
  62.  
  63.     private string IntArrayToString(int[] array) {
  64.         string result = "[ ";
  65.         for (int i = 0; i < array.Length; i++) {
  66.             result += array[i].ToString();
  67.             if (i < array.Length - 1)
  68.                 result += ", ";
  69.         }
  70.         return result + " ]";
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement