|
-
November 15th, 2009, 07:39 PM
#1
print it only if it is not a duplicate
hi everyone, i have a problem with this code, its not doing what i want. the code should only output values that are not duplicates, however its only outputting duplicates, also the dynamic array is not working, could someone look at this code and offer some suggestion on what i should do to fix it, your time is much appropriated, thanks.
Code:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <limits>
const int x = 20;
using namespace std;
void main()
{
int i = 0;
int toss = 0;
int count=0;
int population[20];
int num=0;
bool duplicate = false;
while(1)
{
for (int j = 0; j < x; j++) // test for duplicate random num
{
srand(time(NULL));
num = rand()%90+11;
if (num == population[j])
{
duplicate = true;
//toss++;
break;
} // exit the loop if true
if (!duplicate) // if not duplicate
{
population[j] = num; // fill the dynamic array
count++;
}
}
for (int ii = 0; ii <= count; ii++)
{
cout << population[ii]<<" ";
}
cin.clear();
cin.get();
}
}
Use a one dimensional array to solve the following problem. Read in 20 numbers,
each of which is between 10 and 100, inclusive. As each number is read, print it only if it
is not a duplicate of a number already read. Provide for the “worst case” in which all 20
numbers are different. Use the smallest possible array to solve this problem.Well,
create the array, then each time you randomly generate a number, loop through the
array and check that the number isn't already in there. If it isn't, put it in,
otherwise re-randomize it.
Last edited by omegaclass; November 15th, 2009 at 07:48 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|