Thread: while loop

  1. #46
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Quote Originally Posted by andyouf View Post
    Thank you that is very helpful!! My only question then is why does this code work in not summing or averaging the 99999 for me when I use "[FONT=arial]if (number!=99999)" above sum equation.
    You are always incrementing "loop_count". You need to place the loop_count increment statement inside the "if" code block along with the sum calculation:

    Code:
    if (number!=99999)
    {   sum = sum + number;
        loop_count++;
    }
    Now you might possibly have a "loop_count" value of zero, if no valid values are entered. Zero is a valid value for "loop_count", since it represents the number of valid values entered (you don't want to count 99999).

    Quote Originally Posted by andyouf View Post
    Also, could I use one of your if stmt examples to make it so that if the user opens the program and enters no input (ie 99999) the program returns "no valid input entered."
    You can now use "loop_count" in an "if else" statement after the loop has ended:

    Code:
    if(loop_count == 0) {
    
        /* print "no valid input entered." message */
    
    } else {
    
        /* calculate average and print result, sum, etc */
    }
    -

  2. #47
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    Now if I use a function, say "get_smallest" to get smallest entered or "number" to get the integer entered will it automatically refer back to this loop in main that I'm building?

    I think you sort of answered this when you said I'm always incrementing the loop_count throughout, thank you.

  3. #48
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    My professor wants getNumber to be a function that takes in an address parameter so that the integer entered in keyboard is picked up. I have that down but I don't see how to make this a separate function outside of main? He also wants me to do this with smallest integer entered. How do I declare that so that it refers to my loop?
    Code:
    /* Prompts user and gets integer numbers from keyboard, one number at a time. Ends program when 99999 entered. and displays various results.
            Written by:
            Date:       10/20/14
    */
    #include <stdio.h>
    #include <stdlib.h>
    
    
    //function declarations
    
    
    int main()
    {
        int getNumber;
        int average;
        int sum = 0;
        int loop_count = 0;
    
    
    
    
        do {
            printf("Enter next integer:");
            loop_count++;
    
    
           scanf("%d", &getNumber);   /* read a single integer value input */
            if (getNumber!=99999)
            sum = sum + getNumber;
        } while (getNumber != 99999);
    
    
        average = sum / loop_count;
    
    
        printf("Number of Integers: %d\n", loop_count);
        printf("Average: %d\n", average);
        printf("The Sum of the Number Is: %d", sum);
    }

  4. #49
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    If I understand, you need a function named getNumber, and need to pass the address of a variable to the function to store the result in?

    Is the function supposed to return any value? (the number entered would be assigned to the variable who's address was passed in as an argument)
    But is the function call itself supposed to return a value, for example:
    Code:
    n = getNumber(&number);


    Also, i already started answering your last question, so in case this helps any -

    Quote Originally Posted by andyouf View Post
    Now if I use a function, say "get_smallest" to get smallest entered or "number" to get the integer entered will it automatically

    refer back to this loop in main that I'm building?
    I think you sort of answered this when you said I'm always incrementing the loop_count throughout, thank you.
    What does your main loop look like now?

    In the versions so far, none of the individual values are being saved; so it's not possible to go back and test them for something like "smallest value". You would have to find that value within the loop, while the numbers were being input.

    A simple "if" statement can handle this.
    Writing the actual code is little different than explaining it, so:

    Code:
    int minimum = 99999;    // this is before main loop
    
    if (number!=99999) {
    
        sum = sum + number;
        loop_count++;
    
        if(number < minimum)    // this tests new number against the current minimum
            minimum = number;    // if new number is less, it becomes the new minimum
    }
    -
    Last edited by megafiddle; 10-22-2014 at 09:39 PM.

  5. #50
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    getNumber shld just take one address parameter so that integer from keyboard is picked up...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    //function declarations
    
    
    int main()
    {
        int getNumber;
        int average;
        int sum = 0;
        int loop_count = 0;
    
    
    
    
        do {
            printf("Enter next integer:");
            loop_count++;
            scanf("%d", &getNumber);   /* read a single integer value input */
            if (getNumber!=99999)
            sum = sum + getNumber;
            if(getNumber == 99999 && loop_count == 1) {
            printf("No input was provided"\n);
    
    
    }
        } while (getNumber != 99999);
    
    
        average = sum / loop_count;
    
    
        printf("Number of Integers: %d\n", loop_count);
        printf("Average: %d\n", average);
        printf("The Sum of the Number Is: %d", sum);
    }

  6. #51
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    i get multiple errors when I try your if stmt. why do you initialize minimum to 99999?
    Code:
    {
        int getNumber;
        int average;
        int sum = 0;
        int loop_count = 0;
        int minimum=99999;
    
    
    
    
        do {
            printf("Enter next integer:");
            loop_count++;
            scanf("%d", &getNumber);   /* read a single integer value input */
            if (getNumber!=99999)
            sum = sum + getNumber;
            if(getNumber == 99999 && loop_count == 1) {
            printf("No input was provided"\n);
            if(getNumber < minimum)    // this tests new number against the current minimum
            minimum = getNumber;    // if new number is less, it becomes the new minimum
            printf("minimum number is %d\n"), minimum);
    }
        } while (getNumber != 99999);
    
    
        average = sum / loop_count;
    
    
        printf("Number of Integers: %d\n", loop_count);
        printf("Average: %d\n", average);
        printf("The Sum of the Number Is: %d", sum);
    }

    Also, i already started answering your last question, so in case this helps any -



    What does your main loop look like now?

    In the versions so far, none of the individual values are being saved; so it's not possible to go back and test them for something like "smallest value". You would have to find that value within the loop, while the numbers were being input.

    A simple "if" statement can handle this.
    Writing the actual code is little different than explaining it, so:

    Code:
    int minimum = 99999;    // this is before main loop
    
    if (number!=99999) {
    
        sum = sum + number;
        loop_count++;
    
        if(number < minimum)    // this tests new number against the current minimum
            minimum = number;    // if new number is less, it becomes the new minimum
    }
    -[/QUOTE]

  7. #52
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    actually if I change this printf("minimum number is %d\n"), minimum); to printf("minimum number is %d\n"), getNumber;

    the program does not crash put also won't display minimum number stmt.

  8. #53
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Code:
    printf("minimum number is %d\n"), minimum);
    Parentheses - you have one too many of these: ')'

    When you are testing for a minimum, you start with the largest possible value. That way you are guaranteed to get any value smaller than that. If you started with the smallest possible value, you would never get a value smaller than that, and the value "minimum" would never be replaced with a new minimum value.

    -

  9. #54
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    Ok now it works but it is keeping a running tab of the minimum integer ie enter "new integer...minimum is".....I was looking for Enter new integer to keep repeating and then program output after termination to give the lowest entered #.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    
    
    {
    
    
        int getNumber;
        int average;
        int sum = 0;
        int loop_count = 0;
        int minimum=99999;
    
    
        do {
    
    
            printf("Enter next integer:");
            loop_count++;
    
    
             if(getNumber < minimum)    // this tests new number against the current minimum
                minimum = getNumber;    // if new number is less, it becomes the new minimum
            printf("minimum number is %d\n", minimum);
    
    
            scanf("%d", &getNumber);   /* read a single integer value input */
    
    
            if (getNumber!=99999)
            sum = sum + getNumber;
            if(getNumber == 99999 && loop_count == 1) {
    
    
    
    
            printf("No input was provided\n");
    
    
    
    
    }
    
    
        } while (getNumber != 99999);
    
    
    
    
    
    
        average = sum / loop_count;
    
    
    
    
        printf("Number of Integers: %d\n", loop_count);
    
    
        printf("Average: %d\n", average);
    
    
        printf("The Sum of the Number Is: %d", sum);
    }
    Last edited by andyouf; 10-22-2014 at 10:40 PM.

  10. #55
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    What does your print statement for the minimum value look like?

    Or more important, what is the entire code that crashes?

    -

  11. #56
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    The same 30+ reply thread going on over here as well
    http://www.dreamincode.net/forums/to...20-while-loop/

    Here's a tip for you andy - stick to ONE FORUM AT ONCE.
    Not only are you wasting the time of people giving you help for free, you're just confusing yourself (and wasting time) keeping all these threads going and not actually getting down to learning anything. You're reading different things on each site, and it's just confusing you.

    Follow ONE thread to it's conclusion, you'll get there sooner.
    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.

  12. #57
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    That's a great tip Salem, you're right and it is confusing following both. Sry about that.

  13. #58
    Registered User
    Join Date
    Jun 2014
    Posts
    54
    Quote Originally Posted by megafiddle View Post
    What does your print statement for the minimum value look like?

    Or more important, what is the entire code that crashes?

    -
    The code does not crash, but it outputs: "Enter next integer: minimum number is 99999"

  14. #59
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Well yes, look at the order that you are doing things.

    You are printing out "minimun" before ever reading in a value.

    Untill the first value is read in, "getNumber" is meaningless, and "minimum" is meaningless.

    -

  15. #60
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by andyouf View Post
    My professor wants getNumber to be a function that takes in an address parameter so that the integer entered in keyboard is picked up. I have that down
    I do not think so.
    Version 1 "getNumber to be a function "
    Code:
    /* Function definition */ 
    /* getNumber is a function which takes no parameters, returns nothing, and does nothing. */
    void getNumber(void)
    {
    }
    
    int main(void)
    {
        getNumber();  /* call to getNumber */
    
        return 0;
    }
    Version 2 "getNumber to be a function that takes in an address parameter "
    Code:
    /* Function definition */
    /* getNumber is a function which takes an address of a number, returns nothing, and does nothing. 
    WARNING: Will not set the value of *number_ptr.  */
    void getNumber(int * number_ptr)
    {
    }
    
    int main(void)
    {
        int number; 
    
        getNumber(&number);  /* call to getNumber */
    
        return 0;
    }
    Version 3 "getNumber to be a function that takes in an address parameter so that the integer entered in keyboard is picked up"
    Code:
    #include <stdio.h>
    
    /* Function definition */
    /* getNumber is a function which takes an address of a number, returns nothing, and prompts the user for a number and returns it to the address passed.  Uses code from the original poster.  */
    
    void getNumber(int * number_ptr)
    {
          printf("Enter integer:");
          scanf("%d", number_ptr);   /* read a single integer value input */
    
    }
    
    int main(void)
    {
        int number; 
    
        getNumber(&number);  /* call to getNumber */
    
        return 0;
    }
    Quote Originally Posted by andyouf View Post
    but I don't see how to make this a separate function outside of main?
    I hope that is answered by the above.
    Last edited by pheininger; 10-23-2014 at 02:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  3. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  4. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM