Thread: For loop assistance!

1. 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. 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);
}
} 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. 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. 5. 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 Tags for this Thread

accumulating, for-loop, loop, user, variable 