I wish to a write a program that creates an array of 100 random integers in the range for just argument sake from 1 to 200 and, then use a sequential search to search the array 100 times using randomly generated targets in the same range. Can anybody please help me? I love to also as a feature at the end of the program, display the statistics of firstly the number of searches it has completed, secondly, the number of successful searches, thirdly, the percentage of successful searches and, finally last but not least, the average number of test per search.

PS---I kno already that in order for me to determine the average number of tests per search, i will need to count the number of test for each search. Well at least that is what i figured(DUH). If it's possible i would appreciate it if anyone can help me with this project and that whom so ever can help me solve this problem can take as long as possible to find the solution. Link me up when u found out how to do this.

Recommended Answers

All 8 Replies

#include <ctime>
#include <iostream>

int main( void ) 
{
  int i;    
  
  srand(time(NULL));    
  i = rand()%200;  
  std::cout <<"Your random number is " <<i <<std::endl;  
  
  return(0);
}

Know how to loop?

This sounds like a homework assignment I once gave. Or maybe will give soon.

Please make your best attempt at solving this problem, then post your code if you find you have difficulties.

I found the solution to question i asked previously. But could someone please explain line by line what each line is doing. Thanx...

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

#define MAX_ARRAY 100
#define NUMBER_LIMIT 200
#define SEARCHES 100

void fillAryRand (int array[]);
bool seqSearch  (int array[], int last, int target, int &locn, int &tests);

int main (void)
{

	int numsFound =  0;
	int tests =  0;
	int srchComplete =  0;
	int locn;
	int i;
	int array [MAX_ARRAY];


	srand (time (NULL));
	fillAryRand (array);
	for (i = 1; i <= SEARCHES; i++)
	   {
	    if (seqSearch (array,
	        MAX_ARRAY - 1,
	        rand() % NUMBER_LIMIT + 1,
	        locn,
	        tests) 
	       )
	        numsFound++;
	    srchComplete++;
	   }
	   
	cout << "\nThere were " << srchComplete 
	     << " searches completed.\n";
	cout << "There were " << numsFound 
	     << " successful searches.\n";
	cout << (((float)numsFound / srchComplete) * 100);
	cout << "% of the searches were successful.\n";
	cout << "There were an average of " 
	     << ((float)tests / srchComplete);
	cout << " tests per search.\n\n";
	
	system ("Pause");
	return 0;
}

void fillAryRand (int array[ ])
{
	int i;

	for (i = 0;  i < MAX_ARRAY;  i++)
	     array[i] = rand() % NUMBER_LIMIT + 1;
	return; 
}

bool seqSearch (int array[], int last, int target,
                int &locn, int &tests)
{

	int looker  =  0;

	while ((tests++, looker < last)  
	    && (tests++, target != array[looker]))
	     looker++;
	locn  =  looker;
	return ((tests)++, target  ==  array[looker]);
}

I found the solution to question i asked previously. But could someone please explain line by line what each line is doing. Thanx...

Why? So that you can plagiarize someone else's code and turn it in as your own work? Why don't you add your own comments to work out the logic.

Relax Ancient Dragon. No sense in waving accusations like that around. Be cool. OK. Jeez. Nevermind, Mr. Dragon, i'll figure it out myself. I'll just remove the post since it offense everyone.

Relax Ancient Dragon. No sense in waving accusations like that around. Be cool. OK. Jeez. Nevermind, Mr. Dragon, i'll figure it out myself. I'll just remove the post since it offense everyone.

Translation: "Oops, I got busted trying to cheat." ;)

TRANSLATION: "IT DOESNT MATTER WHAT ANYONE THINKS AS LOOK AS I KNOW WHAT I POST WAS A MISTAKE AND DIDNT THAT SITE IS FILLED WITH SO-CALLED ACCUSER." But hey that's cool. I really dont care what u think.

>But hey that's cool. I really dont care what u think.
Then why do you keep trying to defend yourself? It's amazing how if you just say "Oops, I'm sorry", everyone just forgets about your mistake immediately. But if you yak yak yak about it, not only will people remember, they'll mock you too.

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.