Filling an array with a random number generator

I have this homework I don't want it to be solved .. just some guidance.

the question is :
Write a program that uses a random number generator to fill an array with 30 unique numbers between 0 and 100. The program prints out the array and then prints out all even numbers larger than the largest odd number in the array.


and this is the program that I wrote ... It keeps crashing on start-up :(
Also , how can I know If the number Is already in the array ? I am so confused
Any help is appreciated.

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
#include <iostream>
#include <cstdlib>

using namespace std;
int i;
int array[30];
int odd;
int max;
int counter;

int main()
{	
	for (int j = 0;j<30;j++)
	{

		i = rand() % 101;
		 array[i]=i;

	}

	for (int k = 0; k < 30 ; k++)
	{
		cout << array[k] << " ";

	}
	
	for (int l = 0; l < 30 ; l++)
	{
		odd = array[0];
		max = array[0];
		if (array[l] % 2 != 0 && array[l] > max)
		{
			odd = array[l];
			max = array[l];
		}
		if (array[l] % 2 == 0)
			++counter;

		if (array[l] % 2 == 0 && array[l] > max)
		{
			for (int m = 0; m < counter ; m++)
				cout << array[m];

		}
	}
	
	return 0;
}
1
2
3
4
5
6
7
	for (int j = 0;j<30;j++)
	{

		i = rand() % 101;
		 array[i]=i; //I believe this should be array[j]=i

	}
You initialize max (which by the way my compiler finds ambiguous as name meaning it confuses it with another max, consider changing the name to Max for example) to the first element
without knowing if it's odd or even:
max = array[0];
You can safely initialize it to a value less than your possible smaller bigger number (I mean the smaller odd number can be the bigger of all your odd number: this is 0 I guess if no odd is entered)

Line 17 is also wrong as @stridexr mentioned.

You also don't use unique numbers the way you produce them. Try a loop where you check the numbers entered so far with each new one. Accept it only when it hasn't appeared before (that the point of unique correct?)

Finally you use 3 loops for nothing but when comes to calculating your max you are using a single loop for calculating max, comparing this with your even numbers and also a counter (I don't think it will work as you have plan it with this counter). Don't do it this way. Use a loop for max only.
Thank you guys for helping me
I used your suggestions and here is the new code ... It is working perfectly except that It does not check whether the number Is already in the array or not (unique)



eypros
I tried using a for loop to check whether the new number is the same as the last number in the array , but I don't know how to make a loop to check the new number If it was repeated in the array , I mean all the numbers of the array not just the last number.


Any suggestions about the unique number problem ?

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int i;
int array[30];
int odd;
int Max;
int counter = 0;


int main()

{	
	cout << "The array consist of 30 elemnts is: ";
	cout << endl;

	srand ( time(0) );

	for (int j = 0;j<30;j++)
	{

		i = rand() % 101;
		 

		 if (i != i-1)
			array[j]=i;

		 else
		 {
			i = rand() % 101;
		 array[j]=i;
		 }
	}
	
	for (int k = 0; k < 30 ; k++)
	{
		cout << array[k] << " ";

	}
	cout << endl;
	
	cout << "All the even numbers larger than the largest odd number are: ";
	for (int l = 0; l < 30 ; l++)
	{
		odd = -1;
		Max = -1;
		for (int n = 0; n < 30 ; n++)
		{
			if (array[n] % 2 != 0 && array[n] > Max)
				{
					odd = array[n];
					Max = array[n];
				}
		}

		
		
	}
	
	for(int u = Max+1;u<=100;u++)

		{
			if (u % 2 == 0)
				cout << u << " ";
			if (u == 100)
				break;
		}
	cout << endl;
	cout << "The largest odd number is : " << Max;
	cout << endl;
	return 0;
}


Last edited on
Topic archived. No new replies allowed.