Using an array, problem

This is a discussion on Using an array, problem within the C++ Programming forums, part of the General Programming Boards category; Hey guys, basically I need to extract 2 numbers, a small and large number chosen by using cin to input, ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    79

    Using an array, problem

    Hey guys, basically I need to extract 2 numbers, a small and large number chosen by using cin to input, in an array.

    Example:

    How many integers? 3

    Enter integer #1: 12795
    Enter integer #2: -2784
    Enter integer #3: -27904

    The smallest digit: 0
    Digit 1 can be found in integer number(s): 3

    The largest digit: 9
    Digit 9 can be found in integer number(s): 1, 3

    *Bold indicates an entered number (input)*

    So far this is my code:
    Code:
    int numbers[0];
    	int count;
    	cout << "\tHow many integers do you have? ";
    	cin >> count;
    	cout << "\n";
    	
    	
    	
    	
    	int i;
    	int NumExtract;
    	
    	for(i = 0; i < count; i ++){
    		cout << "\t\tEnter integer #" << i + 1 << " : ";
    		cin >> numbers[i];
    		
    	}
    	
    	for(i = 0; i < count; i ++){
    		while (numbers[i] != 0){
    			
    			if (numbers[i] < 0) {
    				numbers[i] = -numbers[i];
    			}
    			NumExtract = numbers[i] % 10;
    			numbers[i] /= 10;	
    			
    			
    			}
    			
    		
    		
    	}
    This basically gets the numbers to input and everything and extracts the numbers but I'm trying to figure out how to make it so the code knows which Integer line the specified "large/small" number comes from.

    Also, is a for loop good for this sort of problem?

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I am really amazed if it's working..... as you have written int numbers[0] in your code which is a clear thing that you are declaring the array of size 0.....
    So, if you will be able to store the values into the array, then what's so hard to find out that from which integer #, it's coming.....
    Just use the value of i in the last for loop to show the integer #.

    Also, for loop is better as this loop is used when we know the number of iterations... so it's good to use for loop here....

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    I do int numbers[0] to initialize it, then "i" takes over as the number in the array.

    I know how to extract numbers from each integer # but I don't know how to make it print so it says which line it's SPECIFICALLY coming from... I'm not too good with for loops so I'm confused how to do it in this situation.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by .C-Man. View Post
    I do int numbers[0] to initialize it, then "i" takes over as the number in the array.
    What do you really mean?
    I think you just declared an array of size 0 here and nothing else.....
    Do something like
    Code:
    int numbers[3];
    so that it could be the array with size 3...

    Quote Originally Posted by .C-Man. View Post
    I know how to extract numbers from each integer # but I don't know how to make it print so it says which line it's SPECIFICALLY coming from... I'm not too good with for loops so I'm confused how to do it in this situation.
    While using for loop (last one), you are using i as iterator..... so print the value of i where you finding the smallest or largest......

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Code:
    	int count;
    	cout << "\tHow many integers do you have? ";
    	cin >> count;
            int numbers[count];

  6. #6
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    while (numbers[i] != 0){
    			
    			if (numbers[i] < 0) {
    				numbers[i] = -numbers[i];
    			}
    			NumExtract = numbers[i] % 10;
    			numbers[i] /= 10;	
    			
    			
    			}
    What you are doing here is changing the value of array everytime... So, before this loop, first store the value of that array index into some variable and then use that....

    Quote Originally Posted by kargo View Post
    Code:
    	int count;
    	cout << "\tHow many integers do you have? ";
    	cin >> count;
            int numbers[count];
    Wrong.... In C++ you can't do like this..... Unless you know how to create dynamic memory...
    Static arrays are not allowed to use this...

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Quote Originally Posted by Mr.777 View Post
    What do you really mean?
    I think you just declared an array of size 0 here and nothing else.....
    Do something like
    Code:
    int numbers[3];
    so that it could be the array with size 3...
    I mean I initialized it so inside the for loop I could have an unlimited amount of numbers no matter what I enter. Anyway, it works, that's not my problem...

    Quote Originally Posted by Mr.777 View Post
    While using for loop (last one), you are using i as iterator..... so print the value of i where you finding the smallest or largest......
    I don't understand what you mean by this...

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by Mr.777 View Post
    What you are doing here is changing the value of array everytime... So, before this loop, first store the value of that array index into some variable and then use that....



    Wrong.... In C++ you can't do like this..... Unless you know how to create dynamic memory...
    Static arrays are not allowed to use this...
    Read this.....

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Like this?

    Code:
    int numInt;
    
    	
    	for(i = 0; i < count; i ++){
    		cout << "\t\tEnter integer #" << i + 1 << " : ";
    		cin >> numbers[i];
    		
    	}
    	numInt = i + 1;
    	
    	for(i = 0; i < count; i ++){
    		while (numbers[i] != 0){
    			
    			if (numbers[i] < 0) {
    				numbers[i] = -numbers[i];
    			}
    			numExtract = numbers[i] % 10;
    			numbers[i] /= 10;	
    			
    		}
    		
    	}

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    69

  11. #11
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by kargo View Post
    Read this carefully please....

    Code:
    int numbers[3];
    	int count;
    	cout << "\tHow many integers do you have? ";
    	cin >> count;
    	cout << "\n";
    	
    	
    	
    	
    	int i;
    	int NumExtract;
                    int temp;
    	
    	for(i = 0; i < count; i ++){
    		cout << "\t\tEnter integer #" << i + 1 << " : ";
    		cin >> numbers[i];
    		
    	}
    	
    	for(i = 0; i < count; i ++){
                           temp=numbers[i];
    		while (temp!= 0){
    			
    			if (temp < 0) {
    				temp = -temp;
    			}
    			NumExtract =temp % 10;
    			temp /= 10;	
    			
    			
    			}
    			
    		
    		
    	}

    Like this.....
    Now try next...
    Last edited by Mr.777; 03-08-2011 at 07:33 AM.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Ok, I understand this... but how does that help me with printing the right index when there is a number present in the index?

  13. #13
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    The code above is only extracting denominators everytime.....
    Next you have to do this....
    Declare a variable name index in the start first...
    1. In the while loop there should be a conditional statement that will compare the remainder everytime with NumExtract... (Initialize NumExtract with 0 in the beginning where you declared that.)
    2. Now you found the smallest of one index...... Loop through it and try to find the smallest from that unitl the loop ends.....

    Note: Update the index everytime you enter in the conditional statement with the value of i...
    3. In the end, you will get the minimum number of all, so print the number and index as well....

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    I'm really sorry, I'm not that good with C++ terminology can you please explain what you said through my code?

  15. #15
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I helped what i could in your above given code.....

    Run this code and see what more do you need to do....
    Then make a logic.... write the code and then post here in case of errors.....

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 11:00 AM
  2. Sorting array problem :)
    By BEST in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2009, 01:57 PM
  3. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  4. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 12:35 PM
  5. Replies: 6
    Last Post: 02-15-2005, 11:20 PM

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