Thread: For loop assistance!

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    3

    For loop assistance!

    I'm very new to programming (so I apologize in advance), and I am having trouble figuring out how to make a for loop that will do the following:


    I'm asking the user to input two variables (i'll call them x & y), which I then am calculating x/y = z. I want to pose this two variable input question 3 times, and then add up the 3 z to find the average. (The later part about accumulating/averaging I can figure out, but getting a for loop to repeat and give z three times is stumping my extremely novice mind. So far I can only get the for loop to ask for the two variable inputs one time, spit out z, and then terminate (I haven't attempted the averaging of z yet, because I don't have more than one z at this time).


    To make things clearer, here's what I've got (user prompts to be more clearly developed later, just looking for for loop solving here, not prompt critique ):

    Code:
    I'm very new to programming (so I apologize in advance), and I am having trouble figuring out how to make a for loop that will do the following:
    I'm asking the user to input two variables (i'll call them x & y), which I then am calculating x/y = z. I want to pose this two variable input question 3 times, and then add up the 3 z to find the average. (The later part about accumulating/averaging I can figure out, but getting a for loop to repeat and give z three times is stumping my extremely novice mind. So far I can only get the for loop to ask for the two variable inputs one time, spit out z, and then terminate (I haven't attempted the averaging of z yet, because I don't have more than one z at this time).
    To make things clearer, here's what I've got (user prompts to be more clearly developed later, just looking for for loop solving here, not prompt critique :) ):
    includeint main(void) {
    float x, y, z;
    char c;
    
    printf ("Enter x: ");
    scanf ("%f",&x);
    while((c = getchar()!='\n')&& c != EOF);
    
    printf ("Enter y: ");
    scanf ("%f",&y);
    while((c = getchar()!='\n')&& c != EOF);
    
    for(x, y; x <=1; x++)
    {
        z = x / y;
        printf("Your average is %f\n", z);
    }
    
    printf("Thank you for using the program. Goodbye\n");
    
    getchar();
    
    return0;
    }Thanks for your help!!
    Thanks for your help!!

  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
    First of all, when posting code, make sure to use the copy-as-text / paste-as-text features of your IDE and/or browser.

    Second, remove your attempt to remove the trailing newline:
    1. It's not necessary yet.
    2. What you've written is wrong anyway.

    Third, read this -> A development process
    So step 1 is
    Code:
    int main ( ) {
        printf ("Enter x: ");
        scanf ("%f",&x);
        printf ("Enter y: ");
        scanf ("%f",&y);
        z = x / y;
        printf("Your average is %f\n", z);
    }
    Step 2 is
    Code:
    float doQuotient( ) {
        printf ("Enter x: ");
        scanf ("%f",&x);
        printf ("Enter y: ");
        scanf ("%f",&y);
        z = x / y;
        return z;
    }
    
    int main ( ) {
        z = doQuotient();
        printf("Your average is %f\n", z);
    }
    Step 3 is
    Code:
    int main ( ) {
        for ( i = 0 ; i < 3 ; i++ ) {
            z = doQuotient();
            printf("Your average is %f\n", z);
        }
    }
    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
    Registered User
    Join Date
    Feb 2018
    Posts
    3
    Sorry, but can you place this all together? Having trouble understanding what the final product should look like with the way you have it all separated out.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Having it all separated out is how you're supposed to do it.

    Having a big ball of mud all in main just breeds confusion.

    I take it you never even bothered to try and make my post work - just begged for free food.
    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.

  5. #5
    Registered User
    Join Date
    Feb 2018
    Posts
    3
    I was trying, but I was confused. Had more to do with not understand a direction than wanting it thrown down for me. Don't assume.

    It's all figured out now fyi.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a little assistance.
    By drakenathaniel in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2009, 05:07 PM
  2. Need assistance with loop and else if
    By maker10 in forum C Programming
    Replies: 4
    Last Post: 10-11-2008, 02:14 PM
  3. Need some assistance
    By gcpatton in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2006, 08:50 PM
  4. Better way maybe... but need assistance
    By tameeyore in forum C Programming
    Replies: 23
    Last Post: 02-06-2005, 09:51 PM
  5. Help..Need Assistance
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2002, 02:31 PM

Tags for this Thread