Advertisement
Guest User

Untitled

a guest
Jan 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. // funkcje2poprawione.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <cstdlib>
  7. #include <ctime>
  8. #include <cmath>
  9. #define wersja 2
  10.  
  11. #if wersja==1
  12. #define N 5
  13. using namespace std;
  14. int wypisz(const int *wsk, int *a, int *x, int *y)
  15. {
  16.     //int x = 0;
  17.     //int y = 0;
  18.     int roznica = abs(*a - *(wsk + 0 * N + 0));
  19.     for (int i = 0; i < N; ++i)
  20.     {
  21.         for (int j = 0; j < N; ++j)
  22.         {
  23.             if (abs(*a - *(wsk + i*N + j)) < roznica)
  24.             {
  25.                 roznica = abs(*a - *(wsk + i*N + j));
  26.                 *x = i;
  27.                 *y = j;
  28.             }
  29.         }
  30.     }
  31.     return *(wsk + *x*N + *y);
  32.    
  33. }
  34.  
  35.  
  36. int main()
  37. {
  38.     srand(time(NULL));
  39.     int tab[N][N];
  40.  
  41.     for (int i = 0; i < N; ++i)
  42.     {
  43.         for (int j = 0; j < N; ++j)
  44.         {
  45.             tab[i][j] = rand() % 10;
  46.             cout << tab[i][j] << " ";
  47.         }
  48.         cout << endl;
  49.     }
  50.     int a,x,y;
  51.     cout << "Podaj a: " << endl;
  52.     cin >> a;
  53.     cout << "Najblizsza wartosc a="<<wypisz(&tab[0][0], &a, &x, &y)<<", x=";
  54.     cout << x << ", y="<< y;
  55.     cout << endl;
  56.     return 0;
  57. }
  58. #endif//(wersja==1)
  59. #if wersja==2
  60. #define N 5
  61. using namespace std;
  62.  
  63. void insert(int tab[], int a)
  64. {
  65.     int i = 1, wpis, j;
  66.     while (i<a)
  67.     {
  68.         wpis = tab[i];
  69.         j = i - 1;
  70.         while (tab[j]>wpis && j >= 0)
  71.         {
  72.  
  73.             tab[j + 1] = tab[j];
  74.             --j;
  75.         }
  76.         tab[j + 1] = wpis;
  77.         ++i;
  78.     }
  79. }
  80.  
  81. int podzial(int tab[], int poczatek, int koniec)
  82. {
  83.     int start = tab[poczatek];
  84.     int i = poczatek;
  85.     int j = koniec;
  86.     while (true)
  87.     {
  88.         while (tab[i] > start) {
  89.             i++;
  90.         }
  91.         while (tab[j] < start) {
  92.             j--;
  93.         }
  94.         if (i < j) {
  95.             int wpis = tab[i];
  96.             tab[i] = tab[j];
  97.             tab[j] = wpis;
  98.             if (tab[i] == tab[j])
  99.             {
  100.                 i++;
  101.                 j--;
  102.             }
  103.  
  104.         }
  105.  
  106.         else
  107.         {
  108.             return j;
  109.         }
  110.     }
  111. }
  112. void quicksort(int tab[], int poczatek, int koniec)
  113. {
  114.     if (poczatek < koniec)
  115.     {
  116.         int srodek = podzial(tab, poczatek, koniec);
  117.         quicksort(tab, poczatek, srodek);
  118.         quicksort(tab, srodek + 1, koniec);
  119.     }
  120. }
  121.  
  122. int main()
  123. {
  124.     srand(time(NULL)); //Breakuje quicksort niewiadomo czemu
  125.     int tab[N];
  126.     int random;
  127.     for (int i = 0; i<N; i++)
  128.     {
  129.         random = rand() % 10;
  130.         tab[i] = random;
  131.     }
  132.  
  133.     cout << "Przed sortowaniem: " << endl;
  134.     for (int i = 0; i<N; i++)
  135.     {
  136.         cout << tab[i] << ' ';
  137.     }
  138.     cout << endl;
  139.     insert(tab, N);
  140.     cout << "Sortowanie przez wstawianie: " << endl;
  141.     for (int i = 0; i<N; i++)
  142.     {
  143.         cout << tab[i] << ' ';
  144.     }
  145.     cout << endl;
  146.     cout << "Sortowanie quicksort: " << endl;
  147.     quicksort(tab, 0, N - 1);
  148.     for (int i = 0; i<N; i++)
  149.     {
  150.         cout << tab[i] << ' ';
  151.     }
  152.     cout << endl;
  153.     return 0;
  154. }
  155. #endif//(wersja==2)
  156. #if wersja==3
  157. using namespace std;
  158.  
  159. void licz(int liczba, int podstawa)
  160. {
  161.     int i;
  162.     char liczby[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S','T','U' };
  163.     char *wsk = &liczby[0];
  164.     if (liczba>0)
  165.     {
  166.         licz(liczba / podstawa, podstawa);
  167.         i = liczba%podstawa;
  168.         if (i<10)
  169.             cout << i;
  170.         else
  171.             cout << *(wsk + (i - 10));
  172.     }
  173. }
  174.  
  175. int main()
  176. {
  177.     int podstawa, liczba;
  178.     cout << "Podaj liczbe: " << endl;
  179.     cin >> liczba;
  180.     cout << "Podaj podstawe systemu liczbowego: " << endl;
  181.     cin >> podstawa;
  182.     licz(liczba, podstawa);
  183.     cout << endl;
  184.     return 0;
  185. }
  186. #endif//(wersja==3)
  187.  
  188. #if wersja==4
  189. #define N 5
  190. using namespace std;
  191. void kolumna(int (*tab)[N], int kolumny = N - 1)
  192. {
  193.     for (int i = 0; i<N; i++)
  194.     {
  195.         for (int j = 0; j<N; j++)
  196.         {
  197.             if (j == kolumny)
  198.                 cout << tab[i][j] << " ";
  199.             else
  200.                 cout << "  ";
  201.         }
  202.         cout << endl;
  203.     }
  204. }
  205. int main()
  206. {
  207.     int tab[N][N];
  208.     int kol;
  209.     srand(time(NULL));
  210.     for (int i = 0; i<N; i++)
  211.     {
  212.         for (int j = 0; j<N; j++)
  213.         {
  214.             tab[i][j] = rand() % 10;
  215.         }
  216.     }
  217.     for (int i = 0; i < N; i++)
  218.     {
  219.         for (int j = 0; j < N; j++)
  220.         {
  221.             cout << tab[i][j] << " ";
  222.         }
  223.         cout << endl;
  224.     }
  225.     cout << "Podaj nr kolumny: " << endl;
  226.     cin >> kol;
  227.     kolumna(tab, kol);
  228.     cout << "Wywolanie bez argumentow: " << endl;
  229.     kolumna(tab);
  230.     return 0;
  231. }
  232. #endif//(wersja==4)
  233.  
  234. #if wersja==5
  235. #define N 10
  236. using namespace std;
  237. int element(int tab[])
  238. {
  239.     int max = numeric_limits<int>::min();
  240.  
  241.     for (int i = 0; i<N; i++)
  242.     {
  243.         if (tab[i] > max)
  244.         {
  245.             max = tab[i];
  246.         }
  247.     }
  248.  
  249.     return max;
  250. }
  251.  
  252. int element(int tab[], int *a)
  253. {
  254.     int max = numeric_limits<int>::min();
  255.  
  256.     for (int i = 0; i<N; i++)
  257.     {
  258.         if (tab[i] > max)
  259.         {
  260.             max = tab[i];
  261.         }
  262.     }
  263.     for (int i = 0; i<N; i++)
  264.     {
  265.  
  266.         if (tab[i] == max)
  267.         {
  268.             *a = i;
  269.             break;
  270.         }
  271.     }
  272.  
  273.     return max;
  274. }
  275.  
  276. int main()
  277. {
  278.    
  279.     srand(time(NULL));
  280.     int tab[N];
  281.     for (int i = 0; i<N; i++)
  282.     {
  283.         tab[i] = rand() % 100;
  284.         cout << tab[i] << " ";
  285.     }
  286.     int a;
  287.     cout << endl;
  288.     cout << "wartosc max: " << element(tab);
  289.     int max = element(tab);
  290.     cout << endl;
  291.     cout << "max=" << element(tab, &a)<<", i=";
  292.     cout << a;
  293.     cout << endl;
  294.     return 0;
  295. }
  296. #endif//(wersja==5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement