You are using a version without Ads of this website. Please, consider donating:

array min value

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.
Do you have any code at all?
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

value = smallestvalue

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.
Last edited on
yep i agree that would be the better deal in a real solution, or maxint from limits.h but i think here the difficulty is on another level...
Last edited on
I'll try to write a code now and post it here, than we'll see...
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.

 12345678910111213141516171819202122 #include using namespace std; void array(float table[], int size) { int i; size=10; for (i=0;i
Last edited on
int size;
;i<size;
something is wrong in the above lines

you should store the minimum in a varible and then out put it outside the loop.
*hint: u need to deciede somehing(if statement)

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.
 123456789101112131415161718192021222324252627282930313233343536373839 #include using namespace std; int size=5; void enter(float table[],int size) { cout<<"enter numbers"<>table[i]; } } void min(float table[], int size, float &value, int &index) { for(int i=0;itable[i]) { value=table[i]; index=i; } } } int main() { float table[100]; float val; int ind; enter(table,size); min(table,size,val,ind); cout << val<<" "<

so now work.
Last edited on
you dont need the parameter size if you making it as an Global one and isnt the size of the array 100, you set size to 5. and dont you need to change
 12 for(int i=0;i

also the what about the value's starting val? what that starts out way to low