#include <iostream>
#include <cstdlib>
using namespace std;

const int arrSize = 100;  
bool isPresent(int ,int );
     
int main()
{    
    int myArr[arrSize];
    int input;
    
    for (int i=0; i<=arrSize; i++)
        myArr[i]=rand()%200;      //creating array with random numbers
    
    
        
    //sorting random numbers
    for (int j=0; j<=arrSize; j++){
        for(int k=0; k<j ; k++){        
            if (myArr[j] < myArr[k]){
               
               myArr[k] = myArr[j] + myArr[k];                       
               myArr[j] = myArr[k] - myArr[j];
               myArr[k] = myArr[k] - myArr[j];
            }
        }
    }
    
    char quit;
    
    while ( (quit!='N') || (quit!='n') ) 
    {
    
    cout<<"\n Enter number to check in the array: \n";
    cin>>input;
    
    bool j= isPresent( myArr[arrSize], input );
    
    if ( j )
    cout<<"\n End "<<endl; 
    
    cout<<" Do you want to check for another number?\n";
    cin>>quit;
    
    }
    

    
    system("PAUSE");
    return 0;

}

















bool isPresent(int theArr[arrSize], int n)
{
     float x=arrSize;
         
         
     for (int j=0; j<=arrSize; j++)
     {
         
         
[B][I]        if ( (x%2)== 1 );
         {x=x+1;}
         
         if (n==theArr[x/2])
         { cout<<"Element found at location "<<x/2<<endl;
         return 0;}
         
         else;{
         if (n<=theArr[x/2])
         x=x/2;
         
         if(n>=theArr[x/2])
         x=1.75*x;          }[/I][/B]
     }
  cout<<"\n Not found"<<endl;
  return 1;
     
}

// code is the intellectual property of 09030032 [at] lums.edu.pk

why are the bold/italisized "ifs" giving me an error when compiling?

One, you have some semicolons directly after your if statment and your else statement. They may not be errors, but you don't want them. Two, x is a float, so the % operator doesn't make much sense if you use it with a float. Are you sure you want x to be a float?

Additionally, your search function looks to be sort of a Binary Search - but rather odd in its implementation. I suggest you look up binary search algorithm, I think it might be hard to prove correctness of your method.

Also, you return 0 upon success and 1 upon failure - that's kind of backwards from the general understanding as when you return them to the bool variable, 0 is false and 1 true.

Ok ive made the modifications but still theres an unknown error.

Whats messed up now?

#include <iostream>
#include <cstdlib>
using namespace std;

const int arrSize = 100;  
bool isPresent(int theArr[arrSize], int n);
     
int main()
{    
    int myArr[arrSize];
    int input=0;
    
    for (int i=0; i<=arrSize; i++)
        myArr[i]=rand()%200;      //creating array with random numbers
    
    
        
    //sorting random numbers
    for (int j=0; j<=arrSize; j++){
        for(int k=0; k<j ; k++){        
            if (myArr[j] < myArr[k]){
               
               myArr[k] = myArr[j] + myArr[k];                       
               myArr[j] = myArr[k] - myArr[j];
               myArr[k] = myArr[k] - myArr[j];
            }
        }
    }
    
    char quit;
    
    while ( (quit!='N') || (quit!='n') ) 
    {
    
    cout<<"\n Enter number to check in the array: \n";
    cin>>input;
    
    bool j= isPresent( myArr[arrSize], input );
    
    if ( j )
    cout<<"\n End "<<endl; 
    
    cout<<" Do you want to check for another number?\n";
    cin>>quit;
    
    }
    

    
    system("PAUSE");
    return 0;

}



bool isPresent(int theArr[arrSize], int n)
{
     int x=arrSize;
                  
     for (int j=0; j<=arrSize; j++)
     {
         
         if (n==theArr[x/2])
         { cout<<"Element found at location "<<x/2<<endl;
         return 1;}
         
         if (n<=theArr[x/2])
         x=x/2;
         
         if(n>=theArr[x/2])
         x=x-(x/4);
                 
     }
  cout<<"\n Not found"<<endl;
  return 0;
     
}

Line38 - Your function wants an array, not an array element.

Incorrect

bool j= isPresent( myArr[arrSize], input )[B];[/B]

Correct - delete red part

bool j= isPresent( myArr, input )[B];[/B]

Thanks!!

finally the program runs.

Now the thing is that the function only confirms values stored at locations 0 and 1
:/

if the value exists beyond location 0 and 1 it displays not found

#include <iostream>
#include <cstdlib>
using namespace std;

const int arrSize = 100;  
bool isPresent(int theArr[arrSize], int n);
     
int main()
{    
    int myArr[arrSize];
    int input;
    char quit;         // input inquiry for quit or continue
    
    for (int i=0; i<=arrSize; i++)
        myArr[i]=rand()%2000;      //creating array with random numbers
    
    
        
    //sorting random numbers
    for (int j=0; j<=arrSize; j++){
        for(int k=0; k<j ; k++){        
            if (myArr[j] < myArr[k]){
               
               myArr[k] = myArr[j] + myArr[k];                       
               myArr[j] = myArr[k] - myArr[j];
               myArr[k] = myArr[k] - myArr[j];
            }
        }
    }
    
    //display array
    for (int i=0; i<=arrSize; i++)
        cout<<myArr[i]<<" ";
    
    
       
    while (1)
    {
    
    cout<<"\n Enter number to check in the array: \n";
    cin>>input;
    
    isPresent( myArr, input );
    
   
    
    cout<<" Do you want to check for another number?\n";
    cin>>quit;
    if ( quit=='n' || quit =='N')
    break;
    
    } 
    

    
    system("PAUSE");
    return 0;

}


// function code 


bool isPresent(int theArr[arrSize], int n)
{
     int x=arrSize;
         
         
     for (int j=0; j!=arrSize; j++)
     {
         if ( (x%2)==1 )
         x=x+1;
         
         if (n==theArr[x/2])
         { cout<<"Element found at location "<<x/2<<endl;
         return 1;}
         
         else;
         {
              if (n<=theArr[x/2])
              x=x/2;
         
              if(n>=theArr[x/2])
              x=x-(x/4);
         }        
     }
  cout<<"\n Not found"<<endl;
  return 0;
     
}

Read up on "Binary Search". Your algorithm is flawed. Say you have 100 numbers, ordered, in an array. At some point, you'll have narrowed the range down in that array, in that if the number exists inside of the array, it will have an index in the range of, say, 63 to 75. Take the midpoint, index 69. Say the number you're looking for is 457 and array[69] is 400. You then narrow down further and say the index must be from 70 to 75. Take a midpoint, 73 and test it. array[73] = 500, so the range must now be 71 to 72. Test array[71]. Too small. Test array[72]. It's 457. You've found it.

For each test, you throw out half of your remaining indexes. Your function doesn't do this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.