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
Very helpful guys.Thanks.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

void array(float table[], int size)
{
     int i;
     size=10;
     for (i=0;i<size;i++)
     {
         cout<<"value "<<i+1<< ".index = ";
         cin>>table[i];
         }
         }
int main()
{
    float table[10];
    int size;
    array(table,size);
    system("pause");
    return 0;
}
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.
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
34
35
36
37
38
39
#include <iostream>
using namespace std;

int size=5;

void enter(float table[],int size)
{
	cout<<"enter numbers"<<endl;
	for(int i=0;i<size;i++)
    {
              
              cin>>table[i];
     }
}
void min(float table[], int size, float &value, int &index)
{
	
	for(int i=0;i<size;i++)
    {
		if(value>table[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<<" "<<ind<<endl;
      system("pause");
      return 0;
}


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
1
2
 for(int i=0;i<size;i++) 
 for(int i=0;i<=size;i++) 

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

please help me i m confused
Last edited on
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..?
nvm then i m confused to...
i m a newb
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
      }
   }
}
Topic archived. No new replies allowed.