I am pretty sure that the answer for this is n^3, because the 3rd for loop is executed n times and the middle is executed n+4 times and the top one is executed n times. I understand how to use sigma notation for the bottom and top for loop because is uses n, but what do I do different for the middle one because it uses j instead of n

for (int j = 4; j < n; ++j) { 
cin >> val; }
for (int i = 0; i < j; ++i) { 
b = b * val;}
for (int k = 0; k < n; ++k) 
c = b + c;}
}

Recommended Answers

All 2 Replies

if these are nested loops, you summation should be:

sum(j=4..n){sum(i=0..j){sum(k=0..n)ijk}}
=> sum(j=4..n){sum(i=0..j)ij[n(n+1)/2]} (because sum(k=0..n)k = [n(n+1)/2] = n^2}

and then you can solve further using the sum operations.

//O(n)
for (int j = 4; j < n; ++j) { 
  cin >> val; 
}

//O(n)
for (int i = 0; i < j; ++i) { 
  b = b * val;
}

//misformatted?
for (int k = 0; k < n; ++k) 
c = b + c;}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.