445,804 Members | 1,627 Online
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 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
4 Replies

 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 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

 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

 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   for (int i=0;i<20;i++) { cin << intVal; for(int j = 0; j<20;j++) { if (array[j] == intVal ) { // do nothing } else array[i] = intVal; } } }   Or something like that... Nov 4 '06 #5