![]() |
Vector problem - find max, min and positions
Hi everyone,
I'm having a problem with an exercise that requires the use of vectors to find the max and min and their positions in a list. Basically, a user inputs however many numbers which are saved into a vector array, and the array is sent to a function. My idea was to first make a copy of the vector, display the original, sort the original to find max and min, and use the find algorithm to find the index value of where the min and max are located in the original unsorted list. Well, i'm having a few problems. For one, i can't get the damn thing to print out the original vector passed, and from there i'm not really sure how to get the FIND algorithm to work correctly for my purposes. I've got the area of code with the FIND algorithm commented out b/c the program will not compile with it active, and i wanted to see in anyone can tell me what i'm doing wrong with it. Here's my code: :
#include <iostream>I'm sure i'm just doing something stupid, but it's giving me a headache, so i figured i'd let someone else tell me how stupid i am before it takes me hours to figure it out myself. Thanks a lot guys |
I haven't looked at your code (yet, anyway), but I wouldn't bother to sort. I'd traverse the vector and find the minimum and maximum and the positions would just fall out naturally, like worms from an apple.
EDIT: Something like this: :
#include <iostream>Quote:
|
makes sense. I was just trying to get a feel for some of the algorithm stuff since i'll probably be tested over it.
|
Ah, well, yeah. Good idea. Practicing unfamiliar stuff trumps design approach sometimes. I'll look further.
|
In case you didn't see it in Dawei's example, your for loop
:
for(i=0; i != '\0'; ++i)You can use :
for(i=0; i < nums.size(); ++i) |
You may wish to look up the functions min_element() and max_element() (in the <algorithm> header of the C++ standard library).
|
Ahhh.. the for loop... I knew it was causing a problem, but of course i overlooked the fact that i itself is never going to increment to '\0'...lol.....
I changed it to :
cout << "You entered: ";I totally forgot about .size(). I figured there was something for that but I wasn't sure. lol.. and i really wanted to do it without passing the actual length max given at the beginning. Now.. i just need to figure out FIND. Correct me if i'm wrong, but aren't you supposed to provide it with a preferred starting and ending point in the array and a value you want to search for. From what i understand i believe it's supposed to return the index of where that value is in the array. I wasn't sure on that so i took a stab at it and went ahead an added one to the result for FIND to give a location in a list. And i still don't know if that is correct or not, b/c again, FIND isn't working..lol... Thanks for all of you help guys... |
Here's an example using Grumpy's suggestion, plus FIND.
:
#include <iostream>Quote:
|
i read somewhere that FIND() was best used with strings. I dunno if that's true or not, but since it's not working with my float vector, i just went ahead with my normal approach.
:
for(i=0; i<copy_nums.size(); ++i)Dawei, what exactly is CERR? I haven't seen that yet. Could you elaborate please? thanks very much |
It's another output stream, similar to cout - its main use is for printing errors.
|
| All times are GMT -5. The time now is 9:31 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC