0

I have a task which states: "your task is to fill the 10x10 matrix with values that will turn it into a multiplication table. You mustn't use brackets. You mustn't use indexing. Ergo, you must use pointers."

The output should be following: 10x10 multiplication table

That's the solution that I end up with:

#include <iostream> 
using namespace std; 
int main(void) {
int matrix[10][11] = {};

    for(int i = 0; i <= 10; i++) {
        for(int j = 0; j <= 10; j++) {
            matrix[i-1][j-1]= i*j;
        }
    }

    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
        cout.width(4);
        cout << matrix[i][j]; }

        cout << endl;
    } 
}

I tried to think of how can I use pointers instead of indexing, but I could find any information about how to use pointers with two dimensional arrays.

I just started learning C++ and it would be very kind if your help would be descriptive enough for me to understand and in the simplest form.

Thank you!

  • `matrix[i-1][j-1]` what element index would this would make if both `i` and `j` are `0`? – Killzone Kid May 10 '18 at 10:50
  • `You mustn't use brackets` this is a little ambiguous – Killzone Kid May 10 '18 at 11:02
  • Why you need 10x11 matrix if there are only 10 rows and 10 columns – PapaDiHatti May 10 '18 at 11:02
  • A while back I wrote a [tutorial](http://www.cs.technion.ac.il/~eyudin/string_array_syntax.html) for an intro CS class that describes the relationship between the dereference and array index syntax. It also explains how to understand how to indentify "where you are" and what type you have while progressively dereferencing multi-dimensional arrays / multi-level pointers. See especially the section on Dereferencing. – qdin May 10 '18 at 11:06

3 Answers3

2

you can do

#include <iostream> 
using namespace std; 
int main(void) {
int matrix[11][11] = {};

    for(int i = 1; i <= 10; i++) {
        for(int j = 1; j <= 10; j++) {
            *(*(matrix + i - 1) + j - 1) = i * j;
        }
    }

    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
        cout.width(4);
        cout << matrix[i][j]; }

        cout << endl;
    } 
}

int[][] can be used as int**

so matrix can be treated as an int** and *(matrix + i) is the same thing as matrix[i]

Tyker
  • 2,971
  • 9
  • 21
0

The key to understanding what your assignment is, is to understanding the layout of variables in the memory. Link: Array declaration

A declaration of the form T a[N];, declares a as an array object that consists of N contiguously allocated objects of type T.

A declaration: int matrix[10][11] means that there are 10 arrays of 11 int's each.

BUT because this is ALSO continuous, that means that this can also be interpreted as 10*11 = 110 continuous int's. int matrix_1D[ 110 ];

So how to get at these elements?

Usually this is done taking the address of the first element: &matrix[0][0].

The result:

const int n = 10;
const int m = 10;
int matrix[ n ][ m ];

int* matrix_ptr = &matrix[0][0];

for ( int i = 1; i <= n; ++i )
    for ( int j = 1; j <= m; ++j )
    {
        *matrix_ptr = i * j;
        ++matrix_ptr;
    }

By using the ++ operator the pointer will move to the next element.

For output a better choice is to use the Range-based for loop

for ( const auto& row : matrix )
{
    for ( const auto& num: row )
    {
        std::cout << std::setw( 4 ) << num;
    }
    std::cout << '\n';
}
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
0

That's the final solution I came up with:

    int matrix[10][10];

    // *(*(matrix + 0)+1); -----> matrix[0][1]
    // int** ----> int[][]

    for(int i = 1; i < 11; ++i) {
        for(int j = 1; j < 11; ++j)
            *(*(matrix+i-1)+j-1)=i*j;
        }

    for(int i = 0; i < 10; i++) {
        for(int j = 0; j < 10; j++) {
        cout.width(4);
        cout << matrix[i][j]; }

        cout << endl;}

Thank you, everyone!

  • in your first for loop you set the index to 0 and acces the element at i-1 so you are acessing the element at -1 wich is a mistake – Tyker May 10 '18 at 12:27
  • After the edit looks like the "off by one" error is fixed :-) https://en.wikipedia.org/wiki/Off-by-one_error – Robert Andrzejuk May 10 '18 at 13:35
  • "Why numbering should start at zero?" prof.dr. Edsger W. Dijkstra. http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html – Robert Andrzejuk May 10 '18 at 13:41