Created
September 9, 2013 17:02
-
-
Save pstachula-dev/6498512 to your computer and use it in GitHub Desktop.
SDiZO, Lab1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <time.h> | |
using namespace std; | |
struct element | |
{ | |
int liczba; | |
char znak; | |
}; | |
element **tab_wsk; | |
element **losowanie(int n) | |
{ | |
tab_wsk = (element **)malloc(n * sizeof(element)); | |
for(int i = 0; i < n; i++) | |
{ | |
tab_wsk[i] = (element *)malloc(sizeof(element)); | |
tab_wsk[i]->liczba = rand() % 10; | |
tab_wsk[i]->znak = rand() % 10 + 65; | |
} | |
return tab_wsk; | |
} | |
void wyswietl(element **tab_wsk, int n) | |
{ | |
for(int i = 0; i < n; i++) | |
{ | |
cout << "Liczba: " << tab_wsk[i]->liczba << endl; | |
cout << "Znak: " << tab_wsk[i]->znak << endl << endl; | |
} | |
} | |
void sortowanie(element **tab_wsk, int n) | |
{ | |
int i, j; | |
element *temp; | |
for (i = 0; i < n; i++) | |
{ | |
for (j = 0; j < n - 1 - i; j++) | |
{ | |
if (tab_wsk[j]->liczba > tab_wsk[j + 1]->liczba) | |
{ | |
swap(tab_wsk[j], tab_wsk[j + 1]); | |
} | |
} | |
} | |
} | |
void kasowanie(element **tab_wsk, int n) | |
{ | |
for(int i = 0; i < n; i++) | |
{ | |
free(tab_wsk[i]); | |
} | |
free(tab_wsk); | |
} | |
int main() | |
{ | |
srand (time(NULL)); | |
int n; | |
cout << "Podaj ilosc struktur: "; | |
cin >> n; | |
losowanie(n); | |
wyswietl(tab_wsk, n); | |
sortowanie(tab_wsk, n); | |
cout << "Po sortowaniu \n"; | |
wyswietl(tab_wsk, n); | |
kasowanie(tab_wsk, n); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment