Sign in to follow this  
samurai_gator

C++ array compare elements?

Recommended Posts

I made a problem up and I'm having some trouble trying to find the lowest value element in an array. I know it's a logic error, but I've spent hours on it and I can't figure it out.
/* We have 10 CDs all at different prices. Using arrays and
   increment/decrement compare the prices one by one and
   find the lowest price CD

   9/12/09
*/
#include <iostream>
using namespace std;
int main()
{
    double CdPrices[10] = { 10.23, 9.89, 5.25, 22.50, 15.47, 12.00, 10.50, 14.99, 4, 21.99 };
    // collective prices of CDs
    //double presentPrice; // price of CD being inspected, unaffected by newprice
    //double newPrice; // new lowest price of CD
    int index = 0;

    //cout << "Prices of CDs in a list.\n";
    // displays a list of prices
    /*while ( index < 10 )
    { cout << CdPrices[index] << "\n" << endl;
            ++index;
    }
    */
    cout << "Let's find the cheapest CD!\n";
   while ( index < 10 )
   {
        double presentPrice = CdPrices[index++];
        double newPrice = CdPrices[++index];

            if ( presentPrice > newPrice )
            {
                  presentPrice = newPrice;
            }
            cout << "newPrice is: $ " << newPrice << "\n" << endl;
            cout << "presentPrice is: $ " << presentPrice << "\n" << endl;
            cout << " -------------------------------------------------\n";
    }
    return 0;
}


the output I get is: newPrice: $ 5.25 presentPrice: $ 5.25 newPrice: $ 15.47 presentPrice: $ 5.25 newPrice: $ 10.5 presentPrice: $ 10.5 newPrice: $ 4 presentPrice: $ 4 newPrice: $ NaN presentPrice: $ 4

Share this post


Link to post
Share on other sites
The problem:


index = 0;
while ( index < 10 )
{
// When index is 0 ..
double presentPrice = CdPrices[index++]; // presentPrice = CdPrices[0], index is now 1
double newPrice = CdPrices[++index]; // index is now 2, presentPrice = CdPrices[2]

// We seemed to have skipped checking CdPrices[1]!

// And when index is 9...
// double presentPrice = CdPrices[index++]; // presentPrice = CdPrices[9], index is now 10
// double newPrice = CdPrices[++index]; // index is now 11, presentPrices = CdPrices[11]; <-- Out of bounds!

if ( presentPrice > newPrice )
{
presentPrice = newPrice;
}

cout << "newPrice is: $ " << newPrice << "\n" << endl;
cout << "presentPrice is: $ " << presentPrice << "\n" << endl;
cout << " -------------------------------------------------\n";
}



Quick solution:
If you can, use the STL:


#include <algorithm>

double lowestPrice = *std::min_element( CdPrices, CdPrices + 10 );



If you can't, then:

double lowestPrice = CdPrices[0];
for ( int i = 1; i < 10; ++i )
if ( CdPrices[i] < lowestPrice )
lowestPrice = CdPrices[i];



EDIT:
Nobodynews explained much better in words than I could in code. ;)

Share this post


Link to post
Share on other sites
When you find that the presentPrice is greater than the newPrice you overwrite the value you just retrieved from CdPrices and stored in presentPrice. You are essentially incrementing the index twice per loop iteration and comparing CDs 0&1, 2&3, 4&5, etc against each other. Then at the end you compare CDs 9&10 against each other, but there IS no CD 10 so you got that NaN result. The important thing to take away here is that you don't need to increment index twice.

To figure out the correct method, imagine the prices are printed on a stack of index cards for the CDs and you are physically going through them to find the lowest price. What you'll probably do is take the first index card hang on to it. Then you'll look at the next index card and compare it to the card you're still hanging on to. You discard the higher value card and then look at the next card and compare it to the card you're hanging on to.

I'm not sure if that helped, but maybe it will get you back on track.

Share this post


Link to post
Share on other sites
Your algorithm, while appearing interesting, makes absolutely no sense to me.

Do this:
Buy a pack of index cards. Each index card is a variable with a place in memory
Write your array on a stack of the index cards. Then, DO what you are trying to accomplish with the cards. Every time you mentally "remember" something, write it down on a card. After you do that, write down on paper what you did to the cards, in ENGLISH.

Then, transcribe what you did from ENGLISH to C.

If you do this, you should see what happens and find the right algorithm to use.

Share this post


Link to post
Share on other sites
The problem is that you read two new prices every time through the loop. You do not want to change presentPrice every time - only when a "better" new price is found. However, this does require that you give presentPrice an appropriate initial value - if it starts out lower than any element in the array, you have a logic problem.

Proper logic for finding the minimum of a sequence is as follows:

1) Report an error if there are no elements in the sequence.
2) Initialize the "lowest so far" value with the first element from the sequence.
3) For each other element of the sequence, compare it to "lowest so far" and update if appropriate.

Or you could just use the standard library.

Share this post


Link to post
Share on other sites
Quote:
Original post by _fastcall
The problem:

*** Source Snippet Removed ***

Quick solution:
If you can, use the STL:

*** Source Snippet Removed ***

If you can't, then:
*** Source Snippet Removed ***

EDIT:
Nobodynews explained much better in words than I could in code. ;)


ha oh good oll stl.
but seriously dont use stl. least until you understand the logic that it uses.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this