Pointer Arithmetic

My code is working correctly using array notation, but I'm not allowed to use it in this program. I have to use pointer arithmetic. I have an array called yearEndRank [20][6] that we input 5 scores to. The displaying output has to sort them and give the highest ran, which is technically the lowest number. The code I pasted below works, but I am not allowed to use it, since it is not pointer arithmetic. Any help?

1
2
3
4
5
6
7
8
9
10
11
  Put the code youint computeHighestRank (int locYearEndRank, int locSlot)
{

  int temp = locYearEndRank[locSlot][1];

    for (int rank = 1; rank < 6; rank++)
    {
      if (locYearEndRank [locSlot][rank] < temp) temp = locYearEndRank [locSlot][rank];
    }

      return temp; need help with here.
Let's start with the basics:

1
2
3
4
5
int a[20]; // one-dimensional array

a[0] == *(a + 0);
a[1] == *(a + 1);
a[18] == *(a + 18);


So far so good. It gets a little bit trickier with multiple dimensions.

1
2
3
int a[20][10]; // two dimensional array (some would call it matrix)

a[5][8] == /* ??? */;


Well if a[20][10] is a matrix, we can think of 20 as the height, and 10 as the width.

So a[5][8] means we're on line 6, column 9.

(This is because in C++ counting starts from 0 not 1, but we count our lines and columns starting from 1 because we're human.)

Long story short, here's the formula:
a[x][y] == *(a + width_of_a*x + y);

a[5][8] == *(a + 10*5 + 8);

And here's an article your professor should read.
http://www.cplusplus.com/forum/articles/17108/
Last edited on
                                        EQUIVALENT EXPRESSIONS
1
2
3
4
5
6
int a[ 100 ];

a[ 12 ] = 7;

for (int i = 0; i < 10; i++)
  a[ i ] += 1;
int a[ 100 ];

*(a + 12) = 7;

for (int* p = a; p < a + 10; p++)
  *p += 1;

More reading about pointers:

http://www.cplusplus.com/faq/sequences/strings/c-strings-and-pointers/#pointers

view-source:http://www.cplusplus.com/doc/tutorial/pointers/
(Scroll down to "Pointers and Arrays")

Hope this helps.
Sorry for being basic here, but do I start my pointer when im declaring that function prototype at the beginning of my code? Or does it wait until I'm inside of the main?
Sorry for being basic here, but do I start my pointer when im declaring that function prototype at the beginning of my code? Or does it wait until I'm inside of the main?

I don't understand your question.

Perhaps you should read about functions and their parameters:
http://www.cplusplus.com/doc/tutorial/functions/
Forget that last post, as I have it declared now and running in a for loop.

I still just don't understand how to get the pointer to keep increasing and reading in from my array.
1
2
3
4
5
6
7
8
9
int xs[] = { 7, 8, 9 };  // array of integers 

int* p = &x[ 0 ];  // *p is 7

cout << *p << end;  // prints "7"

p += 1;  // update to point to the next integer

cout << *p << endl; // prints "8" 

Hope this helps.
Topic archived. No new replies allowed.