Solved

C++ help with for loops.

Posted on 2004-10-03
4
178 Views
Last Modified: 2010-04-17
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
Comment
Question by:travishaberman
  • 2
4 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 250 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

by:Jaime Olivares
Jaime Olivares earned 250 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

by:
ozo earned 250 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

by:travishaberman
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 102
Tviruailstringtree sort multi columns on header click 1 63
Definitions and default visual studio colors 5 65
iSeries email authority 6 57
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
A short article about a problem I had getting the GPS LocationListener working.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question