Advertisement
onomato90

Zad.2 Cztery zadania z tablic

Jun 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _4_zadania_z_tablica
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             // [3,56,3,5,78,9,78,4,3,567,8]
  17.             // zad.1 znalezc w tej tablicy maxymalna wartosc
  18.             // zad.2 znalezc minimalna
  19.             // zad.3 suma wszystkich elementow w tablicy
  20.             // zad.4 zliczyc ilosc wystapien przykladowego elementu
  21.  
  22.             int[] table = {3, 56, 3, 5, 78, 9, 78, 4, 3, 567, 8};
  23.  
  24.             int wielkosc = table.Length;
  25.  
  26.             for (int i = 0; i < wielkosc; i++)
  27.             {
  28.                 Console.WriteLine(i+1 + ". wartosc: " + table[i]);
  29.             }
  30.  
  31.             Console.WriteLine();
  32.  
  33.             Console.ReadKey();
  34.  
  35.             // zad.1
  36.  
  37.             int max = table[0];
  38.  
  39.             for (int j = 1; j < wielkosc; j++)
  40.             {
  41.                 if (max < table[j])
  42.                 {
  43.                      max = table[j];
  44.                 }
  45.             }
  46.            
  47.             Console.WriteLine("Najwieksza wartosc w tablicy to: " + max);
  48.  
  49.             // Console.WriteLine($"Najwieksza wartosc to: " + table.Max());
  50.             // Console.WriteLine();
  51.  
  52.             Console.ReadKey();
  53.  
  54.             // zad.2
  55.  
  56.             int min = table[0];
  57.  
  58.             for (int j = 1; j < wielkosc; j++)
  59.             {
  60.                 if (min > table[j])
  61.                 {
  62.                     min = table[j];
  63.                 }
  64.             }
  65.  
  66.             Console.WriteLine("Najmniejsza wartosc w tablicy to: " + min);
  67.  
  68.             // Console.WriteLine("Najwieksza wartosc w tablicy to: " + max);
  69.             // Console.WriteLine($"Najmniejsza wartosc to: " + table.Min());
  70.             // Console.WriteLine();
  71.  
  72.             Console.ReadKey();
  73.  
  74.             // zad.3
  75.  
  76.             int x = table[0];
  77.             int y = x;
  78.  
  79.             for (int m = 1; m < wielkosc; m++)
  80.             {
  81.                 if (y == table[0])
  82.                 {
  83.                     y = x + table[1];
  84.                 }
  85.                 else
  86.                 {
  87.                  y = y + table[m];
  88.                 }
  89.             }
  90.  
  91.             Console.WriteLine("Suma wartosci z tablicy jest rowna: " + y);
  92.             Console.WriteLine();
  93.  
  94.             Console.ReadKey();
  95.  
  96.             // zad.4
  97.  
  98.             for (int g = 0; g < wielkosc; g++)
  99.             {
  100.                 int value = table[g];
  101.                 int ilosc = 0;
  102.  
  103.                 foreach (int item in table)
  104.                 {
  105.                     if (item == value)
  106.                     {
  107.                         ilosc++;
  108.                     }
  109.                 }
  110.  
  111.                 Console.WriteLine("Wartosc " + value + " wystepuje w tablicy " + ilosc + " razy");
  112.             }
  113.            
  114.             Console.ReadKey();
  115.  
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement