Thread: newbie programmer - needs help bad.

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    9

    Question newbie programmer - needs help bad.

    Ok, I am an MIS major and am taking my first programming class. This is a "simple" program that we have been assigned and I can not afford to lose much more hair. Hopefully it is ok that I post all of this but these are the instructions and the program that I have written so far. Please, any help is great!!

    Instructions:
    Write a program in C that will use a sentinel controlled loop to allow the user to input as many single digit integers (0,1,2,3,4,5,6,7,8,or 9) as wished. When the user enters –1 to end input, the program will print the smallest digit entered (other than –1), the largest digit entered, and the sum of the digits entered(other than –1). The screen should look something like the following:

    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): 3
    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): 6
    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): 5
    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): 7
    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): -1

    The smallest digit entered was : 3
    The largest digit entered was : 7
    The total of the digits entered was: 21

    Goodbye!!

    Or

    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): 6
    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): 6
    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): 6
    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): 6
    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): 6
    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): -1

    The smallest digit entered was : 6
    The largest digit entered was : 6
    The total of the digits entered was: 30

    Goodbye!!


    Note: If the user enters –1 as the first entry (i.e. no digits from 0 to 9 were entered), the screen should look something like:

    Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): -1

    No digits were entered!

    Goodbye!!

    This is what I have and where I am stuck:
    Code:
     #include <stdio.h>
    
    int main()
    {
    	int total;
    	int smallest;
    	int largest;
    	int counter;
        int integer;
    	
    
    	total = 0;
    	counter = 0;
    
    	printf("Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): ");
    	scanf("%d", &integer);
    
    	while ( integer != -1) { 
    		total = total + integer;
    		counter = counter + 1;
    
    
    		printf("Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): ");
    	    scanf("%d", &integer );
    	}
    
    	largest = integer;
    	smallest = integer;
    
        if( integer > largest) 
    		largest = integer;
    	
    
    	if(integer < smallest) 
    		smallest = integer;
    	   
    	
    	while ( counter = -1){
    		printf("No digits were entered\nGoodbye!!\n");
    	}
    	
        
        printf( "The smallest digit entered was %d\n", smallest );
    	
    	printf( "The largest digit entered was %d\n", largest );
    	
    	printf( "The total of the digits entered was %d\n", total );
    	
    	printf( "Goodbye!!\n" );
        
    	
    	
    
    	return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shouldn't you initialize your high and low values so that when you try to compare them to something it actually works? For example, your "high number" should first be initialized to a low number, so the first time you compare against it, the assignment puts the first value entered as the new high number. The opposite is true for the low number.

    You also need to completely rethink your loop. You need all of the checks in a single loop. Not what you have.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    Thank you for your response. I am working on it now.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    so,
    Code:
    highest = 0
    smallest = 9
    ?? I don't mean to be stupid, I'm just lost.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by hortonheat
    so,
    Code:
    highest = 0
    smallest = 9
    ?? I don't mean to be stupid, I'm just lost.
    That would work, acordingly to your objective.
    But you should place the comparisions wherelse.

    And you should then check if the inputed number is within your range. scanf can read almost any integer.

    And take a good look at
    Code:
    while ( counter = -1){
    printf("No digits were entered\nGoodbye!!\n");
    }
    Try to understand what would happen here.

  6. #6
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Your if () statments need to be inside your input loop. Otherwise all they'll ever compare is the final integer entered.

    Also your while ( counter = -1) will never be executed. Well, something will be executed. Just not what you are expecting.

    You really need to rethink your logic. Try flowcharting or pseudocoding it and walk it step by step.
    Last edited by Scribbler; 10-18-2004 at 12:20 AM.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int total;
    	int smallest;
    	int largest;
    	int counter;
        int integer;
    	
    	total = 0;
    	counter = 0;
    	largest = 0;
    	smallest = 9;
        
    
    	printf("Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): ");
    	scanf("%d", &integer);
    
    	while( integer >=0)
    	{ 
    		total = total + integer;
            counter = counter + 1;
            
            if (integer >= largest){
    			integer = largest;
    			if (integer <= smallest){
    			integer = smallest;
    
    		printf("Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): ");
    	    scanf("%d", &integer );
    
    		printf( "The smallest digit entered was %d\n", smallest );
    		printf( "The largest digit entered was %d\n", largest );
            printf( "The total of the digits entered was %d\n", total );
    	    printf( "Goodbye!!\n" ); 
    		else 
    			printf("No digits were entered\n\n\nGoodbye!!\n");
    			}
    
    		}
    
    	
    	}
        
        
        
    	
    	
    
    	return 0;
    }
    is this getting better?

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    I've cleaned up your loop's indentation so it's easier to visually see what's happening. See if you can spot what's going to happen.
    PHP Code:
        while( integer >=0)
        { 
            
    total total integer;
            
    counter counter 1;
            
            if (
    integer >= largest)
            {
                
    integer largest;
                if (
    integer <= smallest)
                {
                    
    integer smallest;
                    
    printf("Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): ");
                    
    scanf("%d", &integer );
                    
    printf"The smallest digit entered was %d\n"smallest );
                    
    printf"The largest digit entered was %d\n"largest );
                    
    printf"The total of the digits entered was %d\n"total );
                    
    printf"Goodbye!!\n" ); 
                else 
                    
    printf("No digits were entered\n\n\nGoodbye!!\n");
                }
            }
        } 
    Last edited by Scribbler; 10-18-2004 at 12:53 AM.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    that seems right to me.....

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, it's not:
    Code:
    if (integer >= largest)
    {
        integer = largest;
        if (integer <= smallest)
    The only way smallest will get updated is if it is both larger than largest, AND smaller than smallest (or equal to both, in which case, see below) Probably not going to happen, hmm?

    On an aside, there is no point in testing either of those for equality. Only update if it's bigger or smaller, not bigger-or-the-same and smaller-or-the-same. Because why bother updating if it's the same?

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    so I need to give parameters for maximum and minimum?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. You just actually need to pay attention to what's going on, and rethink how you do it.
    Code:
    initialize:
        number is whatever
        low is highest number allowed
        high is lowest number allowed
    while number is not -1
        prompt for a number
        if number is not -1
            if number lower than low
                set low to number
            if number higher than high
                set high to number
    See? It helps if you do something like that (we call it pseudocode). All it is is writing out the logic or steps of what you want to do. Then after you've done that, you turn it into actual code.

    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int total;
    	int smallest;
    	int largest;
    	int counter;
        int integer;
    	
    	total = 0;
    	counter = 0;
    	largest = 0;
    	smallest = 9;
        
    
    	printf("Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): ");
    	scanf("%d", &integer);
    
    	while( integer >=0) 
        { 
            total = total + integer; 
            counter = counter + 1;
             
            if (integer > largest) 
            { 
                largest = integer; 
                if (integer < smallest) 
                { 
                    smallest = integer;
                    printf("Enter a one digit integer (0,1,2,3,.,8,9 or -1 to quit): "); 
                    scanf("%d", &integer ); 
                    printf( "The smallest digit entered was %d\n", smallest ); 
                    printf( "The largest digit entered was %d\n", largest ); 
                    printf( "The total of the digits entered was %d\n", total ); 
                    printf( "Goodbye!!\n" ); 
                else 
                    printf("No digits were entered\n\n\nGoodbye!!\n"); 
                } 
            } 
        } 
    
    	return 0;
    }
    ???????????????

  14. #14
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    nevermind...I copied wrong one

  15. #15
    Registered User
    Join Date
    Oct 2004
    Posts
    9
    no, I didn't...I'm delerious....I didn't know if I'd have to have and integer1 and integer2...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poker bad beats
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-15-2005, 11:42 PM
  2. How bad is bad
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 11-12-2004, 09:26 AM
  3. If you are employed as a programmer, please look
    By Flood Fighter in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-28-2004, 02:35 AM
  4. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM
  5. good news and bad news
    By Garfield in forum A Brief History of Cprogramming.com
    Replies: 25
    Last Post: 10-27-2001, 07:31 AM