[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

C++ help with for loops.

Posted on 2004-10-03
Medium Priority
183 Views
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
Question by:travishaberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 55

Assisted Solution

Jaime Olivares earned 1000 total points
ID: 12213100
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

LVL 55

Assisted Solution

Jaime Olivares earned 1000 total points
ID: 12213109
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

LVL 84

Accepted Solution

ozo earned 1000 total points
ID: 12213112
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

Author Comment

ID: 12213256
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll