How to declare all the variables in Setup() for my program.

Hello, I'm trying to solve the following question i.e in the below picture given for my homework.

I came up with the following code until now, however it does not fully meet the requirements

double n                  = 0;
double MMAn_1             = 0;
double MMAn               = 0;
double SIG2n_1            = 0;
double SIG2n              = 0;
double sum                = 0;
double sum_of_sqr         = 0;
double random_number      = 0;
double Xn                 = 0;
double standard_deviation = 0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Enter Window Size");
while(Serial.available()==0);
n = Serial.parseInt();
Serial.print  ("Window size is "); 
Serial.println(n);
Serial.println("Generating Random Numbers . . . ");
delay(1000);
for(int i = 0 ; i<n ; i++){
random_number = random(101);
Serial.println(random_number);
delay(300); 
sum = sum + random_number;
sum_of_sqr = sum_of_sqr + random_number*random_number;
}
MMAn_1  = sum/n;
SIG2n_1 = (sum_of_sqr - (sum*sum)/n)/n ;
}

void loop() {
  // put your main code here, to run repeatedly:
MMAn  = ((n-1)*MMAn_1 + Xn)/n;
SIG2n = ((Xn - MMAn_1)*(Xn - MMAn) + (n-1)*SIG2n_1)/n;
standard_deviation = sqrt(SIG2n);
Serial.print  ("Moving Average     = "); 
Serial.println(MMAn);
Serial.print  ("Standard Deviation = ");
Serial.println(standard_deviation);
 MMAn_1 = MMAn;
 SIG2n_1 = SIG2n;
Serial.println("Generating New Random Number . . . ");
delay(2000);
Xn = random(101);
Serial.println(Xn);
n = n+1;
}

How do you think I can
1.declare all variables inside the setup.
2. seed using input from unused pin A0.

That homework looks like a trap written by someone who doesn't understand variable scope. If you declare the variables in setup then they are only available to use in setup. If you need them in loop then they need to be local to loop, not setup. Or they need to be global if they are shared between the two.

as for question 2.
randomSeed(analogRead(A0)); will do the trick
as for question 1, well.. you have declared them all before setup(), which is correct, since if you would declare them inside setup() they would be local to the function and not usable outside of it. I am puzzled by that one..

Deva_Rishi:
as for question 2.
randomSeed(analogRead(A0)); will do the trick
as for question 1, well.. you have declared them all before setup(), which is correct, since if you would declare them inside setup() they would be local to the function and not usable outside of it. I am puzzled by that one..

It worked! Thank You.

Do you think my rest of the program leaving the variable declaration is correct?

saisujith:
Do you think my rest of the program leaving the variable declaration is correct?

If you want anyone else to read through your code, then format it. If you press control-T in the IDE then it will format the code for you. SO there really is no excuse to have it all jammed up against the left margin where you can't see any of the blocks.

You know how real coders indent the blocks so they all line up nice and neat? That isn't just to make it pretty.

As I understand it, it doesn't say to declare all variables in setup()...

(a) Declare all necessary variables.

Then in setup() do:
(b)
(c)
(d)

Then in loop(), do :
(e)
(f)
etc ...

Yeah makes sense. I kinda over thought the question.
Thanks a ton!