Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Smallest array position

Status
Not open for further replies.

ScrewSpurrier

Technical User
Feb 10, 2003
7
0
0
US
I am trying to get cout the array value of the smallest value in the array. I have come up with the code below but I always get the awnser 0. I am not sure why as I think my postion should increment by one if smallest number is not found since it is in the loop. Any help would be greatly appreciated.

#include <conio.h>
#include <algorithm>
void main ()
{
const int size = 12;
double array [size];

for (int i = 0; i < size; i++)
{
cout<<&quot;Please enter each months sales amount for the year starting with January : &quot;;
cin>>array;
}
double minimum;
minimum = array[0];
int position = -1;

for (int j = 0; j < size; j++)
{
if(array [j] < minimum)
position = position + 1;

}
cout<<&quot;Minimum sales is in array pos: &quot;<<position;

getch();
}
 
you need to actually have them fill the array indexes, it should say:

for (int i = 0; i < size; i++)
{
cout<<&quot;Please enter each months sales amount for the year starting with January : &quot;;
cin>>array;
}
 
This question is popping up everywhere check these well known macros
Code:
// these can be made into functions to avoid
// the anti-macro people :-P
#define MAX(x,y) (x>y ? x:y)
#define MIN(x,y) (x<y ? x:y)

with an array in a loop you can do the following

// for an array of size one set to first element
int min = array[0];
int max = array[0];

for(int i=0;i+1<size;i++)
{
  min = MIN(array[i],array[i+1]);
  max = MAX(array[i],array[i+1]);
}

// min and max are properly set

Matt

 
For the index some modifications can be done in the for loop.
 
<offtopic>
The TGML is dropping the
Code:
[i]
on the array variable. TGML thinks that this is starting italics in the posting. You can turn off TGML in you posts by click off the Process TGML box in Step 2 just below your post or you can type [ c o d e ] (without the spaces) to turn off TGML on the fly and [ / c o d e ] (without the spaces) to turn it on again. Click the link on the Process TGML to see more info. James P. Cottingham

When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour. That's relativity.
[tab][tab]Albert Einstein explaining his Theory of Relativity to a group of journalists.
 
(1) Thanks James for explaining the italic thing. I did wonder why a posting of mine the other week suddenly burst into italics.

(2) I take it you want to return the position of the smallest value in the array. Actually your code is on the right lines, but won't quite do that. You have set &quot;minimum&quot; to the value of the first element of the array, and thereafter you never change minimum, so you aren't keeping track of what the smallest value in the array is. So, for instance, if the 2nd value in the array is less than the first, then thereafter the interesting question is not &quot;is the current value less than array[0], but is it less than array[1], where the current minimum is. Also, you only really need to set pos, the position of the current smallest value found, when you find a new smallest value!

A good way to adapt your code would be (pseudocode)
if array[now] < minimum then
(1) minimum = array[now]
(2) pos_of_minimum = now
Otherwise just carry on in loop without changing minimum or pos_of_minimum

You don't have to keep track of the minimum if you don't want, since if you are keeping track of the position of the minimum, then the minimum is always:
array[pos_of_minimum]

Hope that helps
 
so basically you'd want:

Code:
#include <conio.h>
#include <algorithm>
void main ()
{
   const int size = 12;
   double array [size];

       for (int i = 0; i < size; i++)
         {
          cout<<&quot;Please enter each months sales amount for the year starting with January : &quot;;
         cin>>array[i];
         }
    int pos=0
      for (int j = 0; j < size; j++)
         {
         if(array [j] < array[pos])
         pos=j
         }
          cout<<&quot;Minimum sales is in array pos: &quot;<<pos;

getch();
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top