Kindly Help me understand arrays

I don't know what to do.

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>

using namespace std;

int main()
{
   int numbers[99]
   srand(time(0))


}

Last edited on
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.
I don't get what you mean for the "Choose a random index and the number to random number - must be != the index+1" can you visualize it/ type in code.
Last edited on
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.
Last edited on
1 to 100 inclusive is 100 numbers less the missing one which gives 99 numbers which have index 0 to 98.

Is the array containing the numbers except the missing number in ascending order - or is the ordering of these numbers random?

Also note that you need an extra #include for the random number function. Also, #include <time.h> should be #include <ctime>

If the ordering is ascending then perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
	srand(static_cast<unsigned int>(time(nullptr)));

	constexpr size_t maxnum {100};
	const size_t miss {rand() % maxnum + 1};
	size_t numbers[maxnum - 1] {};

	for (size_t i = 1, e = 0; i < maxnum; ++i)
		if (i != miss)
			numbers[e++] = i;

	size_t fnd {maxnum};

	if (numbers[0] != 1)
		fnd = 1;
	else
		for (size_t e = 0; e < maxnum - 2; ++e)
			if (numbers[e] != numbers[e + 1] - 1) {
				fnd = numbers[e] + 1;
				break;
			}

	cout << "The missing number is " << fnd << '\n';
}



Last edited on
Hello Growthra,

Growthra wrote:

I wanted to make an array that contains numbers 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 must be found through the code.

P.S.can't use factions and must use the only given 2 headers.


If this is for school post the full instructions that you were given.

The code that you posted does not match the instructions.

Right now it looks like the proverbial X Y problem. http://xyproblem.info/
Because no one is understanding what you want to do.

Something that you could do is demonstrate what the array should look like.

Here is something that you can start with:

  1   2   3   4   5   6   7   8   9  10
 11  12  13  14  15  16  17  18  19  20
 21  22  23  24  25  26  27  28  29  30
 31  32  33  34  35  36  37  38  39  40
 41  42  43  44  45  46  47  48  49  50
 51  52  53  54  55  56  57  58  59  60
 61  62  63  64  65  66  67  68  69  70
 71  72  73  74  75  76  77  78  79  80
 81  82  83  84  85  86  87  88  89  90
 91  92  93  94  95  96  97  98  99 100



Andy
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!
Last edited on
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;
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <ctime>

int main()
{
   int numbers[99];
   int p = 1 + time( 0 ) % 100;
   for ( int i = 1, j = 0; i <= 100; i++ ) if ( i != p ) numbers[j++] = i;

   int missing = 1;
   while ( missing < 100 && numbers[missing-1] == missing ) missing++;
   std::cout << "Missing " << missing << "\n\n";

   for ( int i = 0; i < 99; i++ ) std::cout << numbers[i] << ( (i+1)%10 == 0 ? '\n' : ' ' );
}


Missing 52

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 53 54 55 56 57 58 59 60 61
62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81
82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 
Last edited on
@op,
to make things a bit simpler let's do it with 10 numbers.
 
int numbers[10] = {1,2,3,4,5,6,7,8,9,10};

Let a random index be 3 and the random value be 6.
1
2
3
numbers[3] = 6
Your array would be:
numbers[10] = {1,2,3,6,5,6,7,8,9,10};

Now your code needs to find out that numbers[3] is "missing"
Hope it makes some sense.
Topic archived. No new replies allowed.