Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Zad1._3
- {
- class Program
- {
- static void Main(string[] args)
- {
- // Stworzenie tablicy oraz obiektu klasy Random:
- int[] array = new int[3];
- Random random = new Random();
- // Uzupełnienie tablicy losowymi wartościami oraz jej wyświetlenie:
- Console.WriteLine("Original array:");
- for (int i = 0; i < array.Length; i++)
- {
- array[i] = random.Next(70);
- }
- DisplayArray(array);
- // Sortowanie (w tym przypadku: bąbelkowe):
- for (int j = 0; j < array.Length - 1; j++)
- {
- for (int i = 0; i < array.Length - 1; i++)
- {
- // Jeżeli element AKTUALNY jest większy od NASTĘPNY to zamien je ze sobą:
- if (array[i] > array[i + 1])
- {
- int temporary = array[i];
- array[i] = array[i + 1];
- array[i + 1] = temporary;
- }
- }
- }
- // Wyświetlenie posortowanej tablicy:
- Console.WriteLine();
- Console.WriteLine("Sorted array:");
- DisplayArray(array);
- // Ile razy występuje dana wartość:
- Console.WriteLine();
- Console.WriteLine("Occurence counts:");
- List<int> list = new List<int>();
- for (int j = 0; j < array.Length; j++)
- {
- int number = array[j];
- if (list.Contains(number))
- {
- continue;
- }
- list.Add(number);
- int numberCount = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (number == array[i])
- {
- numberCount++;
- }
- }
- Console.WriteLine(number + ": " + numberCount);
- }
- // Suma:
- Console.WriteLine();
- Console.WriteLine("Sum:");
- int sum = 0;
- for (int i = 0; i < array.Length; i++)
- {
- sum += array[i];
- }
- Console.WriteLine(sum);
- // Kopiowanie tablic:
- Console.WriteLine();
- Console.WriteLine("Performing two (wrong and proper) array copies...");
- int[] wrongCopiedArray = array;
- int[] properCopiedArray = CopyArray(array);
- Console.WriteLine();
- Console.WriteLine("Changing first original array item...");
- array[0] = -100;
- Console.WriteLine();
- Console.WriteLine("Original array with changed first element to -100:");
- DisplayArray(array);
- Console.WriteLine();
- Console.WriteLine("Wrong copied array:");
- DisplayArray(wrongCopiedArray);
- Console.WriteLine();
- Console.WriteLine("Proper copied array:");
- DisplayArray(properCopiedArray);
- Console.ReadLine();
- }
- private static void DisplayArray(int[] someOtherArray)
- {
- for (int i = 0; i < someOtherArray.Length; i++)
- {
- Console.WriteLine((i + 1) + ": " + someOtherArray[i]);
- }
- }
- private static int[] CopyArray(int[] originalArray)
- {
- int[]copiedTable = new int[originalArray.Length];
- for (int i = 0; i < originalArray.Length; i++)
- {
- copiedTable[i] = originalArray[i];
- }
- return copiedTable;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement