Last active
October 19, 2020 09:01
-
-
Save RestoreMonarchy/9a5349873b01831e0409ede7d9db06c5 to your computer and use it in GitHub Desktop.
School Task #1 10/19/2020
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 <cstdlib> | |
#include <time.h> | |
using namespace std; | |
void Fibonaci(int length); | |
void FillWithRandom100Numbers(int arr[]); | |
void OrderByDescending(int arr[]); | |
void Print100Numbers(int arr[]); | |
int GetMaxIn100Numbers(int arr[]); | |
int GetMinIn100Numbers(int arr[]); | |
int main() | |
{ | |
int fiboLength; | |
cin >> fiboLength; | |
// coutuje ciag fibonaciego | |
Fibonaci(fiboLength); | |
int arr[100]; | |
FillWithRandom100Numbers(arr); | |
cout << "Min number in arr: " << GetMinIn100Numbers(arr) << endl; | |
cout << "Max number in arr: " << GetMaxIn100Numbers(arr) << endl; | |
// I could get max and min by getting last and first index of arr after OrderByDescending | |
OrderByDescending(arr); | |
Print100Numbers(arr); | |
} | |
int GetMaxIn100Numbers(int arr[]) | |
{ | |
int max = INT_MIN; | |
for (int i = 0; i < 100; i++) | |
{ | |
if (arr[i] > max) | |
{ | |
max = arr[i]; | |
} | |
} | |
return max; | |
} | |
int GetMinIn100Numbers(int arr[]) | |
{ | |
int min = INT_MAX; | |
for (int i = 0; i < 100; i++) | |
{ | |
if (arr[i] < min) | |
{ | |
min = arr[i]; | |
} | |
} | |
return min; | |
} | |
void Print100Numbers(int arr[]) | |
{ | |
for (int i = 0; i < 100; i++) | |
{ | |
cout << arr[i] << " "; | |
} | |
} | |
void OrderByDescending(int arr[]) | |
{ | |
int temp, actions = 0; | |
for (int i = 0; i < 100; i++) | |
{ | |
for (int i2 = 0; i2 < 99; i2++) { | |
if (arr[i2] > arr[i2 + 1]) | |
{ | |
actions++; | |
temp = arr[i2]; | |
arr[i2] = arr[i2 + 1]; | |
arr[i2 + 1] = temp; | |
} | |
} | |
} | |
cout << "Ordering took " << actions << " actions" << endl; | |
} | |
void FillWithRandom100Numbers(int arr[]) | |
{ | |
srand(time(NULL)); | |
for (int i = 0; i < 100; i++) | |
{ | |
arr[i] = rand() % 1000; | |
} | |
} | |
void Fibonaci(int length) | |
{ | |
int num, temp1 = 0, temp2 = 1; | |
for (int i = 0; i < length; i++) | |
{ | |
if (i < 2) | |
{ | |
// skip zero | |
if (i == 0) | |
continue; | |
num = i; | |
} | |
else { | |
num = temp1 + temp2; | |
temp1 = temp2; | |
temp2 = num; | |
} | |
cout << "For " << i << ": " << num << " " << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment