-
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.
-
November 15th, 2009, 08:08 PM
#2
Re: print it only if it is not a duplicate
Originally Posted by omegaclass
however its only outputting duplicates
It's doing this because each time round the "for" loop (checking for duplicates) it writes the number in the index its on.
So lets say the population array was set to:1,5,3 and you had the random number 9.
This code would go through and do this:
1 == 9 ... no ... set element 0 to 9
5 == 9 ... no ... set element 1 to 9
3 == 9 ... no ... set element 2 to 9
To fix this you need to put the "if (!duplicate)" block of code after the end of the for loop.
Also put the "srand(time(NULL))" before the while loop. You dont want to keep re-seeding the random number generator, it doesn't get any more random if you do.
In terms of a "dynamic array" you havn't shown any code which attempts to create a dynamic array. You need to look into the classes offered by STL and Boost for that sort of thing.
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
-
November 15th, 2009, 08:27 PM
#3
Re: print it only if it is not a duplicate
thank you i will do what you recommend, again thanks, if i have more issues i will post what i have.
-
November 16th, 2009, 04:32 AM
#4
Re: print it only if it is not a duplicate
Code:
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <limits>
const int x = 20;
using namespace std;
int main()
{
int i=10,j=0,k=0,numx, count=0, size=0;
int temp[20];
int arr[20]={0};
int num[count];
srand(time(NULL));
while(1)
{
for (j=0;j<20;j++)
{
regen: numx = rand()%91+10;
count++;
for (k=0; k < count; ++k)
{
if (arr[k]==numx)
{
goto regen;
}
else
{
arr[k] = numx;
size++;
}
}
arr[size];
cout << arr[j] <<" ";
}
cin.get();
}}
-
November 16th, 2009, 04:42 AM
#5
Re: print it only if it is not a duplicate
the above is it, cant figure it out. its 3:40 am and the IQ point just went negative, so this will have to do. thanks couling, but i failed to get it.
-
November 16th, 2009, 07:47 AM
#6
Re: print it only if it is not a duplicate
Originally Posted by omegaclass
the above is it, cant figure it out. its 3:40 am and the IQ point just went negative, so this will have to do. thanks couling, but i failed to get it.
Look at this thread.
http://www.codeguru.com/forum/showthread.php?t=486652
It's the same question.
It starts off wrong, but you'll get your answer by the end.
-
November 16th, 2009, 07:50 AM
#7
Re: print it only if it is not a duplicate
Originally Posted by omegaclass
its 3:40 am and the IQ point just went negative, so this will have to do.
Light weight.
But seriously when you feel like that, get some sleep and start again in the morning. The longer you stay awake unable to work the more time you waste.
Okay, not sure what the count variable was for, size is what's needed in the for loop
Code:
for (k=0; k < size; ++k)
But other than that, it should run.
I'm aware of the odd lecturer who likes "goto", most don't. In the real programming world, you should never use them without a note from your parents. So if you have the time, see if you can remove it. Remember that a for loop can stop looping based or more than one condition:
Code:
for (init statement; condition1 && condition2; advance statement) {
// ...
}
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
-
November 16th, 2009, 11:26 PM
#8
Re: print it only if it is not a duplicate
i still have to submit this before 12 pm today and i just spent 10 hours at my job the 6 hours at school now the race is on, thanks to all.
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
|