By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,193 Members | 826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,193 IT Pros & Developers. It's quick & easy.

question how to handle dupes

P: 57
I am trying to write a program that will input numbers in a array and then loop throught the array and look for dupes. The final output of the array should have the numbers in the array printed only once. And zero signals the end of the array and shouldn't be outputed. This is what I have so far can anyone show me my mistake.



Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4.  
  5.         int scores[20]; //array of numbers
  6.         int value;
  7.         int dupe;
  8.         int count;
  9.  
  10.         //prompt user for the list of numbers with zero being the end of the input.
  11.  
  12.         cout << "enter a list of numbers: ";
  13.         for (int i=0; i < 20; i++){
  14.  
  15.                 cin >> value;
  16.                  if (value == 0)
  17.                 {
  18.                 break;
  19.                 } 
  20.  
  21.                 scores[i] =  value;
  22.                 if (scores[i] == value) 
  23.                 {
  24.                 dupe = 1;
  25.                 }
  26.                 }
  27.                 if (dupe != 1)
  28.                 {
  29.                 scores[count] = value;
  30.                 }
  31.                 count++;
  32.  
  33.                 cout << value;
  34. }
Nov 7 '06 #1
Share this Question
Share on Google+
1 Reply


P: 6
Hi,

In your code.

Expand|Select|Wrap|Line Numbers
  1. scores[i] =  value;
  2.                 if (scores[i] == value) 
  3.                 {
  4.                 dupe = 1;
  5.                 }
  6.  
if statement will always return true because you are assigning value to scores[i] in the previous statement. So this doesnt seems correct.

In the code
Expand|Select|Wrap|Line Numbers
  1. if (dupe != 1)
  2.                 {
  3.                 scores[count] = value;
  4.                 }
  5.  
count is not initialized. It may contain garbage character scores[count] will not indicate an array element.


To detect dupes you need to traverse the array to check whether the value is present in array or not. For first value you can ignore the check and directly store it in array. For the second read you need to check array element 1. Similarly for next read you should check each array element to detect whether dupes are there.

for n th read you should check scores[0] to scores[n-1-count], where count is the number of dupes previously detected.

I wont give you the exact code for this. Try it urself and if you are in trouble, you will get help here...

~David
Nov 7 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.