Thread: printing out the unique numbers

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    printing out the unique numbers

    heya, i am making a program that prints out the unique numbers entered, for example if u enter 10 20 10 30 30 it would print : 10 20 30

    this is my code

    Code:
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::cin;
    
    
    /* FUNCTION PROTOTYPES */
    /****************************************************************************/
    void print_Array (int [], int );
    
    /****************************************************************************/
    
    
    int main ()
    {
      const int size = 10;
      int Readin_Array[size] = {0};
      int Final_Array[size]  = {0};
      int temp = 0;
      int controller = 0;
    
    
      cout <<"Please enter 10 numbers.in range 10-100"<<endl;
      for (int x = 0; x<size; x++)
      {
        cout <<"please enter number "<< x+1 <<": ";
        cin  >> temp;
        while (temp > 100 || temp < 10)
        {
          cout <<"the number must be between 10 and 100."<<endl;
          cin >> temp;
        }
        Readin_Array[x] = temp;
      }
    
      for (int x = 0; x<size; x++)
      {
        int Hold = Readin_Array[x];
        int Count = 0;
    
        while (Count <= size )
        {
          if (Final_Array[Count] == Hold )
          {
            controller = 1;
          }
          Count++;
        }
    
        if (controller !=  1)
        {
         
          Final_Array[x] = Hold;
    
        }
    
      }
    
      cout << endl;
      cout <<"Unique numbers:" << endl;
      print_Array (Final_Array,size);
    
    }
    
    
    
    
    
    
    
    
    
    
    
    /* FUNCTION BODIES */
    /****************************************************************************/
    /****************************************************************************/
    void print_Array (int Array[], int size)
    {
      for (int x = 0; x<size; x++)
      {
        cout <<"\t"<<  Array[x] ;
        if (x == 9){cout << endl;}
      }
    }

    there is a problem, i cant get the final_array to be filled. :/ i dont rly know what i have done wrong

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int controller = 0; should be inside loop :
    Code:
    for (int x = 0; x<size; x++)
      {
         int controller = 0; 
        int Hold = Readin_Array[x];
    also the problem can be in the way how you print the array (the function is not shown here)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    Quote Originally Posted by vart
    int controller = 0; should be inside loop :
    Code:
    for (int x = 0; x<size; x++)
      {
         int controller = 0; 
        int Hold = Readin_Array[x];
    also the problem can be in the way how you print the array (the function is not shown here)

    k that worked great y does controller actually needs to be declaired inside the for loop body?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it should be reinitialized on each iteration
    and because itiis not used outside the loop - it is better to declare it inside the loop

    each var should have smallest possible scope to prevent various errors of using incorrect and not initialized values
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  2. printing only prime numbers
    By Micko in forum C Programming
    Replies: 30
    Last Post: 06-10-2004, 10:23 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 10:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM