Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,062 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,583 people online right now. Registration is fast and FREE... Join Now!




Sequential Search help

 
Reply to this topicStart new topic

Sequential Search help, need a little advice

Evilsean
9 Apr, 2008 - 07:32 AM
Post #1

New D.I.C Head
*

Joined: 9 Apr, 2008
Posts: 3

i need a little help with my sequential search, heres what I got for my search method.
CODE
void InventoryData::seqSearch()
{
    int index =0;
    int found = -1;

    if (index < count)
    {
        if (num[index] == target)
        {
            found = index;
            cout << "Sequential Search found part #:"<< num[index]<< " at a price of $"<< price[index]<<".\n";
            index = count;
        }
        else ++index;
    }
    for(int index = 0; index < count-1; index++)
    {
        if(num[index] == target)
        {
            found = 1;
            num[index] = target;
        }
    }
    
    if(found = 1)
    {
        cout << "Sequential Search found part #:"<< num[index]<< " at a price of $"<< price[index]<<".\n";
    }
    else cout << "Sequential Search could not find that part #.\n";

}

User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Sequential Search Help
9 Apr, 2008 - 07:33 AM
Post #2

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 14,926



Thanked: 47 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
What is the desired output? What is it actually outputting?

What errors are you getting?
User is offlineProfile CardPM
+Quote Post

Evilsean
RE: Sequential Search Help
9 Apr, 2008 - 11:04 AM
Post #3

New D.I.C Head
*

Joined: 9 Apr, 2008
Posts: 3

I have loaded an array and have to use a sequential search and binary search to search for the inputted part number and return the part number and price from the array. It gives me the second item in the array no matter what number I input. No errors, just the wrong information. My problems usually tend to be something simple im overlooking so I thought I would post here to see if someone else can see what I am not seeing.
User is offlineProfile CardPM
+Quote Post

Evilsean
RE: Sequential Search Help
13 Apr, 2008 - 10:07 AM
Post #4

New D.I.C Head
*

Joined: 9 Apr, 2008
Posts: 3

I figured out the sequential search, now the binary search I made isn't working, the method keeps not finding the target passed in.
CODE
int InventoryData::binSearch(int target)
{
    int first = 0;
    int last = count-1;
    int found = 0;
    int mid =0;

    if (first <= last && found == 0)
    {
        mid = ((first + last)/2);

        if (num[mid] == target)
        {
            found = 1;
            cout << "Binary Search located part #"<< num[mid]<< " at a price of $"<< price[mid]<<".\n";
        }
        else if (num[mid] < target)
        {
            first = mid + 1;
        }
        else last = mid - 1;
    }

    
    if (found == 0)
    {
        cout << "Binary Search could not locate that part number.\n";
        mid = -1;
    }
    
    return found;


This post has been edited by Evilsean: 13 Apr, 2008 - 07:50 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:32PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month