-1

I have compiled the following program and i know what it does, it takes ten numbers multiplies it my itself and then at the end add their total together.

    #include <iostream>
    using namespace std;

    main()
    {
    int a[10];//1
    int sumOfSquares = 0 ;//2
    int i =0; //3`enter code here`

    cout << "Please enter the ten numbers one by one " << endl;//4

    for (i = 0 ; i < 10 ; i++)//5 dont get what this does,   
                              //  obviously its a for loop, 
                              //  but why does it increment i by one
    {
    cin >> a [i];//6 this means store the 10 numbers
                 //  in array a and refer to it by the variable i
    }
    for (i = 0 ; i < 10 ; i++) //7 again i dont get why this is here
    {
    sumOfSquares = sumOfSquares + a[i]*a[i];//8
    }

   cout << "The sum of squares is "<< sumOfSquares << endl; //9

   }
Lol4t0
  • 12,444
  • 4
  • 29
  • 65
james alan
  • 19
  • 1
  • 2
  • 3
    http://www.cplusplus.com/doc/tutorial/control/ - please pick up a good C++ book, this is really basic C++ language constructs. – Mat Feb 05 '12 at 12:50
  • you have there that array is accessed by variable 'i' and in next line you don't know what 'i' is doing. read what you have there. – fazo Feb 05 '12 at 12:51
  • You need to read c++ tutorial book for beginners. Any book that explains "for" loop will do. – SigTerm Feb 05 '12 at 12:51
  • I think you don't know what `i++` does... Right?? – Fahim Parkar Feb 05 '12 at 12:52
  • i++ add 1? increments 1 by 1, but from answer i have got so far i think i was wrong in thinking that 1++ add a number. I think what it does do is move from element 0 to element 10 using i++ – james alan Feb 05 '12 at 13:05
  • Maybe the confusion is that you come from another language, where you don't need to specify the counter increment yourself? For instance Delphi where you just state `For i := 0 to 9` and the compiler does the incrementing for you? Is that the problem? – Mr Lister Feb 05 '12 at 13:27

2 Answers2

1

why does it increment i by one

Array indexes run from 0 to N-1, where N is the number of elements in the array. i++ increments the value of i by 1 (equivalent to i = i + 1;). Incrementing i in the for loop is a construct for accessing each element of the array a (in order):

for (int i = 0; i < 10; i++)
{
    a[i] = 2; /* just example */
}

is equivalent to:

a[0] = 2;
a[1] = 2;
...
a[9] = 2;

As others have commented, get a C++ book (see this SO question for a list of C++ books).

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
0
#include <iostream>
using namespace std;

main()
{
    //declare space in memory for 10 numbers to be stored sequentially
    //this is like having ten variables, a1, a2, a3, a4 but rather than having
    //hardcoded names, you can say a[4] or rather i=4, a[i], to get variable a4.
    int a[10];  
    int sumOfSquares = 0 ;  //space for a number to be stored called sumOfSquares
    int i =0;               //space for a number to be stored called i

    cout << "Please enter the ten numbers one by one " << endl; //print msg to screen

    for (i = 0 ; i < 10 ; i++) //make i = 0; while i < 10 run this code! increase i by 1 each time
    {
        //a stores 10 numbers. put the number the user entered in space
        //a[0] the first time, a[1] the second time, a[2] the third time etc etc.
        cin >> a [i];

    }

    //run this code 10 times, with i=0 the first time, i=1 the second time,
    // i=3 the third time etc etc.
    for (i = 0 ; i < 10 ; i++)
    {
        //get the number at a[0] multiple it by the number at a[0]
        //add it to value already in sumOfSquares so this number goes up and up and up.
        //the second time through the loop get a[1] and multiply it by a[1].
        sumOfSquares = sumOfSquares + a[i]*a[i]; 
    }

    //print answer to screen
    cout << "The sum of squares is "<< sumOfSquares << endl; //9

}

Philluminati
  • 2,649
  • 2
  • 25
  • 32