I have a task where I need to find the minimum number in the array. and function must start with this :
void min (float table [], int size, float & value, int & index)
I do not know how to tackle this.help.
You will need a variable that stores the "minimum found so far" as you look through the array. Imagine you have an array with the numbers [7, 3, 10, 2].
Look at the first number (7), since it is the first number, make it the minimum so far.
Look at the second number (3), is it less than the minimum so far? Yes, so make it the minimum so far.
Look at the third number (10), is it less than the minimum so far? No.
Look at the fourth number (2), is it less than the minimum so far? Yes, so make it the minimum so far.
Now you've reached the end of the array and you have the minimum. You will also need to keep track of the index of the "minimum so far" as you follow the steps above. Now just translate this process into code.
i think the best way is you try yourself first and post it here, then the good people of this site will help you further. if you want solutions to your home assignments then you are better off going to a site where you can rentacoder.
as PP said, you need first to think of the algorithm, here's a rephrase
you have an array of values which you want to traverse
int table[size]
you need an index value to check each element in a
int index // 0..size-1, in C/C++ arrays start at index 0
so you need to go through the array looking for the smallest value
how do you know it is the smallest value? by always storing the smallest value encountered in a variable
before you start, you set smallestvalue to some very large value, then you check array element
loop index = 0 to size-1
if table[index] < smallestvalue then smallestvalue = table[index]
end loop
now you return the value by assigning the 'value' which was passed by reference and thus reflects modifications to it
Just one thing you should consider... if you set the smallest value to "some very large value" as the above poster suggested, your function may not work for all possible inputs. Imagine you set the smallest value to 1,000,000.. well if the array only has values over 1,000,000, your answer will end up incorrect. The best way to handle this is to treat the first value in the array as the smallest before the loop begin.
i was write a normal array with 10 values. but still don't know how can be size bigger than table? so if table is table[10] and size is 14...? So i must now insert (float& value,int& index) in function array to find the min value in table.
I'm assuming this problem was assigned to you, in which case I think you have a small misunderstanding about what the function is supposed to do. From the function definition you gave, it sounds like your function needs to take an array of values and return (via the parameters value and index) the minimum and the index of the minimum. The function that you wrote does not take a table of values, but rather assumes the table is empty and gets input from the user to fill the table. Unless the problem specifically tells you to get input for the table entries inside the function, you should already have the table filled with values once it is passed to the function. Essentially the loop you wrote to get input for the table should be done in your main, not in the function that finds the minimum.
Also, as for your question about array size limitations, there is no easy way to deal with this without using a different data structure than a normal array. Using a vector or some kind of linked list would work best for storing dynamic amounts of data. Because you are dealing with arrays though, you will just have to allocate a lot of space for the table and hope the size the user wants doesn't exceed it.
Yes i make size=5, cuz i don't like to enter 100× but array is still 100 if i understand right.? i have 100 indices but using just 5 of them, but value in other indices (95) is 0.
I'm confused to with that val. but it's work and i don't know whay if value is unknown..?
It's starting value is really low because you don't initialize it to anything...therefore you will never get anything lower then it. Inside of your min function you need to set it to be equal to the first element of the array.
void min(float table[], int size, float &value, int &index)
{
value = table[0]; // initialize
for(int i=1;i<size;i++)
{
if(value < table[i]) // should be '<' not '>' since its min
{
value=table[i];
index=i;
}
}
}