By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,564 Members | 804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,564 IT Pros & Developers. It's quick & easy.

Input a sequence of integers and output omitting repeats

P: 2
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..

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int array_num[100];
  8.     int temp,index_array,breaker;
  9.     index_array = 0;
  10.     cout << "Enter your numbers : ";
  11.     do 
  12.     {
  13.         int input;
  14.         cin >> input;
  15.         input = breaker;
  16.         if (input != temp)
  17.         {
  18.            array_num[index_array] = input;
  19.            index_array++;
  20.            temp = input;
  21.         } 
  22.         else 
  23.         {
  24.            continue;
  25.         }
  26.     } while (breaker != -1);
  27.     int i;
  28.     for (int i = 0; i < index_array; i++)
  29.     {
  30.         cout << array_num[i];
  31.     }
  32.     char freeze;
  33.     cin >> freeze;
  34.  
  35. return 0 ;
  36. }
  37.  
Jul 15 '10 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 2,398
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?
Jul 15 '10 #2

100+
P: 207
@puterialia90
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.


Expand|Select|Wrap|Line Numbers
  1. int i; 
  2.     for (int i = 0; i < index_array; i++) 
  3.     { 
  4.         cout << array_num[i]; 
  5.     } 
  6.  
Jul 15 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.