Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // funkcje2poprawione.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- #define wersja 2
- #if wersja==1
- #define N 5
- using namespace std;
- int wypisz(const int *wsk, int *a, int *x, int *y)
- {
- //int x = 0;
- //int y = 0;
- int roznica = abs(*a - *(wsk + 0 * N + 0));
- for (int i = 0; i < N; ++i)
- {
- for (int j = 0; j < N; ++j)
- {
- if (abs(*a - *(wsk + i*N + j)) < roznica)
- {
- roznica = abs(*a - *(wsk + i*N + j));
- *x = i;
- *y = j;
- }
- }
- }
- return *(wsk + *x*N + *y);
- }
- int main()
- {
- srand(time(NULL));
- int tab[N][N];
- for (int i = 0; i < N; ++i)
- {
- for (int j = 0; j < N; ++j)
- {
- tab[i][j] = rand() % 10;
- cout << tab[i][j] << " ";
- }
- cout << endl;
- }
- int a,x,y;
- cout << "Podaj a: " << endl;
- cin >> a;
- cout << "Najblizsza wartosc a="<<wypisz(&tab[0][0], &a, &x, &y)<<", x=";
- cout << x << ", y="<< y;
- cout << endl;
- return 0;
- }
- #endif//(wersja==1)
- #if wersja==2
- #define N 5
- using namespace std;
- void insert(int tab[], int a)
- {
- int i = 1, wpis, j;
- while (i<a)
- {
- wpis = tab[i];
- j = i - 1;
- while (tab[j]>wpis && j >= 0)
- {
- tab[j + 1] = tab[j];
- --j;
- }
- tab[j + 1] = wpis;
- ++i;
- }
- }
- int podzial(int tab[], int poczatek, int koniec)
- {
- int start = tab[poczatek];
- int i = poczatek;
- int j = koniec;
- while (true)
- {
- while (tab[i] > start) {
- i++;
- }
- while (tab[j] < start) {
- j--;
- }
- if (i < j) {
- int wpis = tab[i];
- tab[i] = tab[j];
- tab[j] = wpis;
- if (tab[i] == tab[j])
- {
- i++;
- j--;
- }
- }
- else
- {
- return j;
- }
- }
- }
- void quicksort(int tab[], int poczatek, int koniec)
- {
- if (poczatek < koniec)
- {
- int srodek = podzial(tab, poczatek, koniec);
- quicksort(tab, poczatek, srodek);
- quicksort(tab, srodek + 1, koniec);
- }
- }
- int main()
- {
- srand(time(NULL)); //Breakuje quicksort niewiadomo czemu
- int tab[N];
- int random;
- for (int i = 0; i<N; i++)
- {
- random = rand() % 10;
- tab[i] = random;
- }
- cout << "Przed sortowaniem: " << endl;
- for (int i = 0; i<N; i++)
- {
- cout << tab[i] << ' ';
- }
- cout << endl;
- insert(tab, N);
- cout << "Sortowanie przez wstawianie: " << endl;
- for (int i = 0; i<N; i++)
- {
- cout << tab[i] << ' ';
- }
- cout << endl;
- cout << "Sortowanie quicksort: " << endl;
- quicksort(tab, 0, N - 1);
- for (int i = 0; i<N; i++)
- {
- cout << tab[i] << ' ';
- }
- cout << endl;
- return 0;
- }
- #endif//(wersja==2)
- #if wersja==3
- using namespace std;
- void licz(int liczba, int podstawa)
- {
- int i;
- char liczby[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','R','S','T','U' };
- char *wsk = &liczby[0];
- if (liczba>0)
- {
- licz(liczba / podstawa, podstawa);
- i = liczba%podstawa;
- if (i<10)
- cout << i;
- else
- cout << *(wsk + (i - 10));
- }
- }
- int main()
- {
- int podstawa, liczba;
- cout << "Podaj liczbe: " << endl;
- cin >> liczba;
- cout << "Podaj podstawe systemu liczbowego: " << endl;
- cin >> podstawa;
- licz(liczba, podstawa);
- cout << endl;
- return 0;
- }
- #endif//(wersja==3)
- #if wersja==4
- #define N 5
- using namespace std;
- void kolumna(int (*tab)[N], int kolumny = N - 1)
- {
- for (int i = 0; i<N; i++)
- {
- for (int j = 0; j<N; j++)
- {
- if (j == kolumny)
- cout << tab[i][j] << " ";
- else
- cout << " ";
- }
- cout << endl;
- }
- }
- int main()
- {
- int tab[N][N];
- int kol;
- srand(time(NULL));
- for (int i = 0; i<N; i++)
- {
- for (int j = 0; j<N; j++)
- {
- tab[i][j] = rand() % 10;
- }
- }
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < N; j++)
- {
- cout << tab[i][j] << " ";
- }
- cout << endl;
- }
- cout << "Podaj nr kolumny: " << endl;
- cin >> kol;
- kolumna(tab, kol);
- cout << "Wywolanie bez argumentow: " << endl;
- kolumna(tab);
- return 0;
- }
- #endif//(wersja==4)
- #if wersja==5
- #define N 10
- using namespace std;
- int element(int tab[])
- {
- int max = numeric_limits<int>::min();
- for (int i = 0; i<N; i++)
- {
- if (tab[i] > max)
- {
- max = tab[i];
- }
- }
- return max;
- }
- int element(int tab[], int *a)
- {
- int max = numeric_limits<int>::min();
- for (int i = 0; i<N; i++)
- {
- if (tab[i] > max)
- {
- max = tab[i];
- }
- }
- for (int i = 0; i<N; i++)
- {
- if (tab[i] == max)
- {
- *a = i;
- break;
- }
- }
- return max;
- }
- int main()
- {
- srand(time(NULL));
- int tab[N];
- for (int i = 0; i<N; i++)
- {
- tab[i] = rand() % 100;
- cout << tab[i] << " ";
- }
- int a;
- cout << endl;
- cout << "wartosc max: " << element(tab);
- int max = element(tab);
- cout << endl;
- cout << "max=" << element(tab, &a)<<", i=";
- cout << a;
- cout << endl;
- return 0;
- }
- #endif//(wersja==5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement