Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Vector problem - find max, min and positions (http://www.programmingforums.org/showthread.php?t=8933)

codylee270 Mar 18th, 2006 10:30 PM

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>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
void extrema(vector<float>); 

int main()
{
   
    int i, max;
    float nums[1000];
   
    cout << "How many numbers would you like to enter?\n";
    cin >> max;
    cout << "Enter each number. Press enter after each entry.\n";
   
    for(i=0; i<max; ++i)
    {
            cin >> nums[i];
    }
   
    vector<float> vnums(nums, nums+max);
    extrema(vnums);
   
   
    system("PAUSE");
    return 0;

}
void extrema(vector<float> nums)
{
    int offset_min, offset_max,i;
    float min, max;
   
    // Create copy of original nums vector
    vector<float> copy_nums(nums);
   
    cout << "You entered: ";
    for(i=0; i != '\0'; ++i)
    {
          cout << nums[i] << " " ;
    }
   
    sort(nums.begin(),nums.end());
    min = nums.front();
    max = nums.back();
   
    //offset_min=find(copy_nums[0], copy_nums[i-1], min)+1;
    //offset_max=find(copy_nums[0], copy_nums[i-1], max)+1;
   
    cout << "\nThe max value you entered is " << max;
    cout << max << "is #" << offset_max <<" in the list" << endl;
    cout << "\nThe min value you entered is " << min;
    cout << max << "is #" << offset_min <<" in the list" << endl;
}


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

DaWei Mar 18th, 2006 11:06 PM

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>
#include <string>
#include <vector>

using namespace std;

int uhOh (string TroubleInRiverCity)
{
    cerr << TroubleInRiverCity << endl;
    return 1;
}
int main (int argc, char *argv [])
{
    vector <double> theMysteries;
    double holdingVar;

    cout << "Enter a few doubles to reverse.  Terminate with ctrl-Z on a new line." << endl;
    cout << "Go: ";
    while (1)
    {
        cin >> holdingVar;
        if (!cin.good ()) break;
        theMysteries.push_back (holdingVar);
    }
    if (!cin.eof ()) return uhOh ("Input failed");
    size_t length = theMysteries.size ();
    double min = theMysteries [0];
    double max = theMysteries [0];
    size_t minPos = 0;
    size_t maxPos = 0;
    for (size_t i = 1; i < length; i++)
    {
        if (theMysteries [i] < min)
        {
            min = theMysteries [i];
            minPos = i;
        }
        if (theMysteries [i] > max)
        {
            max = theMysteries [i];
            maxPos = i;
        }
    }

    cout << "The Mysteries: ";
    for (size_t i = 0; i < length; i++)
        cout << theMysteries [i] << " ";
    cout << endl;

    cout << "The minimum is " << min << ", located in position " << minPos << endl;
    cout << "The maximum is " << max << ", located in position " << maxPos << endl;
    return 0;
}

Quote:

Originally Posted by Output
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\David>cd desktop

C:\Documents and Settings\David\Desktop>minmax
Enter a few doubles to reverse. Terminate with ctrl-Z on a new line.
Go: 10.3 2007.925 -127.463 104.721 22 -4
^Z
The Mysteries: 10.3 2007.93 -127.463 104.721 22 -4
The minimum is -127.463, located in position 2
The maximum is 2007.93, located in position 1

C:\Documents and Settings\David\Desktop>


codylee270 Mar 18th, 2006 11:19 PM

makes sense. I was just trying to get a feel for some of the algorithm stuff since i'll probably be tested over it.

DaWei Mar 18th, 2006 11:38 PM

Ah, well, yeah. Good idea. Practicing unfamiliar stuff trumps design approach sometimes. I'll look further.

The Dark Mar 18th, 2006 11:57 PM

In case you didn't see it in Dawei's example, your for loop
:

    for(i=0; i != '\0'; ++i)
wont loop at all, since 0 and '\0' and the same in this case.
You can use
:

    for(i=0; i < nums.size(); ++i)

grumpy Mar 19th, 2006 12:23 AM

You may wish to look up the functions min_element() and max_element() (in the <algorithm> header of the C++ standard library).

codylee270 Mar 19th, 2006 12:46 AM

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: ";
    while(nums[i] != '\0')
    {
          cout << nums[i] << " " ;
          i++;
    }


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...

DaWei Mar 19th, 2006 12:40 PM

Here's an example using Grumpy's suggestion, plus FIND.
:

#include <iostream>
#include <ctime>
#include <cstdio>
#include <climits>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int uhOh (string TroubleInRiverCity)
{
    cerr << TroubleInRiverCity << endl;
    return 1;
}
int main (int argc, char *argv [])
{
    vector <double> theStuff;
    vector <double>::iterator stuffIter;
    double adj = UINT_MAX / 100;
    double min;
    size_t i;

    srand ((unsigned) time (NULL));

    // Fill a vector with random doubles
    for (int i = 0; i < 10; i++)
        theStuff.push_back ((double) rand () / (double) UINT_MAX * adj);

    cout << "The list:\n";
    for (stuffIter = theStuff.begin (); stuffIter != theStuff.end (); stuffIter++)
        cout << *stuffIter << endl;
    cout << endl;

    // Find the minimum
    cout << "The minimum:\n";
    stuffIter = min_element (theStuff.begin (), theStuff.end ());
    min = *stuffIter;
    cout << min << endl;

    // Find the position for ALL occurrences
    for (i = 0, stuffIter = theStuff.begin (); stuffIter != theStuff.end (); stuffIter++, i++)
        if (*stuffIter == min) cout << "The position: " << i << endl;
   
    return 0;
}

Quote:

Originally Posted by Output
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\David>cd desktop

C:\Documents and Settings\David\Desktop>example
The list:
195.59
184.06
248.97
222.35
167.67
117.51
306.27
291.25
124.94
43.76

The minimum:
43.76
The position: 9

C:\Documents and Settings\David\Desktop>example
The list:
196.05
50.44
128.51
314.42
300.99
11.89
86.66
209.11
142.81
166.07

The minimum:
11.89
The position: 5

C:\Documents and Settings\David\Desktop>


codylee270 Mar 19th, 2006 1:35 PM

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)
    {
              if(copy_nums[i] == min)
              {
                              offset_min = i+1;
              }
    }
   
    for(i=0; i<copy_nums.size(); ++i)
    {
              if(copy_nums[i] == max)
              {
                              offset_max = i + 1;
              }
    }
                             
    cout << "\n\nThe min value you entered is " << min << "." << endl;
    cout << min << " is #" << offset_min <<" in the list.\n" << endl;
    cout << "\nThe max value you entered is " << max << "." << endl;
    cout << max << " is #" << offset_max<<" in the list.\n" << endl;
}


Dawei, what exactly is CERR? I haven't seen that yet. Could you elaborate please?
thanks very much

Ooble Mar 19th, 2006 1:57 PM

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