Hello Growthra,
It is always helpful to post the complete assignment that you were given. This way no one has to guess at what you need to do.
My question is do you need to check for duplicates when the array is assigned a number or is it to check after the for loop is finished?
I revised you code a bit. Note the comments:
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
|
#include <iostream>
//#include <time.h> // <--- Use C++ <ctime> not the C header file.
#include <ctime>
#include <cstdlib> // <--- For "srand" and "rand".
using namespace std; // <--- Best not to use.
int main()
{
constexpr int MAXSIZE{ 20 };
//srand((unsigned int)time(0));
srand(static_cast<unsigned int>(time(nullptr)));
int array[MAXSIZE]{}; // <--- ALWAYS initialize your variables.
int i{}, highest = 0; // <--- "i" should be defined in the for loop. The only place it is used.
for (i = 0; i < MAXSIZE; i++)
{
array[i] = rand() % 100 + 1;
//if (i < 20) // <--- This if statement is not really needed. you can do without it.
//{
cout << array[i] << " ";
//}
if (array[i] > highest)
{
highest = array[i];
}
}
//cout << "\n\n";
cout << "\n\nThe Largest integer in the array is " << highest << '\n'; // <--- Changed.
return 0; // <--- Not required, but makes a good break point.
}
|
<time.h"> and <ctime> are the same thing. You only need 1 and <ctime> is the better choice.
Your "srand" is better than I have seen. I offer what I put there as an option which I believe is the more up to date way to code it. Also have a look at
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
About a 30 minute video, but worth the time to understand the problems with "rand".
I can tell you how to check for duplicates, but not knowing what you need to do if you find 1 I am at a bit of a loss.
I am kind of stuck here not knowing is you need to check for a duplicate at the time you generate the random number or after.
Since you are using an array I am thinking nested for loops to compare the first element of the array to the 2nd - last.
I will work on that for a bit.
Andy