Issue with printing a varying array

This is a discussion on Issue with printing a varying array within the C++ Programming forums, part of the General Programming Boards category; Hey all, I got a quick question on a piece of code I am working on. It's a large program ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    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) {
    	numberToAdd = x;
    	sizeOfBag = i;
    	int j;
    	int addIt;	
    	for(x=0; x<i; x++) {
    		cin >> addIt;
    		if(addIt >= 0 && addIt <= 40) {
    			bag[addIt]++;
    			store[j] = addIt;
    			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.

    Any advice?
    Last edited by monki000; 04-19-2010 at 04:08 PM. Reason: Sorry for the crazy spacing, I attempted to make it more clear

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    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. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Stop when you get to the end, instead of when you come across a zero. (Presumably you know how big your arrays are.)

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    IF the count is zero, don't print them. (Continue the loop, but don't print anything.)

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    ah, i didn't think about a nested loop to apply more conditions! great help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing an array of structs?
    By blernblan in forum C Programming
    Replies: 4
    Last Post: 04-28-2009, 04:04 PM
  2. printing array content - spot the error
    By nosnowking in forum C Programming
    Replies: 5
    Last Post: 02-04-2009, 05:50 PM
  3. Small problem with printing elements of an array
    By DLR in forum C Programming
    Replies: 17
    Last Post: 03-09-2006, 06:57 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21