Laurand Osmeni

Let's build a faster web.


How do I calculate with arrays in C++.

Creating a array name as laurand[5].

int laurand[5]

Array holds 5 different elements numbers, 20, 45, 78 and 90.

{20, 45, 67, 78, 90 };

Now creating a variable to hold the sum by default 0.

int sum = 0;

Using for loop to get the indexes, because array holds 5 values, so the integer x will be less < than 5.

for (int x = 0; x < 5; x++)

sum +=laurand[x];

The first time the loop runs is going to call x which is equal to 0, the laurand[x] or the laurand[0] will add number 20 to the sum, then the x which was 0 now is added to laurand[20] because is adding the 20 number, and it keeps adding each element one by one. Example add 20, add again add next number 45, add again the next number 78 and again add 90, now calculate the values all together.

Because x++ increases, so each time the loop runs will add one.

Now get the sum and print programming on the screen.

cout << sum << endl;

Watch live in action creating the program, or scroll below to copy the entire code.

C++ Array Calculation
How do I calculate arrays in C++.
#include <iostream> using namespace std; int main() { int laurand[5] = {20, 45, 67, 78, 90 }; int sum = 0; for (int x = 0; x < 5; x++){ sum +=laurand[x]; cout << sum << endl; } return 0; }

Feel free to use the code.

Last update .

Navigate between pages.