Array problems

This is what i have so far. I'm not completely sure if this correctly assigns the random numbers to the array, so if it doesn't let me know. But I need a simple way to find the max value from the random generated numbers. Any help would be greatly appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>	// for rand and srand functions
#include <ctime>
using namespace std;

int main()
{
	int num = 0;

	// srand(time(NULL));
	srand(1);

	for (int i = 0; i < 100; i++)
	{
		num = rand() % 100 + 1;
		cout << num << " ";
	}

	return 0;
}


You could loop through the array, and compare against the highest number you have found so far until you've looped through the entire array. That will get you the max.
it is now this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>	// for rand and srand functions
#include <ctime>
using namespace std;

int main()
{
	int num = 0;
	int ray [30] = {num}; 
	
	srand(1);

	for (int i = 0; i < 30; i++)
	{
		num = rand() % 100 + 1;
		cout << num << " ";
	}

	
	
	
	
	





Any suggestions or examples on how to do such?
Apologies, I assumed, for some reason, you had an array. But in any case you'll need a variable to keep track of the highest number you've seen so far.
As Zhuge says you need to include a method of identifying which num is greatest like:
L16 ........MAX=num; //assign the 1st num to MAX
L17 if(num>MAX)MAX=num; //upgrade MAX if next num>MAX
L18 cout<<"The maximum random number generated : "<<MAX;
Topic archived. No new replies allowed.