5
Contributors
8
Replies
10
Views
9 Years
Discussion Span
Last Post by Narue
0
#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?

0

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.

0

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

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.

0

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.

0

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." ;)

0

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.

0

>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.

This article 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.