Thread: Issue with printing a varying array

1. Issue with printing a varying array

Hey all, I got a quick question on a piece of code I am working on. It's a large program so i'll only post relevant snippets(which will be functions).

First I prompt the user to declare and array size and fill it with numbers(0-40). I use two array to keep track of their input, one holds the actual values they input and the other is a counter for the number of occurrences of each number
Code:
```	void add(int x,int i) {
sizeOfBag = i;
int j;
for(x=0; x<i; x++) {
j++;
}
else {
cout << "Number must be between 0-40" << endl;
}
}
};```
I think list the numbers they they input by comparing the number of occurances to 0
Code:
```void list() {
int i=0;
for (i=0; bag[store[i]]!=0; i++) {
cout << store[i] << endl;
}
};```
3rd i let them remove a certain number of element
Code:
```void remove(int x, int i) {
numberToRemove = x;
sizeOfRemove = i;
int removeIt;
for(x=0; x<i; x++) {
cin >> removeIt;
if(removeIt >= 0 && removeIt <= 40) {
bag[removeIt]--;

}
else {
cout << "Number must be between 0-40" << endl;
}
}
};
};```
I then repeat the list function. Here's where the problem lies. if they input 1,2,3,4,5 into the array; then remove 3; my output shows only 1,2 opposed to 1,2,4,5.

2. If you had started by entering just 1, 2, 4, 5, would your program print all those numbers? I bet the answer is no. Your for loop stops as soon as there's a missing number, irrespective of where the end of the data actually is.

3. Thanks for the quick reply tabstop. Actually, The first time i run list I get 1,2,3,4,5. It is only after the remove that I have the issue. It works the first time because I ran a loop(not shown) to fill bag[i] with 0. Then use bag[i] as a counter. Therefore all numbers that were input will case bag[input] to increment, making it !=0. In my brain it should work just the same afterwards, but it does not.

EDIT*
In addition to that, if they chose to remove 0 numbers, the list is still correct. So something in my remove function is messing everything up.

4. What I'm saying is don't start with 1, 2, 3, 4, 5. Start with 1, 2, 4, 5 instead (are you typing in the input? Just don't type in 3.) Then see what happens.

5. yeah Im typing the input, I created several cin calls in main. It works the first time no matter what. Look at this run.
Code:
```How many numbers would you like to put into Bag D?: 5
Input 5 numbers into Bag D:
1
3
5
7
9
The numbers in Bag D are:
1
3
5
7
9

How many numbers would you like to remove from Bag D?:
1

Input 1 numbers to remove:
5

The numbers in Bag D are now:
1
3```

6. Okay, I wasn't clear on what you were telling me.

You have this after removal:
Code:
```1   1
3   1
5   0
7   1
9   1```
Do you not see the big 0 in the middle of your data? Do you see how that will cause your for loop to stop? Either you need to move every number back down when a number goes completely away (to fill in the hole), or you need to not stop your for-loop prematurely.

7. exactly, you got it. Sorry for my poor explanation... My thinking was I need to modify the conditions for the list loop, but I cannot think of a way to do it. So that was really my original question haha.

8. Stop when you get to the end, instead of when you come across a zero. (Presumably you know how big your arrays are.)

9. well i do know how big the array is, the issue with that method would mean Id have a bunch of 0's though.. correct? say if the array size is 10 and they input 1,2,3,4,5. the output would be 1,2,3,4,5,0,0,0,0,0,. I do not want that. Is there a way around that?

10. IF the count is zero, don't print them. (Continue the loop, but don't print anything.)

11. ah, i didn't think about a nested loop to apply more conditions! great help!