Average

closed account (oz80RXSz)
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

int main()
{
	float x=0.0;
	float x^2;
	float select = 0;

  cout.precision(4);
  cout << "      x     x^2";
  
  while (x!= -99999)

  {
	cin>>x;
	x^2 = x*x.;
    cout.width(7);
	cout << x << " ";
    cout.width(7);
    cout << x^2 << '\n';

  }

  return 0;
}
Last edited on
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.
closed account (oz80RXSz)
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

int main()
{
	double x=0, xsquared=0, average=0;

	cout.precision(4);

	cout << "       x       xsquared       average" << endl;

	while( x <= 9999 )
	{
		cin >> x;

		xsquared = x * x;
		average = (xsquared + x) / 2;

		cout.width(8);
		cout << x;

		cout.width(14);
		cout << xsquared;

		cout.width(14);
		cout << average << endl;
	}

	return 0;
}
Last edited on
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
closed account (oz80RXSz)
ok let me try =]
BTW, to get a comprehensive running average, you will have to add another variable.

int runningtotal=0;

Then in the loop...

1
2
runningtotal += x;
average = runningtotal / 2;


You should be able to figure out where to put these.
closed account (oz80RXSz)
I did this and I think I just ruined it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

int main()
{
	float x=0.0;
	float xsquared;
	float select = 0;
	float average = 0;
	int i;
	static int sum = 0, count = 0; 
 
  
  cout.precision(4);
  cout << "      x     xsquared	average";
  
  while (ftemp!= -99999)

  {
	cin >> x;

		xsquared = x * x;
		average = (xsquared + x) / 2;

		cout.width(8);
		cout << x;

		cout.width(14);
		cout << xsquared;

		cout.width(14);
		cout << average << endl;


		x =  x+ i; 
 
  count++; 
 
  return x / count; 
  }

  return 0;
}
Unless I have misunderstood you, this is exactly what you want...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

int main()
{
	double x=0, xsquared=0, average=0, runningtotal=0;

	cout.precision(4);

	cout << "       x       xsquared       average" << endl;

	while( x <= 9999 )
	{
		cin >> x;

		xsquared = x * x;
		runningtotal += x;
		average = runningtotal / 2;

		cout.width(8);
		cout << x;

		cout.width(14);
		cout << xsquared;

		cout.width(14);
		cout << average << endl;
	}
	
	return 0;
}
closed account (oz80RXSz)
but it always divides it to 2 it should be like if there are 3 numbers it shuould divide it to 3
Then just create a variable called "count" and do this within the loop...

1
2
count++;
average += (runningtotal / count);


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
closed account (oz80RXSz)
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??
Just put a cout statement(s) inside your loop.
closed account (oz80RXSz)
is this it?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

using namespace std;

int main()
{
	double x=0, xsquared=0, average=0, runningtotal=0;
        int count=0;

	cout.precision(4);

	cout << "       x       xsquared      " << endl;

	while( x <= 9999 )
	{
		
                cin >> x;

		xsquared = x * x;
		runningtotal += x;
		average = runningtotal / 2;

		cout.width(8);
		cout << x;

		cout.width(14);
		cout << xsquared;
               
                count++;
                average += (runningtotal / count);
		cout.width(14);
		cout << average << endl;
	}
	
	return 0;
}
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.
Topic archived. No new replies allowed.