Here is what I try to do:
The purpose of this program is to find the two smallest elements in several integer arrays, each containing 100 elements. The program will use functions with the prototypes: call the function Two_smallest to find the two smallest elements in the array and their subscripts (if there are two equal smallest elements, detect both – they are solution to the problem); this function MUST leave the original array unchanged; display the two smallest elements with their subscripts in main.
can someone help me or show me how to pass to back to main? and another question is this a correct way to find the two smallest elements in the array? and i dont understand what is this mean?

``````FYI: English is my 7th lang and I also have a random array already

void two_smallest (int find_array[] , int size3, int *a, int *a2, int *b,int *b2)
{
for (int index = 0; index <= 100; index ++)
{
if (find_array[index] < find_array )
{
find_array = find_array [index];
*a = find_array;

}
}
cout <<"The first lowest number in this call is " << *a << endl; //testing

for (int index_2=101 ; index_2 < 200; index_2 ++)
{
if (find_array [index_2] < find_array )
{
find_array  = find_array [index_2];
*b = find_array;
}
}
cout <<"The second lowest number is " << *a << endl; // testing
``````

Several comments, I hope you will be able to use them to improve your code.

First, you say you are not to modify the input array, but you keep storing the largest found value at index 0 of the array. Better to allocate variables to store the largest value and the index of that. Assign those values to the reference parameters at the end of the loop(s)

If you are searching in an array of size 100, your loop is off by one (the dreaded OBO error)
`for (int index = 0; index <= 100; index ++)`
will search from index 0 up to an including index 100, for 101 items. Just use the strictly less than comparison ( < 100 ).

Is the problem supposed to have several 100 element arrays, or a 200 element array you treat as two sections? If the first, then you only need the one loop, call the fucntion many times for each different array. If the second, then your loop control is again off a bit, it's going from the 102nd element up to the 200th, for a total of 99 items.

A normal pattern for any min/max search routine is to set your initial min/max to the first element, then compare from the second element on. That way you are sure to have a valid value as the initial min/max.

Rather than structure your function with pointer parameters, use reference parameters. Something like:

``````void find_smallest( int d[], int size, int &index, int &small)
{
//loop stuff
//test for smaller value
index = i;
small = d[i];
}

//usage in main
int small_index;
int small_value;
int array_of_stuff = { 6, 3, 7, 9, 7, 5, 7, 6, 0, 6 };

find_smallest( array_of_stuff, 10, small_index, small_value );
cout << small_index << " " << small_value;
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.