• Status: Solved
• Priority: Medium
• Security: Public
• Views: 383

# Non-duplicate array

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
licker
• 11
• 8
• 2
• +3
1 Solution

Commented:
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 Commented:
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 Commented:
0

Author Commented:
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

Commented:
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 Commented:
0

Author Commented:
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

Commented:
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

Commented:
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

Commented:
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

Commented:

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

Commented:
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

Commented:
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

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

Author Commented:
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

Commented:
->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

Commented:
->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

Commented:
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

Commented:
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 Commented:
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

Commented:
-> 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

Commented:
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 Commented:
OH Thank you much Wyn:) You were very helpful!

0

Commented:
You are welcome :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 11
• 8
• 2
• +3
Tackle projects and never again get stuck behind a technical roadblock.