Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++ Question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Sudie   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 August 2001 - 07:51 AM

Is there a function that determines the lowest element (meaning that which has the lowest value) of a group of integers (which I guess would be in an array). I''ve tried using min_element from the algorithm.h, but I didn''t have much success with that. If so, an example would be nice too. Thanks for your help in advance.

Sponsor:

#2 Null and Void   Moderators   -  Reputation: 1087

Like
Likes
Like

Posted 08 August 2001 - 08:01 AM

That''s pretty east to write:
  
int lowest_value(int *array, unsigned int max) {
int lowest = *array;

while( --max > 0 ) {
array++;
if(*array < lowest) lowest = *array;
}

return lowest;
}

unsigned int lowest_index(int *array, unsigned int max) {
unsigned int lowest = 0, at;

for(at = 1; at < max; at++) {
if(array[at] < array[lowest]) lowest = at;
}

return lowest;
}


[Resist Windows XP''s Invasive Production Activation Technology!]

#3 Gorg   Members   -  Reputation: 248

Like
Likes
Like

Posted 08 August 2001 - 09:49 AM

min_element should do just fine : What you have to understand, it that it uses iterator and returns an iterator. The nice thing is pointers are iterator.

Here is how to use min_element :

  

const int SIZE = 4;

int list[SIZE];

//put values in list;


int* min = std::min_element( list, list+SIZE);
//min now points to the smallest element


int minVal = *min;
int Index = min-list; //index of element





#4 Sudie   Members   -  Reputation: 122

Like
Likes
Like

Posted 08 August 2001 - 10:34 AM

How would you do it if it was based on user input until the user inputs a 0 or a negative number?

Edited by - Sudie on August 8, 2001 5:39:52 PM

#5 Kylotan   Moderators   -  Reputation: 3338

Like
Likes
Like

Posted 08 August 2001 - 04:30 PM

The only difference would be that ''size'' would be however many elements you read in, not the size of the array. (Be sure to ensure that you don''t read in too many elements for the array, though.)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS