[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

C++ help with for loops.

Hello experts,

I have a the below for loop.

  for(k = a[i][j] = 0, k<n, k++) {
         tmp = a[i][j];
         a[i][j] = a[j][i];
         a[j][i] = temp;

}

Question 1) how is k initialized when they have to differnt values to initialize it with?  "k = a[i][j] = 0"

Question 2) Does the loop exicute one last time when k=n?  or does it incriment the value of k to n, check the condition and then exit the loop?

Thank you!

-TH







0
travishaberman
Asked:
travishaberman
  • 2
3 Solutions
 
Jaime OlivaresCommented:
This is a strange and nonsense code:

a[i][j] is initialized to 0
k is initialized to 0

A loop is made from k (0) to the value of 'n' (not know here), incrementing k
k is not used inside the loop
a[i][j] is swaped with a[j][i] many times, but i and j don't advance, just the same is made (k-n-1) times.
0
 
Jaime OlivaresCommented:
it is valid in C/C++ a "chain" initialization. In this case:

a = b = 3;

First b becomes 3, and then a becomes B, that is: 3 too.
0
 
ozoCommented:
1) both k and a[i][j] are initialized to 0
2) no,  it incriments the value of k to n, checks the condition and then exits the loop
0
 
travishabermanAuthor Commented:
Thank you all,

Olivares,
My code is nonsense because it is a question from a book and the point is to evaluate the complexity big O(n).
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now