Created
April 20, 2025 15:27
-
-
Save Grimitch/7fb948952fcbd858251c2216aa2be8c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <vector> | |
#include <limits> | |
double average(const std::vector<int>& arr) { | |
if (arr.empty()) return 0; | |
double sum = 0; | |
for (int num : arr) { | |
sum += num; | |
} | |
return sum / arr.size(); | |
} | |
void minMax(const std::vector<int>& arr) { | |
if (arr.empty()) { | |
std::cout << "array is empty\n"; | |
return; | |
} | |
int minV = arr[0], maxV = arr[0]; | |
int minI = 0, maxI = 0; | |
for (size_t i = 1; i < arr.size(); ++i) { | |
if (arr[i] < minV) { | |
minV = arr[i]; | |
minI = i; | |
} | |
if (arr[i] > maxV) { | |
maxV = arr[i]; | |
maxI = i; | |
} | |
} | |
std::cout << "minimum: " << minV << ", index: " << minI << std::endl; | |
std::cout << "Maximum: " << maxV << ", index: " << maxI << std::endl; | |
} | |
void reverseArray(std::vector<int>& arr) { | |
size_t left = 0, right = arr.size() - 1; | |
while (left < right) { | |
std::swap(arr[left], arr[right]); | |
++left; | |
--right; | |
} | |
} | |
int main() { | |
std::vector<int> arr = { 10, 5, 20, 3, 7 }; | |
std::cout << "arithmetic mean: " << average(arr) << std::endl; | |
minMax(arr); | |
reverseArray(arr); | |
std::cout << "array after reverse: "; | |
for (int num : arr) { | |
std::cout << num << " "; | |
} | |
std::cout << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment