TU-Programmieren_2/lab2/taskA.cpp
2025-04-09 10:22:44 +02:00

62 lines
1.5 KiB
C++

/// @file
/// @brief task A
/// compile/run: g++ -g -std=c++20 taskA.cpp -o taskA && ./taskA
/// @todo add debug statements in function findMax
/// @todo compile and run this file, inspect output
/// @todo runtime debug the program and add breakpoints
/// - at the beginning of each function
/// - inside the loops
/// - at the end of each function
#include <iostream>
#include <vector>
// prints a vector to the console
void print(const std::vector<int>& vec) {
std::cout << "[ ";
for (int i = 0; i < vec.size(); ++i) {
std::cout << vec[i] << " ";
}
std::cout << "]" << std::endl;
}
// takes an input vector and returns a vector where each element is doubled
std::vector<int> doubleVec(std::vector<int> vec) {
std::cout << "'doubleVec' input: ";
print(vec);
for (int i = 0; i < vec.size(); ++i) {
vec[i] *= 2;
std::cout << " loop(i=" << i << "): " << std::endl;
std::cout << " ";
print(vec);
}
std::cout << "'doubleVec' output: ";
print(vec);
return vec;
}
// returns the greatest element of a vector
int findMax(const std::vector<int>& vec) {
int max = vec[0];
for (int i = 1; i < vec.size(); ++i) {
std::cout << " loop(i=" << i << "): " << std::endl;
std::cout << " current: " << vec[i] << std::endl;
if (vec[i] > max) {
max = vec[i];
std::cout << " new max: " << max << std::endl;
}
}
return max;
}
int main() {
std::vector<int> vec = {3, 7, 12, 9, 15};
vec = doubleVec(vec);
int max = findMax(vec);
std::cout << "max: " << max << std::endl;
return 0;
}