So here is what I am trying to do - To write a program with an array of 50 values getting the highest number in the array and printing it out. I have hit a brick wall though. I am pretty sure I've gotten very confused with the returning in the function, for example why is index "undefined" out of the for loop in the findSmall and findBig functions?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int findSmallest(int array[], int size, int index)
{
int index_of_smallest_value = index;
for (int i = index + 1; i < size; i++)
{
if (array[i] < array[index_of_smallest_value])
{
index_of_smallest_value = i;
}
}
return index_of_smallest_value;
}
int findBiggest(int array[], int size, int index)
{
int index_of_biggest_value = index;
for (int i = index + 1; i < size; i++)
{
if (array[i] > array[index_of_biggest_value])
{
index_of_biggest_value = i;
}
}
return index_of_biggest_value;
}
int findSmall(int array[], int size)
{
int index = 0;
for (int i = 0; i < size; i++)
{
index = findSmallest(array, size, i);
//cout << index << endl;
}
return index;
}
int findBig(int array[], int size)
{
int index = 0;
for (int i = 0; i < size; i++)
{
index = findBiggest(array, size, i);
//cout << index << endl;
}
return index;
}
int main()
{
int array[50];
srand(time(NULL));
for (int i = 0; i < 50; i++)
array[i] = rand() % 100;
cout << "The smallest digit is " << findSmall(array, 50) << endl;
cout << "The biggest digit is " << findBig(array, 50);
cin.get();
}
I've edited my above code, however I keep getting returned 49 from both findSmall and findBig functions.