There are several problems with the code:
1. as others have mentioned <stdio.h> is a C header, not a C++ header.
2. std::cout is part of the <iostream> header.
3. can't define a non lamba function within another function. main is a function.
4. a function can only return one value, not two. need 2 (or more) values returned, pass variables to hold the values as references or pointers, and the function return type should be changed to void.
5. your for loop at line 8 is ill-formed, your condition part (i > arrSize) is wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
#include <iterator>
void martialarts(int arr[], int arrSize, int& max, int& min)
{
for (int i { }; i < arrSize; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
if (arr[i] < min)
{
min = arr[i];
}
}
}
int main()
{
int arr[] { 1, 4, 6, 7, 8 };
// int arrSize = sizeof(arr) / sizeof(arr[0]);
int arrSize { std::size(arr) };
int max { };
int min { 65556 };
martialarts(arr, arrSize, max, min);
std::cout << max << '\n';
std::cout << min << '\n';
}
|
Passing a C++ container, such as a std::vector, is easier since the container retains its size. A vector's size can be retrieved using the std::vector::size member function.
http://www.cplusplus.com/reference/vector/vector/size/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <iostream>
#include <vector>
void martialarts(const std::vector<int>& arr, int& max, int& min)
{
for (size_t i { }; i < arr.size(); i++)
{
if (arr[i] > max)
{
max = arr[i];
}
if (arr[i] < min)
{
min = arr[i];
}
}
}
int main()
{
std::vector<int> arr { 4, 6, 1, 7, 8 };
int max { };
int min { 65556 };
martialarts(arr, max, min);
std::cout << max << '\n';
std::cout << min << '\n';
}
|