2012-02-05 3 views
-1

Я скомпилировал следующую программу, и я знаю, что она делает, она принимает десять чисел, умножает ее на мою, а затем в конце объединяет их вместе.путать с программой C++?

#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 

    } 
+3

http://www.cplusplus.com/doc/tutorial/control/ - пожалуйста, подобрать хороший C++ книги, это действительно основные C++ языковые конструкции. – Mat

+0

у вас есть доступ к массиву переменной «i», а в следующей строке вы не знаете, что делает «я». прочитайте, что у вас есть. – fazo

+0

Вам нужно прочитать учебник для C++ для начинающих. Любая книга, которая объясняет цикл «для», будет выполнена. – SigTerm

ответ

1

почему это приращение я на один

массива индексов бежать от 0 к N-1, где N является количество элементов в массиве. i++ увеличивает значение i на 1 (что эквивалентно i = i + 1;). Приращение i в петле for представляет собой конструкцию для доступа к каждому элементу массива a (по порядку):

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

эквивалентно:

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

Как и другие отметили, получить книгу C++ (see this SO question for a list of C++ books).

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 

}