I wanted to make an array that contains numbers that are in order from 1 to 100 but one number is missing and this missing number is to be a random value within the said range and that missing numbers' value can not be assumed as a result, this array should only contain 99 elements because its missing one and then I want to print it.
then using this array I need to find the missing number and not assuming the value of the randomized number. So needs to be found through by code then print it as well.
P.S. can't use factions and must use the only given 2 headers.
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <time.h>
usingnamespace std;
int main()
{
int numbers[99]
srand(time(0))
}
First thing is to make a plan.
How would you detect if a number is missing?
Hint:
If none are missing each number must be 1 smaller than the following.
int numbers[99]
If you need to 100 numbers your array size should be 100.
Fill the array with values from 1 .. 100 in a loop.
Choose a random index and the number to random number - must be != the index+1
Loop through the array again and detect the number out of range.
How are you supposed to generate random numbers? rand() is in <cstdlib>, which allegedly is not allowed.
Edit: Actually you could use time(NULL) % 100, to get predictable, but still a different number as long as you don't run the program more than once a second.
You have 100 numbers and 99 slots, so as you go from 0 to 98 inclusive, if the current index = the number you're excluding, then skip to the next number.
I kind of disagree that this is an xy problem. The code shown basically amounts to nothing, so it doesn't not match the instructions; there just isn't anything there to critique in the first place.
If the size of the array was, say, 9, then the solution would be to choose a random number, say 3, and the array would become: [1, 2, 4, 5, 6, 7, 8, 9, 10]. Same thing applies to size = 99. After forming this array, the code needs to assume that this random number is not known, and to search for a missing number/gap within the array. This could be done by sorting the array (assuming worst case, that it isn't already sorted).
PS: OP edited their post to add srand(time(0)), despite still not #including <cstdlib>. So I guess we can assume that the professor's program will compile without it. Very bad habits that this professor is making their students form!
a simple idea ...
bool numbers[101] {true}; //we will only use 1 to 100, 0 is ignored.
numbers(rand()%100+1) = false; //a random value from 1 to 100
now, you have an array and one of the values is missing.
use this to solve the rest of your problem.
something like
for(int i = 1; i <= 100; i++)
if(!numbers[i]) cout << "the offending value is " << i << endl;