-1

I wanted to know if I could get some help creating a square matrix. I know how to create the matrix, but I need to populate the matrix. I am having trouble following the logic to create the following matrix:

0 1 2 3 4 5
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10

Here is what I have in my code thus far: EDIT* I have changed the else statement from arr[i][j] += 1 to arr[i][j-1] + 1

void computeMatrix(int rows, int cols, int sqMatrix[][cols]){
    int i,j;

    for(i = 0; i < rows; i++){
        for(j = 0; j < cols; j++){
            if(i == 0 && j == 0){
                arr[i][j] = 0;
            }
            else{
                arr[i][j] = arr[i][j-1] + 1; //previously arr[i][j] += 1
            }
        }
    }
}

The issue I am having is that this code makes the following matrix:

0 1 2 3 4 5
6 7 8 9 10 11
12 13 14 15 16 17
18 19 20 21 22 23
24 25 26 27 28 29 
30 31 32 33 34 35

I am not sure what type of logic to implement to get the matrix correct. I have a general idea, but I am not sure how to implement it to the code. I know that as we go down the rows, we increase by 1, and as we go through the columns, we increase by 1 as well.

Any help I could get will be greatly appreciated!

joshiapoorav
  • 63
  • 1
  • 5
  • 4
    Maybe you take a piece of paper and write the values of i and j to each row and column and see if you can find a way to calculate the value of each cell from i and j. – mkrieger1 Aug 06 '20 at 19:36
  • Note that `arr[i][j] += 1` just adds 1 to the value already in `arr[i][j]`, which will be some garbage since it was never initialized. Adding 1 to garbage is still garbage. – Nate Eldredge Aug 06 '20 at 19:43
  • I have updated my code a little bit: – joshiapoorav Aug 06 '20 at 19:44
  • I made the else statement arr[i][j] = arr[i][j-1] + 1, which makes my matrix 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 , etc. etc – joshiapoorav Aug 06 '20 at 19:46
  • arr is not defined and sqMatrix is not used. Apparently the caller provides the matrix, and as it needs to be square, one dimension passed would suffice. Finally, it looks like a great place for the ternary operator: arr[x[[y] = ( x ? arr[x-1][y] + 1 : y ); If x is not 0 then reach back one row and add one else y, the column index, is the value. – David G. Pickett Aug 06 '20 at 20:17
  • Edits to your post should be limited to formatting improvements, and or clarifying _new_ content. Once commenters, and answerers begin to address the original question you posted, it is really _not_ a good idea to change the core essence of your code, in particular the when errors in your code are likely the very things being addressed in comments and answers. Doing this inserts confusion into the post for anyone coming in later trying to understand the issues. Please roll your original edit back to its original. – ryyker Aug 06 '20 at 20:20
  • For problems like this, I suggest writing the matrix out on a piece of papwer. Label the rows and columns with the values of `i` and `j` and then look for a pattern that gives a formula so you can calculate `a[i][j]` directly from `i` and `j`. Hint: you should have seen this in elementary school (or whatever the equivalent is in your country) as an addition table. – Code-Apprentice Aug 06 '20 at 20:46

3 Answers3

2

The elements of the first matrix can be set with:

a[i][j] = i + j;

There's no need for an if statement inside the loop. The whole thing looks like:

void computeMatrix(int rows, int cols, int sqMatrix[][cols])
{
    int i,j;

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            arr[i][j] = i + j;
        }
    }
}
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
  • Thank you very much, I used a suggestion mkrieger to use a pen and paper, which helped me notice the pattern that it is i+j, but thank you very much for your help! – joshiapoorav Aug 07 '20 at 05:40
0
void computeMatrix(const size_t rows, const size_t cols, int *arr)
{
    
    for(size_t r = 0; r < rows; r++)
    {
        for(size_t c = 0; c < cols; c++)
        {
            arr[r * cols + c] = c + r;
        }
    }
}



void printmatriz(const int rows, const int cols, int *arr)
{
    for(size_t r = 0; r < rows; r++)
    {
        for(size_t c = 0; c < cols; c++)
        {
            printf("%2d ", arr[r * cols + c]);
        }
        printf("\n");
    }
}

#define X 6
#define Y 6

int main(void)
{
    int arr[X][Y];

    computeMatrix(X,Y,&arr[0][0]);
    printmatriz(X,Y, &arr[0][0]);
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

How do I create a square matrix in the C programming language?

You think of it as an abstract data type, using C dynamic memory allocation.

This answer gives details and code and uses flexible array members.

Read the Modern C book

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • [This answer](https://stackoverflow.com/questions/47234434/dynamic-allocation-of-an-unknown-matrix-in-c/47235897#47235897) suggests that this question is a duplicate. – ryyker Aug 06 '20 at 20:35
  • Not exactly, since OP don't define what is a square matrix in C. My opinion is that they don't exist in the C language, but a lot of people disagree.... – Basile Starynkevitch Aug 06 '20 at 20:38