4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by mitrmkar
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
0

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
      {
      }
   }
}
0

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; 
}
0

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);
    ...
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.