1

I was wondering how I could finish up this program. It's to perform a linear search on a list "ll" (which length is 31) for the user inputted item it, returning the user inputted numbers and their locations if they're found.

Problem: I'm not sure how to call the functions in this specific scenario, I don't really need to use pointers or pass a value, so the lack of these actually makes it more confusing for me, as those are fairly common scenarios.

#include <iostream> //enables usage of cin and cout
#include <cstring>

using namespace std;

int search (int i, int it, int ll, int z);
int printit (int i, int it, int ll, int z);

int main ()
{
    int i,it,z;
    int ll[] = {2,3,4,5,6,2,3,44,5,3,5,3,4,7,8,99,6,5,7,56,5,66,44,34,23,11,32,54,664,432,111}; //array hardwired with numbers
    //call to search
    return 0;
}

int search (int i, int it, int ll, int z)
{
    cout << "Enter the item you want to find: "; //user query
    cin >> it; //"scan"
    for(i=0;i<31;i++) //search
    {
        if(it==ll[i]) 
        {
        //call to printit
        }
    }
    return 0;
}

int printit (int i, int it, int ll, int z)
{
    cout << "Item is found at location " << i+1 << endl;
    return 0;
}
csheroe
  • 87
  • 3
  • 11
  • How is `search` supposed to know what's in `ll` unless you tell it somehow? Also, `ll` is a terrible variable name - avoid using `l`, `O`, and `I`. – Barry Feb 09 '15 at 02:45

2 Answers2

1

There is a problem with each of the parameters to search:

  • i's passed value gets overwritten before it gets used, and thus should be a local variable
  • Same thing for it
  • ll should be an array of ints
  • z isn't used at all

Things are even worse for printit: 3 of the 4 parameters are ignored.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

Search and print don't need to return int, if you have already print out the results. Also some declared variables are useless. The following code would work:

#include <iostream> //enables usage of cin and cout
#include <cstring>

using namespace std;

void search (int ll[]);
void printit (int n);

int main ()
{
//    int i,it,z;
    int ll[] = {2,3,4,5,6,2,3,44,5,3,5,3,4,7,8,99,6,5,7,56,5,66,44,34,23,11,32,54,664,432,111}; //array hardwired with numbers
    //call to search

    search(ll);
    return 0;
}

void search (int ll[])
{
    cout << "Enter the item you want to find: "; //user query
    cin >> it; //"scan"
    for(i=0;i<31;i++) //search
    {
        if(it==ll[i])
        {
            //call to printit
            printit(i);
        }
    }
//    return 0;
}

void printit (int n)
{
    cout << "Item is found at location " << n+1 << endl;
//    return 0;
}
Zhenyi Luo
  • 61
  • 5