# include %26lt;iostream.h%26gt;
# include %26lt;iomanip.h%26gt;
#include %26lt;ctype.h%26gt; // needed for toupper
#include %26lt;stdlib.h%26gt;// needed for rand
#include %26lt;fstream.h%26gt;
using namespace std;
int main ( )
{
int num[20]={0};
const int numLength = 20;
for(int count = 0;count %26lt; 20;count++)
{
cout%26lt;%26lt;"enter 20 numbers with
repeating numbers"%26lt;%26lt;endl;
do{
cout%26lt;%26lt;"enter number "
%26lt;%26lt;(count+1)%26lt;%26lt;": "%26lt;%26lt;endl;
cin %26gt;%26gt; num[count];
}
while (num[count]%26lt;10
|| num[count] %26gt; 100);
}
cout %26lt;%26lt; "The original set of numbers were: ";
for(int countfun = 0;countfun
%26lt; 20;countfun++)
{
cout %26lt;%26lt; num[countfun]
%26lt;%26lt; setw(3);
}
cout %26lt;%26lt;"\nThe different numbers
from the set of integers are: ";
return 0;
}
I'm supposed to have 20 random numbers with repeating variables.
Example: My numbers 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
Original set of numbers: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
New set of numbers: 1 2
I can't use boolean to remove the duplicates. How would I do this? Thanks.
How would i do this c++ program without using boolean?
This sounds like a class project, so I'm not going to give you any code so that you won't be accused of cheating. That being said, here are a couple of solutions.
Get the instructor to define "can't use boolean." Are you not allowed a boolean variable? What about a method that returns a boolean?
If you can use a method, that is how I would do it:
bool duplicateFound(int* original_set, int num_to_check)
if that is not allowed, remember that in C++, boolean is really an integer. False is 0, anything else is true. But this is really cheating.
How about a recursive method that returns the duplicate?
int recursiveCheck(int* num_to_check, num)
{
//check the first number and you are not at the end of the array
// if check fails, increment the pointer and call this function
}
Reply:After you assign a number into the array, test whether that number is equal to any other array member. If it is a match, then do not increment to the next array member, because you want to assign the next number into that same array member. If it is not a match then you assign the next number to the same array member.
For example, the value of num[0] is 1 and you assign 1 into another array member, here's the test:
for(int i=0; i%26lt;count; i++)
if (num[i] == num[0]) count--;
Now you might have another problem, because each time you decrement "count", your other loop "for(int count = 0;count %26lt; 20;count++)" will run an extra iteration. That problem can be solved by using a variable such as "max" instead of the literal number "20", and decrementing "max" when you decrement "count". So now the test will look like this:
for(int i=0; i%26lt;count; i++)
if (num[i] == num[0])
{
count--;
max--:
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment