Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- struct Struktura{
- int i;
- float f;
- char c;
- };
- string print(Struktura** tab){
- string output ="";
- for(int i=0;i<20;i++){
- output += to_string(tab[i]->i) + " ";
- }
- return output;
- }
- Struktura** losowanie(int N){
- struct Struktura** tab=(struct Struktura**) malloc(N*sizeof(struct Struktura));
- bool uniqueNumbers[10000];
- int randomNumber;
- for(int i=0;i<N;i++){
- tab[i] = (struct Struktura*) malloc(sizeof(struct Struktura));
- bool uniqueNumberFound = false;
- while(uniqueNumberFound==false){
- randomNumber = (rand()%10001)-1000;
- int indexInArray;
- if(randomNumber<0){
- indexInArray=randomNumber+9000;}
- else{
- indexInArray=randomNumber;
- }
- if(uniqueNumbers[indexInArray] != true){
- uniqueNumbers[indexInArray] = true;
- uniqueNumberFound = true;
- }
- }
- tab[i]->i=randomNumber;
- tab[i]->c = 'B' + rand() % 18;
- tab[i]->f = 1000 + i;
- }
- return tab;
- }
- void kasowanie(Struktura** tab, int n){
- for(int i=0;i<n;i++){
- free(tab[i]);
- }
- free(tab);
- }
- void sortowanie(Struktura** tab, int N){
- bool sortedArray;
- for(int i=0;i<N;i++)
- {
- sortedArray = true;
- for( int j=0;j<N-i-1;j++)
- {
- if(tab[j]->i > tab[j+1]->i){
- Struktura* temp = tab[j];
- tab[j] = tab[j + 1];
- tab[j + 1] = temp;
- sortedArray = false;
- }
- }
- if(sortedArray==true){
- return;
- }
- }
- }
- int zliczanieZnakow(Struktura** tab, int n, char c){
- int counter = 0;
- for(int i=0;i<n;i++){
- if(tab[i]->c == c){
- counter++;
- }
- }
- return counter;
- }
- int main()
- {
- srand(time(NULL));
- int N;
- char X;
- FILE* fp = fopen("inlab01.txt", "r");
- if (fp == NULL)
- return -1;
- fscanf (fp, "%d %c", &N, &X);
- fclose(fp);
- clock_t begin, end;
- double time_spent;
- begin = clock();
- Struktura** s = losowanie(N);
- sortowanie(s, N);
- int charReps = zliczanieZnakow(s, N, X);
- string t1 = print(s);
- cout << t1;
- kasowanie(s, N);
- end = clock();
- time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- cout << charReps << " " << time_spent;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement