Question:
when exchanging two rows in atwo dimensional array the fastest way is to:?
CS STUDENT
2006-07-29 21:23:21 UTC
1.exchange elements of array 2. exchange the address of each element in the two rows 3. set the address of one row equal to the other & vice versa 4. store the address of the rows in an array of pointers & exchange the pointers
Three answers:
gene_frequency
2006-07-29 21:30:00 UTC
Fastest to slowest (assuming all answers are workable):

3. set the address of one row equal to the other & vice versa

2. exchange the address of each element in the two rows

4. store the address of the rows in an array of pointers & exchange the pointers

1. exchange elements of array



the last (1.) is worst because you're actually moving each element's data...the data element might be huge...we don't know.



the first (3) is fastest because u only swap 2 pointers/addresses...done!



Before swapping addresses, the address must be saved. If you don't save the address, you'll loose the 1st address and you'll have 2 pointers to the same address...no good.



The wording is tricky here...some answers might not work if address saving is not implied. Thus, if #3 won't work, then #2 is next best, etc.



Important point: moving data is considered more expensive (slower) than swapping pointers to that data.



If this question is from your instructor, then he might want you to identify not only what is fastest, but which ones don't work, too.



...good luck.
ravelo
2016-11-03 11:07:04 UTC
1st off, your evaluating the placement and not the actual content textile of the array. hence exchange to names[index] interior the if assertion. 2d: the strcmp function "Returns an imperative value indicating the relationship between the strings: a nil value shows that the two strings are equivalent. a value extra desirable than 0 shows that the 1st character that doesn't tournament has a larger value in str1 than in str2; And a value under 0 shows the different." str1 and str2 are "call" and "names[index]" on your software hence, the if assertion ought to be if (strcmp(call, names[index]) == 0) third, whilst your loop reaches the tip of your array, it is going to print "call became no longer found" regardless. hence, use an if to examine whether the boolean variable continues to be fake; if no longer print no longer something.
americanDesi
2006-07-29 23:03:57 UTC
answer 3 will not work. It is because if for example you set the value of 'a' to 'b' then you loss the original value in b.





I think that both 1 and 2 will take the same time and will be faster than 4.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...