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

Printing out integers in an array only once

P: 43
I am wring a program where you accept a sequence of integers as input and then print every integer exactly once...you can assume there are no more than 20 integers in the input bt there may be less. Zero ends the input. It shouldn't be printed though
ex: user enters 8 5 22 3 5 9 3
program shoul print : 8 5 22 3 9

Heres what i got so far:

#include <iostream>
using namespace std;
int main()
{
// Declare array and number of elements in it.
int array[20];
int i =0, length;
// Instructing user to enter in a series of numbers'
cout << "Please enter in a series of numbers ending with a 0." << endl;
do
{
cin >> array[i];
i++;
}
while(array[i-1] != 0 && i < 20);
}

All that does is collect the input and now I need to figure out a way to cout all the numbers once and I can't figure out how to do that I've been looking online for hours...any help would be greatly appreciated. Thanks
Nov 4 '06 #1
Share this Question
Share on Google+
4 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
I am wring a program where you accept a sequence of integers as input and then print every integer exactly once...you can assume there are no more than 20 integers in the input bt there may be less. Zero ends the input. It shouldn't be printed though
ex: user enters 8 5 22 3 5 9 3
program shoul print : 8 5 22 3 9

Heres what i got so far:

#include <iostream>
using namespace std;
int main()
{
// Declare array and number of elements in it.
int array[20];
int i =0, length;
// Instructing user to enter in a series of numbers'
cout << "Please enter in a series of numbers ending with a 0." << endl;
do
{
cin >> array[i];
i++;
}
while(array[i-1] != 0 && i < 20);
}

All that does is collect the input and now I need to figure out a way to cout all the numbers once and I can't figure out how to do that I've been looking online for hours...any help would be greatly appreciated. Thanks
Looks to me like you already figured it out - just do the same thing while cout'ing. The only caveat I might mention is that (if I remember correctly - you might want to write the program without the if-else, then try a first-element 0 to check this) do-while loops do everything one time, and then check the condition. So my pseudocode would be:

if array0 != 0
do
cout array
array++
while array!=0 && array<20
else
cout 'you didn't put anything in the array!'
Nov 4 '06 #2

P: 43
Looks to me like you already figured it out - just do the same thing while cout'ing. The only caveat I might mention is that (if I remember correctly - you might want to write the program without the if-else, then try a first-element 0 to check this) do-while loops do everything one time, and then check the condition. So my pseudocode would be:

if array0 != 0
do
cout array
array++
while array!=0 && array<20
else
cout 'you didn't put anything in the array!'
How will that output the integers only once though...wouldn't that output all the integers in the area even the repeats???
Nov 4 '06 #3

Ganon11
Expert 2.5K+
P: 3,652
Hmm...sounds like the best option here would be to use a set, though I'm not sure if you're supposed to be using advanced data structures yet (nor have I come across the C++ code to implement sets).
Nov 4 '06 #4

sicarie
Expert Mod 2.5K+
P: 4,677
Whoops, sorry, I missed the "only print them once" part.

I would try to do that while accepting integers in - just stick another loop in there that iterates through the current array and doesn't add to it, but still increments the index, as you are only supposed to get 20.

Expand|Select|Wrap|Line Numbers
  1.  
  2. for (int i=0;i<20;i++) {
  3. cin << intVal;
  4. for(int j = 0; j<20;j++) {
  5. if (array[j] == intVal ) {
  6. // do nothing
  7. }
  8. else
  9. array[i] = intVal;
  10. }
  11. }
  12. }
  13.  
Or something like that...
Nov 4 '06 #5

Post your reply

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