Like Tree4Likes
  • 2 Post By Tclausex
  • 1 Post By jwall
  • 1 Post By stahta01

Learning C. Need help on this program

This is a discussion on Learning C. Need help on this program within the C Programming forums, part of the General Programming Boards category; Hi: I am learning C and I am having a little trouble with this program. The task was to write ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    68

    Learning C. Need help on this program

    Hi:

    I am learning C and I am having a little trouble with this program.

    The task was to write a program that can determine the largest and smallest integers.

    I am using a while loop to accumulate the numbers
    the value 0 acts as a "sentinel" value that will stop the program.
    The problem is when I enter 0 to stop the program, it will take 0 as the smallest number. This also happens when I do other values like -99 or others. Any help will be greatly appreciated.
    Code:
    //Write a program that reads an integer and determines and prints whether it is od or even
    
    
    #include <stdio.h>
    #include <math.h>
    #include <limits.h>
    
    
    int main(void)
    {
    	int numbers;//numbers
    	int counter = 0;//counter of variables
    	int accumulator = 0;//adds all numbers up
    	int average;//computes average of all the numbers
    	int largest = 0;
    	int smallest = 0;
    	int counting = -99;//value that, when entered, will stop program. 
    	while(numbers != 0)
    	{
    		printf("Please enter a number. Enter 0 to end.\n");
    		scanf("%d", &numbers);
    		accumulator += numbers;
    
    
    		if(numbers == 0)
    			{
    				printf("Thank you.\n");;
    				if(numbers < smallest && numbers !=0 );
    				{
    					numbers = smallest;
    				}//end if
    			}//end if
    		else
    			{
    				counter++;
    			}//end if
    		if(numbers % 2 == 0 && numbers != 0)
    			{
    				printf("%d is an even number.\n", numbers);
    			}//end if
    		else if(numbers % 2 != 0 && numbers != 0)
    			{
    				printf("%d is an odd number.\n", numbers);
    			}//end else
    	
    		if(numbers < smallest && numbers != 0)
    		{
    			smallest = numbers;
    		}//end if
    
    
    		if(numbers > largest && numbers != 0)
    		{
    			largest = numbers;
    		}//end if
    	}//end while
    	average = accumulator/counter;
    	if(counter > 2)
    		{
    			printf("User made %d entries\n", counter);
    		}//end if
    	else
    		{
    			printf("User made 1 entry.\n");
    		}//end else
    		
    	printf("Total of all entries: %d\n", accumulator);
    	printf("Average: %d\n", average);
    	printf("Largest number: %d\n ", largest);
    	printf("Smallest number: %d\n ", smallest);
    
    
    }//end main

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    There is an extra semicolon here, after the if
    Code:
    if(numbers < smallest && numbers !=0 );
                    {
                        numbers = smallest;
                    }//end if
    Remove it.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You are reading in the number, processing it, then returning to the top of the while loop to test the input. So yes, 0 is being treated as an input.

    The easiest solution at a beginning level would be to prompt and read the number before entering the while loop, and then again at the bottom of the loop.
    Not an elegant approach since your duplicating code, but I'm betting you haven't been introduced to functions or the comma operator yet.

    BTW. When you execute
    Code:
    while(numbers != 0)
    numbers is uninitialized and therefore you have no idea what value it holds. If it happens to be zero, you would never enter the loop at all. So, either initialize it to your sentinel value when you declare it, or insure some assignment prior to testing it.
    slain and Crossfire like this.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Fixed it. Thanks for the help.
    Crossfire likes this.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    but is there a way to use a non-integer as a sentinel value?

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    You mean as an initializing value? Yes, there is.

    Example. Ask the user to input a number until he inputs -5. (not so clever example, but ok).
    Code:
    int init = -5;
    int input;
    
    do{
       printf("Please input\n");
       scanf("%d", %input);
    }while(input != init);
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,610
    Quote Originally Posted by jwall View Post
    but is there a way to use a non-integer as a sentinel value?
    Lookup the meaning of the return value of scanf. You might be able to use it.

    Tim S.
    Tclausex likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    The short answer: no.

    By definition a sentinel is some value in the range of the datatype being used which does not represent a valid data value. So when you're determining if a sentinel is even the right approach, you have to look at what your valid data range is and can you accommodate that AND have values outside that range which would be sentinel values. E.g., if valid data is all integers from 0 to 32,000. Then a signed integer (assuming 4 bytes) would support your data range and any negative value would be a sentinel. If however, you require acceptance of negative values, you are more limited (you might use INT_MIN if you're glued to the idea of a sentinel).

    If you mean 'is there some other data type?', then no as well. Integer and floating point are your two intrinsic types to use (at least I can't imagine using any derived type), and FP isn't any good for checking equality due to precision.

    Another approach would be to read user input until they signal end-of-file [EOF](ctrl-z or similar depending on host environment). That would bypass the whole notion of sentinel.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Quote Originally Posted by Tclausex View Post
    The short answer: no.

    By definition a sentinel is some value in the range of the datatype being used which does not represent a valid data value. So when you're determining if a sentinel is even the right approach, you have to look at what your valid data range is and can you accommodate that AND have values outside that range which would be sentinel values. E.g., if valid data is all integers from 0 to 32,000. Then a signed integer (assuming 4 bytes) would support your data range and any negative value would be a sentinel. If however, you require acceptance of negative values, you are more limited (you might use INT_MIN if you're glued to the idea of a sentinel).

    If you mean 'is there some other data type?', then no as well. Integer and floating point are your two intrinsic types to use (at least I can't imagine using any derived type), and FP isn't any good for checking equality due to precision.

    Another approach would be to read user input until they signal end-of-file [EOF](ctrl-z or similar depending on host environment). That would bypass the whole notion of sentinel.

    How would I do that here?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by jwall View Post
    How would I do that here?
    If I handed you five apples, but said "ignore the last one, it's no good", how would you handle that?

    Same logic is needed here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is learning how to program in mode 13h still worth it?
    By thefeedinghand in forum Game Programming
    Replies: 2
    Last Post: 08-02-2010, 08:44 PM
  2. Learning C++, needing help with a program
    By Wilfenite in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2008, 09:39 AM
  3. Learning how to program
    By Tesnik in forum C++ Programming
    Replies: 2
    Last Post: 09-07-2007, 07:33 PM
  4. reinforcement learning c program
    By Temden in forum C Programming
    Replies: 1
    Last Post: 05-10-2006, 07:27 AM
  5. Q-Learning C program help
    By Temden in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2006, 04:56 AM

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