Input a sequence of integers and output omitting repeats

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puterialia90
    New Member
    • Jul 2010
    • 2

    Input a sequence of integers and output omitting repeats

    Write the statements, including loop, to read in a sequence of positive integers and display the sequence after deleting adjacent repeated values. Use negative integer to terminate the loop. For example, if the user enters the following sequence:

    12 16 16 8 22 55 55 55 19 16 42 -1

    the output will be

    12 16 8 22 55 19 16 42

    Pleas do help me with this problem..

    this is my source code..

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    int main()
    {
        int array_num[100];
        int temp,index_array,breaker;
        index_array = 0;
        cout << "Enter your numbers : ";
        do 
        {
            int input;
            cin >> input;
            input = breaker;
            if (input != temp)
            {
               array_num[index_array] = input;
               index_array++;
               temp = input;
            } 
            else 
            {
               continue;
            }
        } while (breaker != -1);
        int i;
        for (int i = 0; i < index_array; i++)
        {
            cout << array_num[i];
        }
        char freeze;
        cin >> freeze;
    
    return 0 ;
    }
    Last edited by Banfa; Jul 15 '10, 08:47 AM. Reason: Added [code]...[/code] tags
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Presumably your program doesn't work. What is wrong? Do you get compiler errors? Do you get run-time errors? Does the program run but give the wrong answers?

    Comment

    • hype261
      New Member
      • Apr 2010
      • 207

      #3
      Originally posted by puterialia90
      Write the statements, including loop, to read in a sequence of positive integers and display the sequence after deleting adjacent repeated values. Use negative integer to terminate the loop. For example, if the user enters the following sequence:

      12 16 16 8 22 55 55 55 19 16 42 -1

      the output will be

      12 16 8 22 55 19 16 42

      Pleas do help me with this problem..

      this is my source code..

      Code:
      #include <iostream>
      #include <cstdlib>
      using namespace std;
      
      int main()
      {
          int array_num[100];
          int temp,index_array,breaker;
          index_array = 0;
          cout << "Enter your numbers : ";
          do 
          {
              int input;
              cin >> input;
              input = breaker;
              if (input != temp)
              {
                 array_num[index_array] = input;
                 index_array++;
                 temp = input;
              } 
              else 
              {
                 continue;
              }
          } while (breaker != -1);
          int i;
          for (int i = 0; i < index_array; i++)
          {
              cout << array_num[i];
          }
          char freeze;
          cin >> freeze;
      
      return 0 ;
      }
      I believe that your problem lies in this line of code...

      input = breaker;

      You have just lost the value that the user entered into the program and put in a trash value so it would be very hard input to equal temp.

      It should be breaker = input;

      Your loop is also poorly constructed because it will evaluate the negative ending number and add it to the end of the list.



      Also you may want to look at this line of code. Depending upon what version of the C++ standard your compiler is using you may get a compilier error for duplicate declaration of the int i.


      Code:
      int i; 
          for (int i = 0; i < index_array; i++) 
          { 
              cout << array_num[i]; 
          }

      Comment

      Working...