Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Skillbox_5_7 : MonoBehaviour {
- private void Start() {
- SummOfEvenIntegersInRange();
- SummOfEvenIntegersInArray();
- IndexOfNumberInArray();
- ArraySelectionSort();
- }
- private void SummOfEvenIntegersInRange() {
- int min = 7, max = 21, summ = 0;
- for (int i = min; i <= max; i++)
- if (i % 2 == 0)
- summ += i;
- Debug.Log("Summ of even integers in range: " + summ.ToString());
- }
- private void SummOfEvenIntegersInArray() {
- int[] array = new int[] { 81, 22, 13, 54, 10, 34, 15, 26, 71, 68 };
- int summ = 0;
- for (int i = 0; i < array.Length; i++)
- if (array[i] % 2 == 0)
- summ += array[i];
- Debug.Log("Summ of even integers in array: " + summ.ToString());
- }
- private void IndexOfNumberInArray() {
- int[] array = new int[] { 81, 22, 13, 34, 10, 34, 15, 26, 71, 68 };
- int numberToFind = 34;
- int index = IndexOf(numberToFind, array);
- Debug.Log("Index of number in array: " + index.ToString());
- }
- private int IndexOf(int number, int[] array) {
- if (array == null || array.Length == 0)
- return -1;
- for (int i = 0; i < array.Length; i++)
- if (array[i] == number)
- return i;
- return -1;
- }
- private void ArraySelectionSort() {
- int[] array = new int[] { 6, 8, 3, 5, 9, 10, 7, 2, 4, 1 };
- Sort(array);
- Debug.Log("Sorted array: " + IntArrayToString(array));
- }
- private void Sort(int[] array) {
- for (int i = 0; i < array.Length - 1; i++) {
- int smallest = i;
- for (int j = i + 1; j < array.Length; j++)
- if (array[j] < array[smallest])
- smallest = j;
- (array[i], array[smallest]) = (array[smallest], array[i]);
- }
- }
- private string IntArrayToString(int[] array) {
- string result = "[ ";
- for (int i = 0; i < array.Length; i++) {
- result += array[i].ToString();
- if (i < array.Length - 1)
- result += ", ";
- }
- return result + " ]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement