Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Non-duplicate array

Posted on 2000-03-12
Medium Priority
377 Views
I need to be able to take 20 integers and as each one is read, display it only if it is not a duplicate of a number already read.  This is what I have but it doesn't do what I need it to.  Anybody?

int nonduplic()
{
const int arraysize=5;
const int frequencysize=101;

int a[arraysize];
cout<<"Enter 20 integers between 10 and 100\n";
for(int i=0;i<arraysize;i++){
cin>>a[i];
if(a[i]<10||a[i]>100){
i--;
}
}
int frequency[frequencysize]={0};
}
for(int n=0;n<frequencysize;n++){
if (frequency[n]==1){
cout<<"The non-duplicate values are:\n"<<n;

}
}

}

I have a funny feeling the frequency is irrelevant...
0
Question by:licker
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 11
• 8
• 2
• +3

LVL 32

Expert Comment

ID: 2610398
Homework problem, eh?

You have:

const int arraysize=5;

So tell me.  How many integers can you keep track of using this??

>I have a funny feeling the frequency is irrelevant...

If frequency is irrelevant, then why did you put it in the code?  Ohhhh, wait a minute.  I get it.  This is NOT your code.  You copied it from someone else and don't understand what they did.....

0

Author Comment

ID: 2610472
For your information this IS my code and I declared the array to be of size 5 so that when I would run it I wouldn't have to enter 20 integers to see if it works!  I thought that if I checked to see which ones occurred more than once(hence the frequency) I would be able to discard them.  Now I realize that I don't need to discard them I need to print only one of them(hence why frequency may be irrelevant)
I have spent over a week trying different ways to approach this and several experts have been very helpful(UNLIKE YOURSELF!)  In the future if you don't have useful( or even courteous comments) keep them to yourself!
Thank You
0

Author Comment

ID: 2610520
0

Author Comment

ID: 2610521
I have taken another approach that seems to make more sense, but I am confused as to how I can take the values that have duplicates and set them aside so in the end I can display both those w/ and thosew/o duplicates.

#include <iostream.h>
#include<conio.h>
void checkarray(int[],int);

int main()
{
const int arraySize = 5;
int i,a[ arraySize ];

cout<<"Enter 20 integers between 10 and 100:\n";
for (i = 0; i < arraySize; i++ ){
cin>>a[i]i;
if (a[x]<10||a[x]>100){
x--;
}
}
checkarray(a,arraysize);
cout>>"The non-duplicate values are:\n";

for(i=0;i<arraysize;i++){
cout<<a[i];
cout<<"\n\n\t";
getch();
}
}

void checkarray(int b[], int size)
{
for ( int n = 0; n < size; n++ )
//UNSURE HOW TO FINISH;

}
0

LVL 3

Expert Comment

ID: 2610666
Here is one possible way to do it.

int a[arraySize];
int duplicates[arraySize];
int nonDuplicates[arraySize];
int numDuplicates;
int numNonDuplicates;

checkArray(a, arraySize,
duplicates, numDuplicates,
nonDuplicates, numNonDuplicates);

// Print out duplicate and nonDuplicate arrays.

---------------

void checkArray(int a[], int size,
int nonDuplicates[], int & numNonDuplicates,
int duplicates[], int & numDuplicates)
{
numDuplicates = 0;
numNonDuplicates = 0;

for (int i = 0; i < size; i++)
{
int duplicate = 0;

for (int j = 0; j < size; j++)
{
if (i != j && a[i] == a[j])
{
duplicate = 1;
}
}

if (duplicate)
{
duplicates[numDuplicates] = a[i];
numDuplicates++;
}
else
{
nonDuplicates[numNonDuplicates] = a[i];
numNonDuplicates++;
}
}
}

If you want the duplicates array to be unique you could add another for loop before you add an integer to it.

0

Author Comment

ID: 2610786
0

Author Comment

ID: 2610787
OK so I tried the following and I get an off the wall answer in hex:

void checkArray(int [],int,int [],int,int[],int);

int main()
{
const int arraySize=5;
int a[arraySize];
int duplicates[arraySize];
int nonDuplicates[arraySize];
int numDuplicates;
int numNonDuplicates;

cout<<"Enter 20 integers between 10 and 100:\n";
for (int x=0;x<arraySize;x++){
cin>>a[x];
if(a[x]<10||a[x]>100){
x++;
}
}
checkArray(a, arraySize,duplicates, numDuplicates,
nonDuplicates, numNonDuplicates);

cout<<"The non-duplicate values are:\n"<<duplicates<<nonDuplicatesendl;
cout<<"\n\n\t";
getch();

}
void checkArray(int a[],int size,int nonDuplicates[],int numNonDuplicates,
int duplicates[],int numDuplicates)

{
numDuplicates = 0;
numNonDuplicates = 0;

for (int i = 0; i < size; i++)
{
int duplicate = 0;

for (int j = 0; j < size; j++)
{
if (i != j && a[i] == a[j])
{
duplicate = 1;
}
}

if (duplicate)
{
duplicates[numDuplicates] = a[i];
numDuplicates++;
}
else
{
nonDuplicates[numNonDuplicates] = a[i];
numNonDuplicates++;
}
}

}
0

LVL 3

Expert Comment

ID: 2610895
You need to make the array sizes pass by reference (ie. don't forget the &'s).

void checkArray(int a[],int size,int  nonDuplicates[], int & numNonDuplicates
int duplicates[],int & numDuplicates)

After calling checkArray print out the arrays like this.

cout << "Non-duplicates: ";

for (int count1 = 0; count1 < numNonDuplicates; count1++)
{
cout << nonDuplicates[count1] << " ";
}

cout << endl;
cout << "Duplicates: ";

for (int count2 = 0; count2 < numDuplicates; count2++)
{
cout << duplicates[count2] << " ";
}

cout << endl;
0

LVL 5

Expert Comment

ID: 2610979
Hello,licker
Below is the code I write.
Take a look.

void checkArray(int [],int,int [],int,int[],int);

int main()
{
const int arraySize=5;
int a[arraySize];
int duplicates[arraySize];
int nonDuplicates[arraySize];
int numDuplicates;
int numNonDuplicates;

cout<<"Enter 20 integers between 10 and 100:\n";
for (int x=0;x<arraySize;x++){
cin>>a[x];
if(a[x]>10&&a[x]<100){
x++;
}
}
checkArray(a, arraySize,duplicates, numDuplicates,nonDuplicates, numNonDuplicates);

cout<<"The non-duplicate values are:\n"<<duplicates<<nonDuplicatesendl;
cout<<"\n\n\t";
getch();

}

void checkArray(int a[],int size,int nonDuplicates[],int numNonDuplicates,
int duplicates[],int numDuplicates)
{  int temp=0;
numDuplicates = 0;
numNonDuplicates = 0;
for(int j=1;j<size;j++)
{
for(int i=0;i<size-j;i++)
if(a[i]>=a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1}=temp;
}
}//sort the int array from small to big

bool firsttime=TRUE;//is it the duplicate the firsttime

for(int k=0;k<size;k++)
{
if(a[k]==a[k+1]){
if(!firsttime)
continue;
duplicates[numDuplicates]=a[k];
numDuplicates++;
firsttime=FALSE;
}//if
else{
firsttime=TRUE;
nonduplicates[numNonDuplicates]=a[k];
numNonDuplicates++;} //else

} //for

}//end
0

LVL 5

Expert Comment

ID: 2610985
licker,code above store them into numDuplicate and numNonDuplicate.If you want to use them in Main(),you'd pass them by reference or address.

0

LVL 5

Expert Comment

ID: 2611003

for (int x=0;x<arraySize;x++){
cin>>a[x];
if(a[x]<10||a[x]>100){
x++;
}
}
is strange to me.
Maybe this way

for (int x=0;x<arraySize;x++){
cin>>a[x];
if(a[x]<10||a[x]>100){
x--;
}
0

LVL 1

Expert Comment

ID: 2611143
i won't wirte it for you but here is an idea

use a 2-3 tree from all the integers
that you all ready read then each time you take an integer try to find it in the tree if you didn't find it print it and add it to the tree

the idea is that to find anything in a 2-3 tree is log(n) and to insert is log(n)
0

LVL 3

Expert Comment

ID: 2611394
A STL set is of course the way to go:

#include <set>
#include <iostream>
using namespace std;

typedef set< int, less<int> > Set;

int main()
{
Set the_set;
for (int i = 0; i < 20; ++i)
{
int x;
cin >> x;
if (x < 10 || x > 100) --i;
else the_set.insert(x);
}

for (Set::iterator si = the_set.begin(); si != the_set.end(); ++si)
cout << *si;
return 0;
}
0

LVL 5

Expert Comment

ID: 2611463
STL , sure .It's very useful sometimes.
0

Author Comment

ID: 2612165
Cannot use call by reference, only null or call by value.  Need to display as follows:

Enter 20 integers...:
15 16 15 17 18 25...

The non-duplicate values are:

15 16 17 18 25...

No need to sort values
Wyn, what is FIRST, LAST?  Too advanced... Can only use basics of arrays:(
0

LVL 5

Expert Comment

ID: 2612306
->what is FIRST, LAST?

licker,i mean if it's the first duplicate number ,my CheckFunction() code first will sort the arrays from small to large and later traverse it and store numDuplicate ,numNonDupliate ,Duplicate[],NonDuplicate[] respectively ,i.e:
INPUT FROM USER:

3 7 4 5 6 7 7 7

Ater sort it,it becomes:

3 4 5 6 7 7 7

Then,in my last for loop,if the first is false  means 7 has stored into Duplicate[] and NumDuplicate,so We should not count it again ,then ignore the third 7 above.
Understand?

licker, You can ask more for clarification before reject one,right ?

Regards
W.Yinan

0

LVL 5

Expert Comment

ID: 2612355
->Cannot use call by reference, only null or call by value.
=========================
What do you mean by this? Do you mean what is by reference?

I sort it just for simplicity to get the duplicate ones as I have explained in my last comment.
0

LVL 5

Expert Comment

ID: 2612365
Btw,there is a typo,should be:
3 7 4 5 6 7 7 7

Ater sort it,it becomes:

3 4 5 6 7 7 7 7

Then I can easily get your need.Right? Just one for(i=0;i<size;i++) as my code illustrate.
0

LVL 5

Accepted Solution

Wyn earned 560 total points
ID: 2612557
Hi,licker.
Anyway,below is working Project I just write .I have compiled and run it.All do quite well.You can simply copy and compile and link and run.

I assume 5 number ,You can change the size at will.

It will show duplicated value number,value contents ,nonduplicated value number,value contents.
===================================
#include "iostream.h"
#include "conio.h"

void checkArray(int a[],int size,int nonDuplicates[],int& numNonDuplicates,
int duplicates[],int& numDuplicates)
{  int temp=0;
numDuplicates = 0;
numNonDuplicates = 0;
for(int j=1;j<size;j++)
{
for(int i=0;i<size-j;i++)
if(a[i]>=a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}//sort the int array from small to big

bool firsttime=true;//is it the duplicate the firsttime

for(int k=0;k<size;k++)
{
if(a[k]==a[k+1]){
if(!firsttime)
continue;
duplicates[numDuplicates]=a[k];
numDuplicates++;
firsttime=false;
}//if
else{
firsttime=true;
nonDuplicates[numNonDuplicates]=a[k];
numNonDuplicates++;} //else

} //for

}//end

void main()
{
const int arraySize=5;
int a[arraySize];
int duplicates[arraySize];
int nonDuplicates[arraySize];
int numDuplicates;
int numNonDuplicates;

cout<<"Enter 5 integers between 10 and 100:\n";
for (int x=0;x<arraySize;x++){
cin>>a[x];
if(a[x]<10||a[x]>100){
x--;
}
}
checkArray(a,arraySize,nonDuplicates, numNonDuplicates,duplicates, numDuplicates);

cout<<"The duplicate values number is:\n"<<numDuplicates<<endl;
cout<<"The Nonduplicate values number is:\n"<<numNonDuplicates<<endl;

cout<<"the duplicate values are"<<endl;
for(int i=0;i<numDuplicates;i++)
cout<<duplicates[i]<<endl;

cout<<"the nonduplicate values are"<<endl;
for(int j=0;j<numNonDuplicates;j++)
cout<<nonDuplicates[j]<<endl;

_getch();

}

Regards
W.Yinan
0

Author Comment

ID: 2612890
I must solve the problem using call by value.  I do not need to sort the values in order, just print them w/out their duplicates
0

LVL 5

Expert Comment

ID: 2612932
-> I do not need to sort the values in order. =========================
Again,i'm not sorting them.

->I must solve the problem using call by value
========================
I think you'd better look how I call the CheckArray().
If you want to call by value.
It's easy .You can simply print them in the CheckArray() function.Then ,you get rid of the &:)
But I'm still wonder why  you refuse by reference?It's good here.
Or I misunderstand?

->just print them w/out their duplicates
===============================
Just delete print code I wrote above you dont want.
i.e :change:
===============
cout<<"The duplicate values number is:\n"<<numDuplicates<<endl;
cout<<"The Nonduplicate values number is:\n"<<numNonDuplicates<<endl;

cout<<"the duplicate values are"<<endl;
for(int i=0;i<numDuplicates;i++)
cout<<duplicates[i]<<endl;

cout<<"the nonduplicate values are"<<endl;
for(int j=0;j<numNonDuplicates;j++)
cout<<nonDuplicates[j]<<endl;
========================
TO
==========================
cout<<"the nonduplicate values are"<<endl;
for(int j=0;j<numNonDuplicates;j++)
cout<<nonDuplicates[j]<<endl;
=============================

But since you dont need duplicates and so on,why you declare them  in your code?

Regards
W.Yinan

0

LVL 5

Expert Comment

ID: 2613010
FYI,here is the call by value code.
and only paint the non-duplicated array.
===================================
#include "iostream.h"
#include "conio.h"

void checkArray(int a[],int size,int nonDuplicates[],int numNonDuplicates,
int duplicates[],int numDuplicates)
{  int temp=0;
numDuplicates = 0;
numNonDuplicates = 0;
for(int j=1;j<size;j++)
{
for(int i=0;i<size-j;i++)
if(a[i]>=a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}//sort the int array from small to big

bool firsttime=true;//is it the duplicate the firsttime

for(int k=0;k<size;k++)
{
if(a[k]==a[k+1]){
if(!firsttime)
continue;
duplicates[numDuplicates]=a[k];
numDuplicates++;
firsttime=false;
}//if
else{
firsttime=true;
nonDuplicates[numNonDuplicates]=a[k];
numNonDuplicates++;} //else

} //for

cout<<"the nonduplicate values   are"<<endl;
for(int j=0;j<numNonDuplicates;j++)
cout<<nonDuplicates[j]<<endl;

}//end

void main()
{
const int arraySize=5;
int a[arraySize];
int duplicates[arraySize];
int nonDuplicates[arraySize];
int numDuplicates = 0;
int numNonDuplicates = 0;

cout<<"Enter 5 integers between 10 and 100:\n";
for (int x=0;x<arraySize;x++){
cin>>a[x];
if(a[x]<10||a[x]>100){
x--;
}
}
checkArray(a,arraySize,nonDuplicates, numNonDuplicates,duplicates, numDuplicates);

_getch();

}
============================
So,as you see.What's good by value...

And if you really need another approach.
You can compare one another using two for loop,i.e:
for(int i=0;i<size;i++)
for(int j=0;j<size;j++)
if(a[i]==a[j])
...............
...............
...............

But in my eyes,this is thoroughly no gain on speed and time,even worse.

However,You can optimize my code,to compress it into (0+size)size/2 comparison times.(note ,less than size*size times).You simply test them in my first for loop but I dont think it's necessary.

Regards
W.Yinan

0

Author Comment

ID: 2613681
OH Thank you much Wyn:) You were very helpful!

0

LVL 5

Expert Comment

ID: 2615322
You are welcome :)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more â€¦
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.htâ€¦
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month8 days, 15 hours left to enroll