Nick
Nick

Reputation: 213

Program checking to see if a users number is stored in the array in c++

I wrote this code to check to see whether or not a positive integer the user enters is stored in the array of the program, and then displays the result to the user. I am very new to C++ and am having some difficulties. Can someone please help me.

#include <iostream> //needed for access to cin and cout

using namespace std;

int main (void)
{
    int LS (int [], int);

    int ll=10;
    int l[ll]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
    int i=0;
    int it=0;
    int result=0;

    cout << " Welcome User, this program allows you to enter a number"<<endl
         << " and see if it is stored in our database."<<endl;

    cout << " Please enter number :  ";
    cin >> it;

    result = LS (l[ll], it); 

    if (result==0)
    {
        cout << " The number" << it << " you entered was not found in our database. " << endl;
    }

    if (result==1)
    {
        cout << " The number" << it << " you entered has been found in our database. " << endl;
    }

    return 0;
} 



int LS (int l[int ll], int it) //function to search for "it"
{

    int i=0, f=0; 

    if (l[i]==it)
    {
        f=1; 
        i++;
    }

    if (i==ll)
    {
        return f;
    }
}

Upvotes: 2

Views: 1423

Answers (5)

jpo38
jpo38

Reputation: 21514

Many changes to have a better code:

  • Use boolean to return true/false statements
  • Use more explicit function/variable names
  • Do your for loop correctly!
  • ...

    #define ARRAY_SIZE 10
    bool findItem( int item, int* items, size_t count )
    {
        for ( size_t i = 0; i != count; ++i )
        {
            if ( items[i] == item )
                return true;
        }
        return false;
    }
    
    int main (void)
    {
        int l[ARRAY_SIZE] = {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
    
        cout << " Welcome User, this program allows you to enter a number"<<endl<< " and see if it is stored in our database."<<endl;
    
        cout << " Please enter number :  ";
        int toFind;
        cin >> toFind;
    
        if ( !findItem( toFind, l, ARRAY_SIZE ) )
        {
            cout << " The number" << toFind << " you entered was not found in our database. " << endl;
        }
        else
        {
            cout << " The number" << toFind << " you entered has been found in our database. " << endl;
        }
    
        return 0;
    }
    

Upvotes: 1

Shankar
Shankar

Reputation: 417

#include <iostream> //needed for access to cin and cout

using namespace std;

int main (void)
{
int LS (int [], int);
int ll=10;
int l[ll]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
int i=0;
int it=0;
int result=0;

cout << " Welcome User, this program allows you to enter a number"<<endl
  << " and see if it is stored in our database."<<endl;

cout << " Please enter number :  ";
cin >> it;

result = LS (l[ll], it); 
if (result==0){
 cout << " The number" << it << " you entered was not found in our database. " << endl;
}

if (result==1)
{
 cout << " The number" << it << " you entered has been found in our database. " << endl;
}

 return 0;
} 



int LS (int l[int ll], int it) //function to search for "it"{
  int i=0; 
  while (l[i]!=it and i<11){
     i++;
  }
if (i==ll)
   return 0;
return 1;
}

This code works because if an element is found then while loop will break and hence i value will be less than 11

Upvotes: 2

Christian Hackl
Christian Hackl

Reputation: 27528

First of all, you should know that you extremely complicate your life. Just use std::find, which works perfectly fine with arrays, and be done with it! Here's how you would write the program using normal standard C++11:

#include <iostream> // for std::cout and std::cin
#include <algorithm> // for std::find
#include <iterator> // for std::begin and std::end

int main()
{
    int const numbers[] = {30, 2, 13, 9, 25, 17, 89, 32, 73, 69 };

    std::cout << " Welcome User, this program allows you to enter a number\n"
                 " and see if it is stored in our database.\n";

    std::cout << " Please enter number :  ";
    int input;
    std::cin >> input;

    if (std::cin)
    {
        if (std::find(std::begin(numbers), std::end(numbers), input) == std::end(numbers))
        {
            std::cout << " The number " << input << " you entered was not found in our database.\n";
        }
        else
        {
            std::cout << " The number " << input << " you entered has been found in our database.\n";
        }
    }
    else
    {
        // invalid input...
    }
}

(In fact, the only C++11-specific feature used here is std::begin / std::end.)


Yet you are probably interested in knowing what went wrong with your original code.

The first problem with it is that it usually won't even compile.

 int ll=10;
 int l[ll]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};

This is using a GCC-specific compiler extension. In portable standard C++, the array size must be known at compile time, and ll is only known at run time.

Fortunately, you don't even have to specify the size anyway. This is one of the few good things about raw arrays in C++:

 int l[] = {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};

The next problem:

 int LS (int l[int ll], int it) //function to search for "it"

Proprietary compiler extensions aside, this should probably be:

 int LS (int l[], int size, int it)

When you pass an array to a function like this, any size information is lost, so an extra size parameter is needed. Calling the function then looks like this:

result = LS (l, sizeof(l) / sizeof(int), it); 

And finally, in your LS function, you are not iterating over your array but just compare one single element. Here is a possible modification of your original code:

int LS (int l[], int size, int it)
{
  int i=0, f=0; 

  while (i < size)
  {
    if (l[i]==it)
    {
      f=1; 
    }
    i++;
  }

  return f;
}

It still has quite a few problems, but it's a start.


Moral of the story:

  1. Use functions from <algorithm>. They also work with arrays.
  2. Use std::vector and std::array whenever you can. You should almost never see raw arrays in modern C++ code.
  3. One of the few good features of raw arrays is the fact that the compiler counts the elements automatically at the point of declaration.

Upvotes: 1

kadina
kadina

Reputation: 5376

I found some issues here.

1) We need to provide the function declarations outside of all the functions. So int LS(int [], int) should be outside of main. Function decleration is suggesting the C++ compiler that function LS will take 2 arguments a) an integer array b) one integer

2) Function can not determine number of elements in the array automatically. So we need to send the number of elements in the array to function LS. So function declaration should be changed as below.

int LS (int [], int, int);

2nd parameter is number of elements in the array.

3) Only 10 elements are placed inside the array. So no need to keep the array size as 11 in main()

 int l[ll]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};

4) Finally your function is missing the logic of looping through the array.

So Find below program which is modified version of yours.

#include <iostream> //needed for access to cin and cout

using namespace std;

int LS(int [], int, int);

int main (void)
{
    int ll=10;
    int l[l0]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
    int i=0;
    int it=0;
    int result=0;

    cout << " Welcome User, this program allows you to enter a number"<<endl
    << " and see if it is stored in our database."<<endl;

    cout << " Please enter number :  ";
    cin >> it;

    result = LS (l, 10, it); 

    if (result==0)
    {
        cout << " The number" << it << " you entered was not found in our database. " << endl;
    }

    if (result==1)
    {
        cout << " The number" << it << " you entered has been found in our database. " << endl;
    }

    return 0;
} 

int LS(int L[], int n, int it)
{
    for(int i = 0; i < n; i++)
    {
        if (l[i]==it)
        {
            return 1;
        }
    }

    return 0;
}

Upvotes: 1

Abhi
Abhi

Reputation: 744

You are missing a loop to iterate through all the values in the array

#include <iostream> //needed for access to cin and cout

using namespace std;

int main (void)
{
 int LS (int [], int);

 int ll=10;
 int l[ll]= {30, 2, 13, 9, 25, 17, 89, 32, 73, 69};
 int i=0;
 int it=0;
 int result=0;

 cout << " Welcome User, this program allows you to enter a number"<<endl<< " and see if it is stored in our database."<<endl;

 cout << " Please enter number :  ";
 cin >> it;

 result = LS (l, it); 

 if (result==0)
 {
   cout << " The number" << it << " you entered was not found in our database. " << endl;
 }

 if (result==1)
 {
  cout << " The number" << it << " you entered has been found in our database. " << endl;
 }

  return 0;
} 



int LS (int l[10], int it) //function to search for "it"
{

 int i=0, f=0; 
 for(i=0;i<10;++i) {
 if (l[i]==it)
 {
   f=1;
   return f;
 }
  return f;
 }
}

Upvotes: 1

Related Questions