Need help in Printing Distinct numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    const int SIZE = 10;
    int num = 0,
        store[SIZE] = {0},
        count = 0;
    
    cout << "Enter ten numbers: ";
    cin >> store[count];
    
    
    while (count != (SIZE-1))
    {
         count++;
         for (int i = 0 ; i < SIZE; i++)
         {
             if (store[i] == store[count])
            //i don't know what to write here... 
           //if numbers already in the array discard it
           //else store in the array

         }
         cin >> store[count];
    }
    
   for (int i = 0 ; i < SIZE; i++)
        cout << store[i] << " ";
    
    getch();
    return 0;
}



this program have to read 10 numbers from the array and print out the distinct numbers...
example:
input: 1 2 3 4 5 3 1 1 2 2
output: 1 2 3 4 5

my head is blank now.. don't know how to check the number inside the array
u just need to follow the following algorithm with the initial condition that :
STEP 1: accept any number from the given array of ten numbers.
STEP 2: check whether that number is already present in the new array.
STEP 3: Then copy it to new array if the result of STEP 2 is FALSE.
OR
STEP 3: DO nothing and go to step 1:

Repeat the above steps till every number from the given number is read......

Now ur problem lies in STEP 2 i guess...........its very simple following the below algorithm:

STEP 1: Select any number from the given number
STEP 2: Then check whether the number is present in ur new array or not, by running a loop which checks each element of the new array,return TRUE if match is found else FALSE

**to avoid a fatal error use a variable 'counter' to count the number of valid elements of the new array, and always perform the check till that level only...........when u use STEP 2 of the second algorithm**

u might wish to alter ur code if u want......or u can just place counter=0 initially; and introduce a for loop :
1
2
3
4
5
{ for(k=0;k<counter;k++)
{if(newarray[k]!=  store[i])
{newarray[counter]=store[i];}
counter++;
}}

while we place an outer while loop varying i from 1 to SIZE-1
hope it helps u...come back if nything still persists u........ :)
Last edited on
mj, thank you for spending your time to help me... but i still have some problems

so, in this program i need 2 array:
array1[]; (size 10)
array2[]; (size not fixed)

2 counters:
count1 (for array 1)
count2 (for array 2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    while (count != (SIZE-1))
    {
         count1++;

         for (int i = 0 ; i < SIZE; i++) //check present numbers in array1
         {
                for (int j = 0; j < count2 ; j++)  //count2 is 0
                {
                       if (array2[j] != array1[i])
                            array2[count2] = array1[i];   //store array1 elements in array2
                  }
                       count2++;
         }
         cin >> array1[count1];
    }


sorry i'm still not clear about second algorithm step 2 part...
[by running a loop which checks each element of the new array,return TRUE if match is found else FALSE]
does it means if the number is same return true else false? and what's the point to return true or false? i'm really confuse about it... sorry....
the size of array is fixed and equal to the original array=10; since we know that the number of members it will contain wud be less than or equal to 10, and to determine at what point it stops accepting ny further lements we introduce the counter...........like this............
second part ur correct in ur understanding....... im returnng true or false to detrmine whether copying should take place or not as pointed by the first algorithm.............. :)

and also there needs to be a correction while cheking if ther is a match or not, we introduce a variable 'check', given value zero initially; which increments each time no match is found; and we assign array[count2]=array1[i] only when check==counter.....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 while (count1 != (SIZE-1)) //count should be count1 
    {
         count1++;

         for (int i = 0 ; i < SIZE; i++) //check present numbers in array1
         {
                for (int j = 0; j < count2 ; j++)  //count2 is 0
                {
                       if (array2[j] != array1[i])
                            check++;   //increments for evry non-matching element
                  }
if(check==count2)
array2[count2]=array1[i];                       
count2++;
         }
         cin >> array1[count1];
    }
Last edited on
i had try in your way... but still can't get what i wan...
now i wanna make it clear..
this is what i want in my program:
when i enter input in array1 data inside it was:
array1[] = {1, 2, 3, 4, 5, 3, 2, 1, 4, 5} (size 10)

initially:
array2[] = {0, 0, 0, .......} (unknown size)

after compare or check with array1, same data will only store in array2 once
array2[] = {1,2,3,4,5} (size 5)

if i using above code, i found out after the while loop was done, check = 0, and count2 = 0, how can i suppose to print the elements in array2 that just finished copies , because if i used the fixed size of array1, which is 10, what it could it print because as above example it only have 5 elements inside... please give me more advice on what i want in my program...
Last edited on
u r not supposed to run the array 2 entirely from 0 to 9 rather from 0 to count2-1; ie y we introduced it so we do not get garbage values.......
You cannot construct an array of unknown size. You must pick a size when you make it. You can create new, bigger arrays as you go if you need them, but you cannot have an array of unknown size.

Alternatively, make array 2 the same size as array1, and keep track of how many unique integers you find (call this n in this example), and then when you've finished building array2, print out only the first n elements.

Here is a more C++ way of doing it, just for interest.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
  int myints[] = {1, 2, 3, 4, 5, 3, 2, 1, 4, 5};
  vector<int> myvector (myints, myints+10);
  vector<int>::iterator it;

  sort (myvector.begin(), myvector.begin()+10);

  vector<int> copyTarget(myvector.size(), 0);
  vector<int>::iterator rangeEnd = unique_copy (myvector.begin(),myvector.begin()+10,copyTarget.begin()); 

  vector<int> trimmedCopyTarget;
  vector<int>::iterator posInTrimmedCopyTarget = trimmedCopyTarget.begin();
  vector<int>::iterator posInCopyTarget = copyTarget.begin();
  while ( posInCopyTarget != rangeEnd)
    {
      trimmedCopyTarget.push_back(*posInCopyTarget);
      posInTrimmedCopyTarget++;
      posInCopyTarget++;
    }
  
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    cout << " " << *it;

  cout << endl;
  for (it=trimmedCopyTarget.begin(); it!=trimmedCopyTarget.end(); ++it)
    cout << " " << *it;

return 0;
}

mj:
i'll try it again until i get the answer... thanks for the advise

Moschops:
i just start learned array this few days... your code here is beyond my level... anyway, thanks for willing to help me, and i got your important point here! cannot have an array of unknown size , print out only the first n elements, i got new ideal now, i'm gonna make another try, thanks a lot!
and also i wud suggest few more things in the code to make it look simpler......1. accept the items in array1 separately..... and a few more changes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while (count1 != SIZE)) //count should be count1 
    {
         count1++;

         for (int i = 0 ; i < count1; i++) // i<count1 instead of SIZE just think since the array is not full yet... check present numbers in array1
         {
                for (int j = 0; j < count2 ; j++)  //count2 is 0
                {
                       if (array2[j] != array1[i])
                            check++;   //increments for evry non-matching element
                  }
if(check==count2)
{array2[count2]=array1[i];                       
count2++;}
         }
         cin >> array1[count1];
    }
Last edited on
finally i have solved my problem! thanks for all the help! ^.^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void array2Value(int a1[], int a2[] , const int SIZE, int &count)
{
    int notSame = 0;
    for (int i = 1; i < SIZE; i++)
    {
        notSame = 0;
        for (int j = 0; j < SIZE; j++)
        {
           if (a1[i] != a2[j])
              notSame++;   //add 1 if both value are not the same
         }     
           
        if (notSame == SIZE)
            a2[count+1] = a1[i]; //assign new value to the next index
            
        count++;
    }
}


i change some of it:
1) at first, i assign all the value to array1 ,and assign the 1st value of array1 to array2
2) then only i call my function to start compare/ check
3) problem solved ^__^
i added a function into it, so it is easy to read..
Last edited on
Topic archived. No new replies allowed.