Linear search

Discussion in 'C++' started by littlegirl, Apr 5, 2006.

  1. littlegirl

    littlegirl Guest

    hi guys can some one help me here
    i have to accept a number
    and preform a linear search for the number

    and if its not one of the number it has to say its invali

    #include <iostream
    using namespace std
    int searchlist (int[],int,int)
    int main(

    const int Num_Account = 18
    char Account[Num_Account]=
    {"5658845, 4520125", "7895122, 8777541"
    "8451277, 1302850"
    "8080152, 4562555", "5552012, 5050552"
    "7825877, 1250255"
    "1005231, 6545231", "3852085, 7576651"
    "7881200, 4581002" }

    cout << "enter Number\n"
    cin >> num

    int searchlist(int list [],int num,int value

    int index = 0
    int position = -1
    bool found = false

    while (index < num &&!found

    if (list == value

    found = true
    position = index

    index++


    return 0
    littlegirl, Apr 5, 2006
    #1
    1. Advertising

  2. * littlegirl:
    > hi guys can some one help me here
    > i have to accept a number
    > and preform a linear search for the numbers
    >
    > and if its not one of the number it has to say its invalid
    >
    >
    > #include <iostream>
    > using namespace std;
    > int searchlist (int[],int,int);
    > int main()
    > {
    > const int Num_Account = 18;
    > char Account[Num_Account]=
    > {"5658845, 4520125", "7895122, 8777541",
    > "8451277, 1302850",
    > "8080152, 4562555", "5552012, 5050552",
    > "7825877, 1250255",
    > "1005231, 6545231", "3852085, 7576651",
    > "7881200, 4581002" };
    >
    > cout << "enter Number\n";
    > cin >> num;
    >
    > int searchlist(int list [],int num,int value)
    > {
    > int index = 0;
    > int position = -1;
    > bool found = false;
    >
    > while (index < num &&!found)
    > {
    > if (list == value)
    > {
    > found = true;
    > position = index;
    > }
    > index++;
    > }
    >
    > return 0;
    > }


    Try first of all to move the definition of the 'searchlist' function to
    /above/ the 'main' function: C++ does not support direct nesting of
    function definitions.

    When you've moved the definition, remove the declaration of 'searchlist'
    that you now have after your 'using' directive, and in general, avoid
    declaring functions that you define later on.

    In 'main' you need to call 'searchlist', and present the result of that
    call.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Apr 5, 2006
    #2
    1. Advertising

  3. littlegirl

    osmium Guest

    "Alf P. Steinbach" writes:

    <one added point>

    >> i have to accept a number and preform a linear search for the numbers
    >>
    >> and if its not one of the number it has to say its invalid
    >>
    >>
    >> #include <iostream>
    >> using namespace std;
    >> int searchlist (int[],int,int);
    >> int main()
    >> {
    >> const int Num_Account = 18;
    >> char Account[Num_Account]= {"5658845, 4520125", "7895122, 8777541",
    >> "8451277, 1302850",
    >> "8080152, 4562555", "5552012, 5050552",
    >> "7825877, 1250255",
    >> "1005231, 6545231", "3852085, 7576651",
    >> "7881200, 4581002" };
    >>
    >> cout << "enter Number\n";
    >> cin >> num;
    >>
    >> int searchlist(int list [],int num,int value)
    >> {
    >> int index = 0;
    >> int position = -1;
    >> bool found = false;
    >>
    >> while (index < num &&!found)
    >> {
    >> if (list == value)
    >> {
    >> found = true;
    >> position = index;
    >> }
    >> index++;
    >> }


    You promised to return an int. Remember? There is no way for the caller to
    determine what happened.

    An int works fine, but the more modern way is to return a bool, it has
    better self-documenting properties than an int.

    >> return 0;
    >> }

    >
    > Try first of all to move the definition of the 'searchlist' function to
    > /above/ the 'main' function: C++ does not support direct nesting of
    > function definitions.
    >
    > When you've moved the definition, remove the declaration of 'searchlist'
    > that you now have after your 'using' directive, and in general, avoid
    > declaring functions that you define later on.
    >
    > In 'main' you need to call 'searchlist', and present the result of that
    > call.
    osmium, Apr 5, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    1,096
    Roedy Green
    Nov 15, 2005
  2. cyprian
    Replies:
    1
    Views:
    279
    Tom Hawtin
    May 28, 2007
  3. sanborne
    Replies:
    5
    Views:
    1,991
  4. Angus
    Replies:
    10
    Views:
    457
    James Kanze
    Mar 28, 2011
  5. Abby Lee
    Replies:
    5
    Views:
    378
    Abby Lee
    Aug 2, 2004
Loading...

Share This Page