So I'm messing around trying to generate an array with no duplicate numbers.I realize that I am making this way more complicated than it needs to be and that there are other ways to accomplish this.
I seem to have created an infinite loop, somewhere in the do while statement maybe? Other than it not exiting the loops it seems to function how I wanted it to. Suggestions?
In the first line of your do-loop block, you set dup to 0. You then don't change it anywhere in the rest of your code. So how can the condition (dup != 0) ever be true?
You define the array p to have 6 elements, but then you loop over seven values for the array indices c and i (0 to 6). When you try and access p[6], you're accessing memory past the end of the array, which will give you undefined behaviour.
int main(){
int temp,size=16,p[size];
int dup;
srand(time(0));
cout<<"count #"<<endl;
for (int c=0;c<=size-1;c++){
do{
dup=0;
temp=rand()% size;
for (int i=0;i<=size-1;i++){
if(temp==p[i]){
dup++;
}
}
}while(dup!=0);
p[c]=temp;
cout<<c<<" "<<p[c]<<endl;
}
cout<<"End of the line"<<endl;
return 0;
}
On line 17 I increment dup. Sorry I sort of hid it!
Apologies for missing that. Although I would add that with a more consitent indentation and bracing style, it would be easier for both you and others to read your code and see what's going on. For example, it would make sense for the end of your while loop to be aligned with the start of it.
Moving that dup++ to a new line certainly helps - and bonus points for enclosing it in braces :)
You've ignored my comments about the fact that you're writing past the end of your array. Both your for loops are looping from 0 to 16, which means that in their final iterations they're attempting to access p[16], which is the 17th element of the array. However, you've declared p to only have 16 elements, so on that final iteration, you're going past the end of the array.
Yes. Think about it logically - your index is starting at 0, and your condition says that the loop will continue for as long as it is equal to or less than 16. That means that it will loop 17 times. But your array is only 16 elements long.
Your codes should still function good or well.
Because the do while loop is to ensure each member of the array is assigned a different or unique number from the randomly generated numbers from 0 to size-1. Even if you change the value of size to any number above 16, the program should run.
cool stuff there.
Slightly modified to read it easy here's the code and the output working for size = 30.
I'm working on Linux and my command line for building the program is:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
usingnamespace std;
int main()
{
int size;
cout << "How many numbers? ";
cin >> size;
if(size <= 0) return -1;
int *p = newint[size]; // dynamic memory allocation
srand(time(0));
int contor = 0;
while(contor < size)
{
bool flag = false; // a search starts with flag down
int temp = rand() % size;
for(int i = 0; i < contor; ++i)
if (temp == p[i])
{
flag = true; // was found a duplicate among p[]...
break; // breaking the for loop
}
if(!flag) // if the flag remains down then temp is good
p[contor++] = temp; // and is enlisted!
}
for(int i = 0; i < size; ++i)
cout << setw(4) << i << setw(7) << p[i] << '\n';
cout << '\n';
delete[] p; // memory allocation release
return 0;
}