Thread: Problem solving with repetition

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    3

    Problem solving with repetition

    Hello all, I am having a bit of trouble figuring out the solution to this:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    /*
    You are to write a program that will allow a user to enter infinite numbers (greater than zero)
    (One number at a time).  You must have a way for the user to stop entering values.
    Once the user stops, you will immediately display the following:
    
    
    The lowest number entered is:
    The highest number entered is:
    The number of values entered is:
    The average of the numbers entered is:
    */
    
    
    main() {
    
    
    	int userNumber=0, sum=0, count=0, highNum=0, lowNum=0, lastNum = 0;
    	double average;
    
    
    	printf("Enter a number greater than 0: (Enter -1 to stop) \n");
    	scanf_s("%i", &userNumber);
    
    
    	while (userNumber != -1){
    		if (userNumber > lowNum && userNumber > highNum)
    			highNum = userNumber;
    		count = count + 1;
    		sum = sum + userNumber;
    		printf("Enter a number:(-1 to stop)\n");
    		scanf_s("%i", &userNumber);
    	}
    	average = (double)sum / count;
    
    
    	printf("\t The sum is: %i\n", sum);
    	printf("\t %i numbers were input\n", count);
    	printf("\t The average is %.2lf\n", average);
    	printf("The highest number entered was: %i", highNum);
    	printf("The last number entered was: %i", lastNum);
    	printf("The lowest number entered was: %i", lowNum);
    	system("pause");
    }

    My program outputs everything I need except the lowest number entered. I have been spending a considerable amount of time on this problem to no avail. Please help me out here fellow programmers! This is for a school project due tomorrow. More important than the homework, however, is the fact that I truly want to figure this out. I'm a new programmer but I plan to make this my career so I am extremely hungry for knowledge!! Thanks in advance to you awesome helpers, guru's and mentors

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    You don't set the value of 'lowNum' any where in your code; well, apart from initializing its value to zero.

    You need to have something like this in your code:

    Code:
    if (userNumber > lowNum && userNumber > highNum)   
             highNum = userNumber;
    else if (userNumber < lowNum)
                lowNum = userNumber;

  3. #3
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Quote Originally Posted by Aslaville View Post
    You don't set the value of 'lowNum' any where in your code; well, apart from initializing its value to zero.

    You need to have something like this in your code:

    Code:
    if (userNumber > lowNum && userNumber > highNum)   
             highNum = userNumber;
    else if (userNumber < lowNum)
                lowNum = userNumber;
    The problem here is that the OP initialises lowNum to 0. This means that no number entered by the user is smaller than lowNum.

    @Cheyne: You need to initialise lowNum to the largest value it can hold. The header file limits.h should define something like INT_MAX, which is the largest value an int variable can hold.

    Cheers.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    @Cheyne: You need to initialise lowNum to the largest value it can hold.
    Not really, you can use the first value entered to set the low and high values.

    Code:
    ...
        // The first scanf() function.
        printf("Enter a number greater than 0: (Enter -1 to stop) \n");
        scanf_s("%i", &userNumber);
        highNum = userNumber;
        lowNum = userNumber;
    
    ...
    Jim

  5. #5
    Registered User talahin's Avatar
    Join Date
    Feb 2015
    Posts
    51
    Jim, you're correct. That's an option I hadn't thought about.

    And now for the next question. What happens when a user enters -1 as the first value.

    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-19-2014, 04:06 AM
  2. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  3. solving a problem of c
    By aunogarafat in forum C Programming
    Replies: 1
    Last Post: 11-07-2012, 11:03 AM
  4. Simplifying Numerical Solving before actual solving
    By Vespasian in forum Tech Board
    Replies: 3
    Last Post: 10-14-2012, 11:39 AM
  5. Help with solving this problem
    By Neox in forum C++ Programming
    Replies: 5
    Last Post: 11-20-2010, 03:40 PM

Tags for this Thread