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; I misunderstood my assignment earlier. I now understand for the part with : The smallest digit: 0 Digit 1 can ...

  1. #16
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    I misunderstood my assignment earlier. I now understand for the part with :

    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

    that the code was supposed to extract the largest and smallest digit. I did that in my code now:

    Code:
    for(i = 0; i < count; i ++){
    		temp = numbers[i];
    		while (temp!= 0){
    			
    			if (temp < 0) {
    				temp = -temp;
    			}
    			numExtract = temp % 10;
    			temp /= 10;
    		
    			if( numExtract > largeDigit ){
    				largeDigit = numExtract;
    				
    			}
    			if( numExtract < smallDigit ){
    				smallDigit = numExtract;
    			}
    			
    			
    		}
    		
    		
    		
    	}
    	cout << "\n";
    	cout << "The smallest digit: " << smallDigit << endl;
    	cout << "The largest digit: " << largeDigit << endl;
    The only thing I need to do now is print out which lines have the smallDigit and largeDigit on them.

    Can anyone please help me? I'm stuck and I don't know what to do. How do I find what line (index) smallDigit and largeDigit are on?? I will appreciate ANY help.
    Last edited by .C-Man.; 03-08-2011 at 07:33 AM.

  2. #17
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Introduce two variables in the start of your program....
    Code:
    int index_Lower=0;
    int index_Larger=0;
    inside if's(), assign the value of i to these and you will get your answer....
    Print the value of these two variables in the end....
    Good luck...

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    Again, don't hand out solutions to the logic problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I just provided him with algo and not the code...
    Anyways, i don't think that this simple solution is that tough for him/her to think.... May be he/she is in wrong direction so that was just to put him/her in the right direction....

  5. #20
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Introduce two variables in the start of your program....
    Code:
    int index_Lower=0;
    int index_Larger=0;
    inside if's(), assign the value of i to these and you will get your answer....
    Print the value of these two variables in the end....
    Good luck...
    Thanks, I found that out, but can I still apply this if the number is printed out on multiple indexes?

  6. #21
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,662
    I'm pretty sure that part of the homework involves two separate arrays.

  7. #22
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Do you think using a pointer would help in this situation?

  8. #23
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,662
    *shrug* Notice how a digit can appear in more than one number in the list. That means in order to display all the places such a digit occurs in the list, you may also need to efficiently store that information. Pointers don't really mean anything here, unless they point to dynamic allocations or something.

  9. #24
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    I don't get how it can print multiple indexes that smallDigit and largeDigit are on, with one int.

    This is my code:
    Code:
    int numbers[10];
    	int count;
    	int smallDigit = 9;
    	int largeDigit = 0;
    	
    	cout << "\tHow many integers? ";
    	cin >> count;
    	cout << "\n";
    	
    	
    	
    	
    	int i;
    	int numExtract;
    	int temp;
    	int indexSmall;
    	int indexLarge;
    
    	
    	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){
    			cout << i << endl;
    			if (temp < 0) {
    				temp = -temp;
    			}
    			numExtract = temp % 10;
    			temp /= 10;
    		
    			if( numExtract > largeDigit ){
    				largeDigit = numExtract;
    				indexLarge = i + 1;
    			}
    			if( numExtract < smallDigit ){
    				smallDigit = numExtract;
    				indexSmall = i + 1;
    
    			}
    			
    			
    		}
    		
    			
    			
    			
    		
    	}
    	cout << "\n";
    	cout << "The smallest digit: " << smallDigit << endl;
    	cout << "Digit " << smallDigit << " can be found in integer number(s): " << indexSmall << endl;
    	cout << "The largest digit: " << largeDigit << endl;
    	cout << "Digit " << largeDigit << " can be found in integer number(s): " << indexLarge << endl;

  10. #25
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    int numbers[10];
    	int count;
    	int smallDigit = 9;
    	int largeDigit = 0;
    	
    	cout << "\tHow many integers? ";
    	cin >> count;
    	cout << "\n";
    	
    	
    	
    	
    	int i;
    	int numExtract;
    	int temp;
    	int indexSmall;
    	int indexLarge;
    
    	
    	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){
    			cout << i << endl;
    			if (temp < 0) {
    				temp = -temp;
    			}
    			numExtract = temp % 10;
    			temp /= 10;
    		
    			if( numExtract > largeDigit ){
    				largeDigit = numExtract;
    				indexLarge = i + 1;
    			}
    			if( numExtract < smallDigit ){
    				smallDigit = numExtract;
    				indexSmall = i + 1;
    
    			}
    			
    			
    		}
    	cout << "\n";
    	cout << "The smallest digit: " << smallDigit << endl;
    	cout << "Digit " << smallDigit << " can be found in integer number(s): " << indexSmall << endl;
    	cout << "The largest digit: " << largeDigit << endl;
    	cout << "Digit " << largeDigit << " can be found in integer number(s): " << indexLarge << endl;
    		
    			
    			
    		
    	}
    Try this.....

    This'll print smallest and largest digit and their indexes each time it completes comparisons with the inner loop.....
    Now, you can think to make it much better....

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

Similar Threads

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

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