Thread: smallest int problem

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    23

    smallest int problem

    I'm to write a program that finds the smallest of several intagers. the first vaulue read specifies the number if values remaing for caculation.

    here is my code that seems to work put gives me a long negitive number everytime.
    Code:
    #include <stdio.h>
    
    int main() // start execution
    {
    	int enter; // the number that determains  specific vaulues remaining
    	int counter = 1; // counter used in while
    	int number ; // numbers to be entered
    	int little; // saves the littlest ineger
    	int compare; // used to compare numbers
    
    	printf ("Please Enter how many sequences:\n");
    	scanf  ("%d", &enter);
    
    	while( counter <= enter ) // numbers to be entered until = enter
    	{
    		printf ("Please enter a  positive digit:\n");
    		scanf  ("%d", &number);
    
    		number = compare;	
    	 
         if (number <= compare){ // if stores the littlest int
    			little = number;
    				
    		} // ends if
    		counter++; // counter addition
    			
    		
    	} //ends while
    
    	printf ("Our smallest number is %d.\n", little);
    
    	return 0; // program ended correctly
    } // end main
    thanks,
    joe

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > number = compare;
    What does this do to the following comparison?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code:
    		number = compare;	
    	 
         if (number <= compare){ // if stores the littlest int
    			little = number;
    				
    		} // ends if
    		counter++; // counter addition
    			
    		
    	} //ends while
    Here you set number to compare each time. Your compare number is unitialized, thus is garbage. You'll need to do something like have the user enter one number outside the loop to set as compare. Or, you could set compare to the max int 0x7fffffff.

    EDIT: Bummer, beaten.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    i'm sorry i meant to type
    Code:
    compare = number;
    hope that makes sense now
    but i've been known to be way off before. lol

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    But that is still wrong.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    ok but how would i go about setting anumber outsdie this loop to be entered

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i'm sorry i meant to type
    Here's a tip - paste the last thing you ran, directly from your source code.
    Not some half-assed "I think this is something like what I tried some time ago".

    Accurate answers demand accurate information.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    salem i did change it and it aways give the answer of the last number i entered

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Code:
    printf("Please enter a positive digit:\n");
    scanf("%d", &compare);
    enter--;
    	while( counter <= enter ) // numbers to be entered until = enter
    	{
    		printf ("Please enter a  positive digit:\n");
    		scanf  ("%d", &number);

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    ok here i tried new code
    Code:
    #include <stdio.h>
    
    int main() // start execution
    {
    	int enter; // the number that determains  specific vaulues remaining
    	int counter = 1; // counter used in while
    	int number ; // numbers to be entered
    	int little; // saves the littlest ineger
    	int compare; // used to compare numbers
    
    	printf ("Please Enter how many sequences:\n");
    	scanf  ("%d", &enter);
    
    	printf("Please enter a positive digit:\n");
        scanf("%d", &compare);
        enter--;
    
    	while( counter <= enter ) // numbers to be entered until = enter
    	{
    		printf ("Please enter a  positive digit:\n");
    		scanf  ("%d", &number);
    
    		compare = number;	
    	 
         if (number <= compare){ // if stores the littlest int
    			little = number;
    				
    		} // ends if
    		counter++; // counter addition
    			
    		
    	} //ends while
    
    	printf ("Our smallest number is %d.\n", little);
    
    	return 0; // program ended correctly
    } // end main
    i still get the amswer of whatever number i last promted to enter

    thank you all so far for your help

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    And you still aren't reading the posts that Salem and I made LONG AGO: (spoon feeding time)

    Code:
    	while( counter <= enter ) // numbers to be entered until = enter
    	{
    		printf ("Please enter a  positive digit:\n");
    		scanf  ("%d", &number);
    
    		compare = number;
    Quote Originally Posted by Almost Salem
    What does this code do?

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    i see but even if i remove this from the code and then recompile it it still hase the effect of saying
    Code:
    Our smallest number is "whatever the last number i entered"
    
    so if i tell it 3 on sequence then
    lets say first promt i enter 9 
    second i enter 1 
    and third i enter 5 
    five will be the answer as the smallest number
    i need to know why its alway printing the last nember i entered as the answer

  13. #13
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Remove little from your source. Then, in the if statement, set compare in place of little.

  14. #14
    Registered User
    Join Date
    Nov 2006
    Posts
    23
    thank you thank you thank you kennedy and salem
    i guess i over thought this one and had to many int's lol
    and had everything equaling
    joe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. very weird .h problem
    By royuco77 in forum C++ Programming
    Replies: 1
    Last Post: 09-11-2005, 07:55 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM