Confused

closed account (yboiAqkS)
Can someone explain this nested for loop?

int result{};
if (vec.size() > 1) {
for (size_t i=0; i< vec.size()-1; ++i)
for (size_t j=i+1; j< vec.size(); ++j)
result = result + vec.at(i) * vec.at(j);
}
return result;
Consider if vec has the elements 2 3 4 5

Then result is

2 * 3 + 2 * 4 + 2 * 5 + 3 * 4 + 3 * 5 + 4 * 5
Hello giroux47,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



This may help you to visualize and understand the code better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int result{};

if (vec.size() > 1)
{
    for (size_t i = 0; i < vec.size() - 1; ++i)
    {
        for (size_t j = i + 1; j < vec.size(); ++j)
        {
            result += vec[i] * vec[j];
        }
    }
}

return result;

Line 1 defines the variable "result" and the {}s set it to zero.

Line 3 makes sure the vector is large enough to to let the for loops work .

The outer for loop goes to the vectors size -1, so that it will not exceed the size of the vector. This is because the inner for loop starts at "i + 1" and this keeps "j" from going past the end of the vector.

The inner for loop goes from "i + 1 to vector.size()" so that you are always multiplying the outer for loop element by what is left in the vector.

seeplus's response shows what the two loops are doing.

The two for loops will limit how far they will travel through the vector, so the ".at()" function is just extra overhead that is not needed here. Using the []s is all that is really needed.

Line 9 is just a shorthand way of writing this. Both versions work the same way.

Andy
If you want to understand what a piece of code is doing, you could get it to tell you itself?!

Andy

with vec: 2 3 4 5

calc_result
BEGIN calc_result
set result = 0
run outer loop: 0-2
i = 0
  run inner loop: 1-3
  j = 1
    vec[0] = 2
    vec[1] = 3
    vec[0] * vec[1] = 6
    result -> 6
  j = 2
    vec[0] = 2
    vec[2] = 4
    vec[0] * vec[2] = 8
    result -> 14
  j = 3
    vec[0] = 2
    vec[3] = 5
    vec[0] * vec[3] = 10
    result -> 24
i = 1
  run inner loop: 2-3
  j = 2
    vec[1] = 3
    vec[2] = 4
    vec[1] * vec[2] = 12
    result -> 36
  j = 3
    vec[1] = 3
    vec[3] = 5
    vec[1] * vec[3] = 15
    result -> 51
i = 2
  run inner loop: 3-3
  j = 3
    vec[2] = 4
    vec[3] = 5
    vec[2] * vec[3] = 20
    result -> 71
return result = 71
END calc_result

result = 71


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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int calc_result(const vector<int>& vec);

int main()
{
  vector<int> vec {1, 2, 3, 4};
  cout << "with vec:";
  for(auto i : vec)
  {
      cout << " " << i;
  }
  cout << "\n\n";    

  cout << "calc_result\n";
  int result = calc_result(vec);  
  cout << "\n";
  cout << "result = " << result << "\n";  
  cout << "\n";  
}

int calc_result(const vector<int>& vec)
{
  cout << "BEGIN calc_result\n";     
  int result{};
  cout << "set result = " << result << "\n"; 
  if (vec.size() > 1)
  {
    cout << "run outer loop: 0-" << (vec.size() - 2) << "\n";
    for (size_t i = 0; i < vec.size() - 1; ++i)
    {
      cout << "i = " << i << "\n";
      cout << "  run inner loop: " << (i + 1) << "-" << (vec.size() - 1) << "\n";        
      for (size_t j = i + 1; j < vec.size(); ++j)
      {
        cout << "  j = " << j << "\n";           
        int val = vec[i] * vec[j];
        cout << "    vec[" << i << "] = " << vec[i] << "\n";
        cout << "    vec[" << j << "] = " << vec[j] << "\n";
        cout << "    vec[" << i << "] * vec[" << j << "] = "
          << (vec[i] * vec[j]) << "\n";
        result += val;
        cout << "    result -> " << result << "\n";          
      }
    }
  }
  else
  {
      cout << "vec too small to run loop\n";
  }
  cout << "return result = " << result << "\n"; 
  cout << "END calc_result\n"; 
  return result;
}
Last edited on
Topic archived. No new replies allowed.