I was asked this question in an interview. Am looking for answers for this:

A rectangular array of cells (say m*n) are selected in a spreadsheet (eg: MS excel). Assume that the cells contain only integer values. They need to be pasted to some location within the same spreadsheet after transposing the array (which means that the destination location will have transposed array containing n*m cells). We cannot use any memory buffers (eg: clipboard) to perform this operation. We can only use one or two temporary integer variables if required.
Write a C function/algorithm to do the same.
I'll really appreciate if someone can help me on this. Remember that some (or all) cells at the paste location might overlap with the copied (source) cells.

Recommended Answers

All 8 Replies

If you are allowed to use two temporary integer variables, then use both. Have one variable,VAL, that stores the value of one single cell at any given time. Have the other variable, LOC, store the location in the spreadsheet. I.e. if you have already moved 27 cells than LOC = 28. You will increment LOC each time a value is stored in the new spreadsheet, not each time a value is stored in VAL. This will tell you where to get the next value from and where to place it. Then, each time LOC % n == 0 you will want to move down to the next row of the spreadsheet. Let me know if you want more detail.

We don't give code as answers to questions. We help you with the code you write. So try it and reply with your attempt.

You should anything about how to wrogramming matrixes. WaltP is right, we don't write programmes for nothing.

Like the previous posters have said: I'd be happy to help you with some code, but only if you show some effort first.

People,

Let me clarify my query.
I'm not looking for code for this problem. I said 'Write a C function/algorithm to do the same' in my original post, because I was quoting the exact problem statement (as asked by the interviewer).
I pretty well know the logic (and code) for transposing a rectangular matrix in-memory. The only thing I need help for is how to take care of overlapping cells in this copy-paste operation. The transpose program is quite straight-forward but we've to ensure that no data-loss happens while doing this. So, any tips regarding that are welcome. No need to give any code.

thanks!

If there is a possibility of overlapping cells, move the cells that are in the overlap section first.

People,

Let me clarify my query.
I'm not looking for code for this problem. I said 'Write a C function/algorithm to do the same' in my original post, because I was quoting the exact problem statement (as asked by the interviewer).
I pretty well know the logic (and code) for transposing a rectangular matrix in-memory. The only thing I need help for is how to take care of overlapping cells in this copy-paste operation. The transpose program is quite straight-forward but we've to ensure that no data-loss happens while doing this. So, any tips regarding that are welcome. No need to give any code.

thanks!

Absolutely none of this information was apparent in your first post. Be sure you give all necessary information the first time.

1) Use 2 arrays
2) use an n+m x n+m array

The correct answer is that the question is absurd.

No programmer worth his salt would waste his company's time on some trivia like this. Yes, it is possible to do an overlapped transpose and paste without using extra memory, but there is no need. Can you really believe anyone to be using your spreadsheet on a system so limited in resources that it can't duplicate itself? Access itself wouldn't run.

Just allocate memory for the transposed array, transpose into it, copy it back in the new location (possibly overlapped), and free the memory. Anything more than that is not cost-effective, sure to be error prone (especially when other people start messing with it), and a maintenance nightmare. Keep code simple.

So, to reiterate, writing specialized cases is a waste of time (money) and resources (money) in development, production, and maintenance when there is no quantifiable gain in function, speed, use of target system resources, and/or etc.

Hope this helps.

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.