Solved

Non-duplicate array

Posted on 2000-03-12
24
368 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
  • 11
  • 8
  • 2
  • +3
24 Comments
 
LVL 32

Expert Comment

by:jhance
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points to 100
0
 

Author Comment

by:licker
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points to 140
0
 

Author Comment

by:licker
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:LucHoltkamp
Comment Utility
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
Comment Utility
STL , sure .It's very useful sometimes.
0
 

Author Comment

by:licker
Comment Utility
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
Comment Utility
->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
Comment Utility
->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
Comment Utility
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 140 total points
Comment Utility
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
Comment Utility
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
Comment Utility
-> 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
Comment Utility
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
Comment Utility
OH Thank you much Wyn:) You were very helpful!

0
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
You are welcome :)
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now