?
Solved

Non-duplicate array

Posted on 2000-03-12
24
Medium Priority
?
376 Views
Last Modified: 2010-04-02
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 answer=0;answer<arraysize;answer++){
  ++frequency[a[answer]];
 }
 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
Comment
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
  • Learn & ask questions
  • 11
  • 8
  • 2
  • +3
24 Comments
 
LVL 32

Expert Comment

by:jhance
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

by:licker
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

by:licker
ID: 2610520
Adjusted points to 100
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:licker
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

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

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

// Read in array;

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

by:licker
ID: 2610786
Adjusted points to 140
0
 

Author Comment

by:licker
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++;
      }
   }

}
Please help, getting very frustrated.
0
 
LVL 3

Expert Comment

by:mnewton022700
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

by:Wyn
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

by:Wyn
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

by:Wyn
ID: 2611003
btw ,your code:

 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

by:ntdragon
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

by:LucHoltkamp
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

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

Author Comment

by:licker
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

by:Wyn
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

by:Wyn
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

by:Wyn
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

by:
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();
 
}

Free to ask.

Regards
W.Yinan
0
 

Author Comment

by:licker
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

by:Wyn
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

by:Wyn
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

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

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2615322
You are welcome :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question