Think that a function is a building. Each building specialize on
a certain function. Researchers collect data in the university,
and a manuscript is submitted to a publisher. The manuscript is
reivewed and edited in the publisher, and it may be returned to
the university or published in a journal.
Inside of a building, people (variables) are doing their own works. From each building, you can see other buildings. But you can't see through the people inside of the building (local variables). But you can see a person walking outside (global variables).
Scope of a variable is where the variable is visible to
other C code.
Two main scopes in C: local scope and global scope
Blocks (surrounded by { }) define the boundaries (walls of buildings).
Visible to the function and to blocks inside of it.
Generally declared before main().
Visible to all functions, and can be modified in anywhere.
#include <stdlib.h> double jesseJames; int main(void) { int buz = 2; jesseJames = 0; jesseJames = jesseJames + buz; : } int HardyP(double ms) { int john, daisy; jesseJames = 3; : } double HenryU(long long grant) { double polly; int i, john; for (i=0; i < 10; i++) { double data; : } }
When we say punch John in Henry Univ., the people in Henry Univ. know who he is. John in Hardy publisher won't be punched accidentally.
With global variable, it takes a lot of effort to know what jesseJames is going through in other functions.
Global variables are good to store information relevant to all parts of the program.
The following code is a bad example.
int main(void) { int sum; for (i =0; i<5; i++) { sum = sum + i; } return 0; }
#include <stdio.h> int func1(int bob, int buz) int main(void) { int bob = 3, buz =5; bob = func1(buz, bob) buz = func1(bob, buz) printf("value of bob is %d\n value of buz is %d\n", bob, buz); return 0; } int func1(int bob, int buz) { bob = 2 * bob + buz; return bob; }
#include <stdio.h> int gZ; void func(int x); int main (void) { gZ = 5; func(gZ); printf("gZ = %d\n", gZ); } void func(int x) { x = 2; gZ += x; }
Hint, you can make an infinite loop by for( ; ; ) { } (nothing other than 2 semi-colons in the parentheses), and use break; to get out from the loop when the condition is met. But there are several ways of doing this, so use whatever intuitive to you.
From main(), call the BinomTrial() many times, and take the average number of heads. Confirm that the expected value is k p.
(optional) calculate the variance. It should be close to k p (1-p).
Hint: For a random variable X, variance can be calculated from:
In addition to keep track of sum of values from the every trials, you can use another variable to keep track of sum of squared values from the every trials.
#include <stdio.h> int main () { int var = 3; { int var = 5; printf ("Value w/i a block = %d\n", var); } printf ("Value outside = %d\n", var); return 0; }