Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 18th, 2006, 9:30 PM   #1
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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
codylee270 is offline   Reply With Quote
Old Mar 18th, 2006, 10:06 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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>
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers

Last edited by DaWei; Mar 18th, 2006 at 10:36 PM.
DaWei is offline   Reply With Quote
Old Mar 18th, 2006, 10:19 PM   #3
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
makes sense. I was just trying to get a feel for some of the algorithm stuff since i'll probably be tested over it.
codylee270 is offline   Reply With Quote
Old Mar 18th, 2006, 10:38 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Ah, well, yeah. Good idea. Practicing unfamiliar stuff trumps design approach sometimes. I'll look further.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 18th, 2006, 10:57 PM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 810
Rep Power: 4 The Dark is on a distinguished road
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)
The Dark is offline   Reply With Quote
Old Mar 18th, 2006, 11:23 PM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
You may wish to look up the functions min_element() and max_element() (in the <algorithm> header of the C++ standard library).
grumpy is offline   Reply With Quote
Old Mar 18th, 2006, 11:46 PM   #7
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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...
codylee270 is offline   Reply With Quote
Old Mar 19th, 2006, 11:40 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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>
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 19th, 2006, 12:35 PM   #9
codylee270
Unverified User
 
Join Date: Sep 2005
Posts: 209
Rep Power: 0 codylee270 is an unknown quantity at this point
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
codylee270 is offline   Reply With Quote
Old Mar 19th, 2006, 12:57 PM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
It's another output stream, similar to cout - its main use is for printing errors.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC