How This Code is Working?

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
  //ForStatistics.cc
//Computes the sample mean and variance of N numbers input at the keyboard.
//N is specified by the user but must be 10 or fewer in this example.

#include <iostream>
using namespace std;

int main()
{
   int    N=0;
   float  number, sum=0.0, sumSquares=0.0;
   float  mean, variance;

   // Wait until the user inputs a number in correct range (1-10)
   // Stay in loop if input is outside range
   while(N<1 || N>10)
   {
      cout << "Number of entries (1-10) = ";
      cin  >>   N;
   }

   // Execute loop N times
   // Start with i=1 and increment on completion of loop. 
   // Exit loop when i = N+1;
   for(int i=1; i<=N; i++ )
   {
       cout << "Input item number " << i << " = ";
       cin >> number;
       sum = sum + number;
       sumSquares = sumSquares + number*number;
   }

   mean = sum/N;
   variance = sumSquares/N - mean*mean;
   
   cout << "The mean of the data is " << mean << 
          " and the variance is " << variance << endl;

   return 0;
}

This code wants some numbers and adds them, divides to N and than calculates Variance.

I have no idea about coding. Teach me meanings of all that commands even dats and commas.
Also you can find some more codes at http://www.inference.phy.cam.ac.uk/teaching/comput/C++/examples/ForStatistics.shtml but some of these cods are not working. This code is working but i didnt understand how it works.
I have no idea about coding.

Then learn C++

Aceix.
There's a tutorial on this forum which will cover the basics for you
http://www.cplusplus.com/doc/tutorial/
#include <iostream> //iostream active
using namespace std; //use standart namespaces

int main() //i dont know but we always writing this
{
int N=0; //N is an integral and it is 0
float number, sum=0.0, sumSquares=0.0; //float is something and it means 0X0
float mean, variance; //float is variance

// Wait until the user inputs a number in correct range (1-10)
// Stay in loop if input is outside range
while(N<1 || N>10) //N will be between 1 and 10
{
cout << "Number of entries (1-10) = ";
cin >> N; //enter value of N
}

// Execute loop N times
// Start with i=1 and increment on completion of loop.
// Exit loop when i = N+1;
for(int i=1; i<=N; i++ ) //i is 1. If i is <= N than plus i to other i
{
cout << "Input item number " << i << " = ";
cin >> number; //enter the number
sum = sum + number;
sumSquares = sumSquares + number*number; //that must be something about variance
}

mean = sum/N; //mean is middle number
variance = sumSquares/N - mean*mean; //variance formule

cout << "The mean of the data is " << mean <<
" and the variance is " << variance << endl;

return 0; //return to noexistence
int main() //i dont know but we always writing this


Seeing this i would agree with Aceix and wildblue. Going through the tutorials on this site would be a good starting place.
Last edited on
Topic archived. No new replies allowed.