|
| ComputerGeek123 (25) | |||
| I know the simple way to calculate the average but I created a table there are 2 columns one is x and the other one is x^2 and the more numbers I type in x it squares them I wanna know how you can take the average of x and x^2
| |||
Last edited on | |||
| chris (70) | |
| Three points: Firstly, I do not think you can have "float x^2" - ^ is an operator & hence should cause an error, try calling the variable just "x2". Secondly, on the line "x^2 = x*x.;" - there is a stray "." just remove that & that should work fine. Thirdly, if you want to take the average I recommend that you create some new variables: Have a variable to store the running average of x Have a variable to store the running average of x squared Have a variable that counts how many numbers have been entered. From there you should be able to calculate the average - the above variables should be edited in the loop. | |
| ComputerGeek123 (25) | |
| hmm ok but how am i gonna have a variable to store the running average of x Have a variable to store the running average of x squared Have a variable that counts how many numbers have been entered. I couldnt figure that out | |
| Lamblion (149) | |||
| |||
Last edited on | |||
| Lamblion (149) | |
| I see also that you want a RUNNING average of x. That should be very easy for you to add into the above example. See if you can do it by yourself. If not, we'll take it further. Here's a hint... change "average" in the above example to equal the running total of x, and then divide that by 2. | |
Last edited on | |
| ComputerGeek123 (25) | |
| ok let me try =] | |
| Lamblion (149) | |||
BTW, to get a comprehensive running average, you will have to add another variable.int runningtotal=0;Then in the loop...
You should be able to figure out where to put these. | |||
| ComputerGeek123 (25) | |||
I did this and I think I just ruined it
| |||
| Lamblion (149) | |||
Unless I have misunderstood you, this is exactly what you want...
| |||
| ComputerGeek123 (25) | |
| but it always divides it to 2 it should be like if there are 3 numbers it shuould divide it to 3 | |
| Lamblion (149) | |||
Then just create a variable called "count" and do this within the loop...
Make sure to initialize count to zero. This or any variation thereof will give you whatever you want. Whatever your desired outcome, this is very simple to construct. You should be able to modify these variables to suit whatever number you want to come up with. | |||
Last edited on | |||
| ComputerGeek123 (25) | |
| Thank you so much I actually learned how to it but I have one more question how can i take that average column and make it a row and put it under that loop?? | |
| Lamblion (149) | |
| Just put a cout statement(s) inside your loop. | |
| ComputerGeek123 (25) | |||
is this it?
| |||
| Lamblion (149) | |
| I don't know what you are trying to achieve. Just run it yourself and see it it's what you want. If not, adapt it accordingly. | |
This topic is archived - New replies not allowed.
