473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

question how to handle dupes

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
1 1099
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

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

Similar topics

3
by: ScottH | last post by:
I was looking for thw SQL to delete dupes from a table, and came across this. All who saw it agreed in principle, but I can't quite figure out the logic. If we are deleting all rows whose rowid...
12
by: google_groups3 | last post by:
Hi all. I currently have 2 text files which contain lists of file names. These text files are updated by my code. What I want to do is be able to merge these text files discarding the...
3
by: Georges Heinesch | last post by:
Hi. This issue semms trivial, but I didn't get it working so far. I have a database, which contains dupes. I'd like to create a query, which shows all dupes (not only one record, but all...
27
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
0
by: gdarian216 | last post by:
my question is my array is set to a size of 20 but the user can input less. I was writting if statements to get rid of the dupes and was wondering if i would have to go through it all 20 times or is...
1
by: gdarian216 | last post by:
I am tring to get rid of dupes and his code is taking the first input and repeating it. I don't know why. this is what i have so far can anyone help #include <iostream> using namespace std; ...
3
by: gdarian216 | last post by:
I am writting a program that gets a list of numbers from a user no more then 20 integers and checks for dups. If zero is entered it stops entering numbers to array. I then need to output the entered...
7
by: Jan | last post by:
Hi: When I searched the newsgroup for this problem, I saw two or three instances of the question being asked, but it was never answered. Not too promising, but here goes: I have a form with...
10
by: username88 | last post by:
I am having trouble with a query for my database. It is a name & address database with columns like firstname, lastname, email, etc. I am trying to show dupes in my 20,000 name database. I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.