Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I have a C multidimensional array 'matrix[61][20]'. I included a photo of what I imported (it took some time to enter it).

The problem is that the chart I used starts with 20 at the top left. So when I print out a row it is backwards. 20 is where 1 should be 19 is where 2 should be and so on.

If you look at the chart the very top 150 and 20 which has a value of '18'. I need it to be 150 and 1 which should be a value of '48'

Is there a way to get it to flip the matrix?

-Lars
 

Attachments

  • matrix.jpg
    matrix.jpg
    355.6 KB · Views: 108
Build your algorithm

Write down on a piece of paper what you want each index to be. Then look through that list and see if you can come up with a formula that reproduces that series of numbers.

You also are dealing with two dimensions here, you may want to just solve the problem in one dimension first, then expand to the second dimension later.

For example, the first index you want is 19. The second index is 18, the third is 17 (if I understand what you're trying to do). So you have:

x f(x)
1 19
2 18
3 17
and so on.


So what is f(x)?
 
ha. I was about to ask you what f(x) was. I suppose I could run it through a Switch Statement and make 20 into 1 and so on. I was tossing it out there because Objective - C is so vast someone might say "Just use the reverseMatrixMethod that is standard that everyone knows about but you :)

I already have my 2d array set up. I painstaking wrote down all the numbers from 40 up to 100 and placed each number line by line (1200 total) into a .txt document. Then I read in the file into an NSString and used a double for loop to populate the 2d array. Everything works, just every line was backwards.

I am familiar with Switch's so I can convert the number that way.

Thanks!
 
f(x) = 20 - x

ha. I was about to ask you what f(x) was.

f(x) = 20 - x

Thus when x is 1, f(x) = 19.
when x is 2, f(x) = 18.
when x is 3, f(x) = 17.

So something like
Code:
for (x = 1, x > = 20, x++)
{
     index = 20 - x;
     print matrix[index];
}
good luck
 
Know that I read you post it seems much more intelligent then the 20 case statement I just wrote :)

Does the 'f' stand for Formula? x = 20 - x;
 
Not x = 20 - x, because that is 2x = 20 or x = 10.

f(x) is a function in mathematics.

If you know C or C++, you could write a math function like this as:

Code:
int f(int x)
{
   return 19 - x; // zero-indexed array of size 20
}

Or Python,
Code:
def f(x):
   return 19 - x # zero-indexed array of size 20

Except in programming, we often name functions and variables with useful names that are later recognizable / understandable. We also do input parameter checking to protect ourselves from untrustworthy callers or to help us find bugs.

Code:
#include <assert.h>
...
// the zero-indexed array has 20 elements. indexes are 0..19
#define ARRAY_MAX_INDEX 19
...
// compute the offset of the array which has values in opposite of desired order
// index 0 => 19
// index 1 => 18
// index 19 => 0
int computeArrayOffset(int index)
{
   assert(index >= 0 && index <= ARRAY_MAX_INDEX);
   return ARRAY_MAX_INDEX - index;
}

But the above is essentially equivalent to f(x) = 19 - x which is the correct function for zero-indexed array.
 
Last edited:
Thanks for the Wiki link. haha... I knew there had to be an easy way to reveres the numbers and everyone but me knew about it :)

Thanks!

-Lars
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.