guys..I would like to ask for your help..I was given a programming assignment about searching a random number in a array of also random numbers. here's the problem:

write a program that creates an array of 100 random integers in the range from 1 to 200 and, then use a sequential search to search the array 100 times using randomly generated targets in the same range. at the end of the program, display the statistics of the number of searches it has completed, the number of successful searches, the percentage of successful searches and finally l the average number of test per search.


here is my code and I just would like you guys to look through it and give me some comments if there is something wrong with my code. thanks a lot.

#include<iostream>
#include<cstdlib>
#include<ctime>

using namespace std;

int main(){
    int array[100];
    int key;
    int search=0;
    int success=0;
    double percent;
    int average;
    
    
    srand(time(0));
    
    // Step 1: I first made the array of 100 random integers from 0 to 99//
    
    for(int i=0; i<100; i++){
          array[i]= 0 + rand() % 100;
          
          
          }
          
    // Step 2: generate a random search key from 0 to 199//
    
    for( int loop= 0; loop<100; loop++){
         
    key= 0 + rand() % 200;
    


    //Step 3; find the key using the sequential search algortihm//
          
      for(int j=0; j<100; j++){
             if( array[j]==key){
                 search=search + 1;
                 success= success + 1;
                 }
             else if(array[j] !=key){
                  search=search + 1;
                  success=success;
                  
                  }
                  

              }
              }
              
    
  


            percent= (success / search) * 10000;
            
             
             cout<<"The number of searches completed is:"<<search<<endl;
             cout<<"The number of successful searches is:"<<success<<endl;
             cout<<"The percentage of succesful searches is:"<<percent<<endl;
             
             
             
             

                 
                 
    
    
    
    
    system("pause");
    return 0;
}

>>array= 0 + rand() % 100;
That will not produce a random number between 1 and 100. Instead it produces a number between 0 and 99 inclusively. The instructions say nothing about generating 200 UNIQUE random numbers so I guess adding duplicate numbers to the array is not a problem.

As for the rest of the program I think you mis interpreted the instructions -- you have to use a loop something like this:

do this 100 times
    generate a random number between 1 and 100
    search the array for the number
    is it found ?
        yes, then increment success counter
    go back to top of loop
end of loop

About the if/else usage

if( array[j]==key){
        search=search + 1;
        success= success + 1;
    }
    else if(array[j] !=key){
        search=search + 1;
        success=success;
    }

You can change it to

if( array[j]==key){
        search=search + 1;
        success= success + 1;
    }
    else{ // here you already know that array[j] != key
        search=search + 1;
        success=success;  // <- that has no effect, you could remove it
    }

Then, in general you should format the code properly, as of now it's difficult to read, consider keeping related things together and properly indented

for( ... )
{
   for( ... )
   {
      if( ... )
      {
      }
      else
      {
      }
   }
}

guys i've updated my code and I would like you to look through it once again.. I also would like to ask how do I get the average test per search?? I just can't seem to figure it out...here's my code:

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

int Search( const int [], int, int ); 

int main()
{
    const int arraySize = 100; 
    int myarray[arraySize];
    int Key; 
    int search =0;
    int success=0;
    int average;
    double percent;
    
   srand(time(0));
   for ( int i = 0; i < arraySize; i++ )
   {
    myarray[i]= 1 + rand() % 101;
    Key= 0 + rand() % 201;
    int element = Search(myarray,Key, arraySize);
   
if ( element != -1 )
   {
        search=search + 1;
        success=success + 1;
        average=element;
   }
else
   {
       search= search + 1;
   }
   

}
   cout<<"*****************************SEARCH STATISTICS**************************"<<endl;
   cout<<"The number of searches is:"<<search<<endl;
   cout<<"The number of successful searches is:"<<success<<endl;
   cout<<"The average of test per search is:"<<average<<endl;



         
   return 0; 
} 


int Search( const int array[], int key, int sizeOfArray )
{
   for ( int j = 0; j < sizeOfArray; j++ )
      if ( array[ j ] == key )  
         return j; 

   return -1; 
}

A major change is required there, you cannot do it with one for() loop, first you have to initialize the array before doing any searches

// initialize the whole array first
for ( int i = 0; i < arraySize; i++ )
{
    myarray[i]= 1 + rand() % 200;  // [B]range 1 .. 200[/B]
}

// then issue [B]100[/B] searches ...
for ( int i = 0; i < [B]100[/B];  i+ )
{
   Key= 1 + rand() % 200; // [B]range 1 .. 200[/B]
    int element = Search(myarray,Key, arraySize);
    ...
}