Incomplete code that I am attempting to fix

I'm having a hard time writing the body of the bolded functions. Any help would be great.




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

bool findIt(int* start, int* end, int value);
//this function will search the array beginning with start
//and ending with end for the presence of value
//if found it returns true, if not return false


void randomizeIt(int* start, int length, int max);
// this function will fill the array starting at start
//with random numbers between 1 and max until it reaches length


int main()
{
//define variables
bool again = true; // controls whether user wants to find
//another number
const int MaxSize = 10; // number of items in the array
const int MaxNum = 100; // max value of item in array
int numArray[MaxSize -1];
int userInput = -1; //value entered by user to search for


srand(time(0));
do{
if (userInput == -1) //randomize array - if userInput = -1
randomizeIt(numArray, MaxSize, MaxNum);

cout << "Enter a number between 1 and " << MaxNum <<
" (0 = quit, -1 = randomize array ";
cin >> userInput;
if (userInput > 0){
if(findIt(numArray, &numArray[MaxSize-1], userInput))
cout << "found " << userInput <<endl;
else cout << userInput << " not found" << endl;
}else{
if(userInput != -1) again = false;
}
}while (again);
return 0;
}
Last edited on
They do not seem to work when I attempt to write the code.


Perhaps you could supply the attempted code so it might be pointed out what's wrong with it?

http://cplusplus.com/articles/jEywvCM9/
bool findIt(int* start, int* end, int value)
{
for (int *p = start; p<=end; p++) {
if (*p == value)
return true;
}
return false;
}

#include <stdlib.h>
void randomizeIt(int* start, int length, int max)
{
for (int *p = start; p<=end; p++) {
*p = rand() % max; // could also use "random()"
}
}
findIt looks good to me given the way it is called.

In randomizeIt, you need to take into account the length parameter. There is no end variable in randomizeIt.

rand() % max gives a value in the range of 0 to max-1. Add 1 to move it into your target range.
Topic archived. No new replies allowed.