Thread: 2d matrix beginner.

  1. #1
    Registered User
    Join Date
    Apr 2021
    Posts
    8

    2d matrix beginner.

    Hi, I am trying to do some exercises to learn 2d matrices in c. My goal is to create a 2d matrix which has only 3 columns but the user defined raws lets say "x" raws. The second column must contain the second exponent of the first column-numbers and the third column must contain the third exponent of the first column.

    1 1^2 1^3
    2 2^2 2^3
    3 3^2 3^3
    . . . . .
    . . . . .
    x x^2 x^3

    This is exactly what I am trying to write. I have started to code but now I am stuck. Can anyone help me with any advice? I will post my code in the first answer.

  2. #2
    Registered User
    Join Date
    Apr 2021
    Posts
    8
    This is how far I have come:

    1 1 1
    2 2 2
    3 3 3

    This is the code:

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
    
    
        int m[3][3];
        int i, j;
        for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                    m[i][j] = i+1;
            printf("%i\n", m[i][j]);
            }
            }
        printf("Content of the 2 D Array: \n");
          for(i=0;i<3;i++){
            for(j=0;j<3;j++){
                printf("%4d", m[i][j]);}
                printf("\n");
            }
    
    
        return 0;
    }

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by hekk_tech View Post
    Hi, I am trying to do some exercises to learn 2d matrices in c. My goal is to create a 2d matrix which has only 3 columns but the user defined raws lets say "x" raws. The second column must contain the second exponent of the first column-numbers and the third column must contain the third exponent of the first column.

    1 1^2 1^3
    2 2^2 2^3
    3 3^2 3^3
    . . . . .
    . . . . .
    x x^2 x^3

    This is exactly what I am trying to write. I have started to code but now I am stuck. Can anyone help me with any advice? I will post my code in the first answer.

    Matrices can be hard to understand. It's easier when you think of them as something concrete rather than an abstract "matrix". When I'm teaching matrices, I use football as an example. Say you've got four teams, three competitions, and results for those three competions (ranks). Which team is best? Well we'd rank the league cup above the FA cup, and the Champion's league top of all. So that gives us three weights, say two for the FA cup, five for the league, eight for the Chamipon's league So you can see naturally how we obtain a final score for each team.

    But what weights to use? I ask two or three people around the class for their weights. That gives me a 3xN matrix of weights. Then we've got matrix to matrix multiplication.

    The code to multiply two matrices in C is in fact extremely easy to write. The problem is wholly in understandign what you are doing.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I thought it was just a 2D array of some number of rows initialised with
    x, pow(x,2), pow(x,3)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2021
    Posts
    8
    Quote Originally Posted by Malcolm McLean View Post
    Matrices can be hard to understand. It's easier when you think of them as something concrete rather than an abstract "matrix". When I'm teaching matrices, I use football as an example. Say you've got four teams, three competitions, and results for those three competions (ranks). Which team is best? Well we'd rank the league cup above the FA cup, and the Champion's league top of all. So that gives us three weights, say two for the FA cup, five for the league, eight for the Chamipon's league So you can see naturally how we obtain a final score for each team.

    But what weights to use? I ask two or three people around the class for their weights. That gives me a 3xN matrix of weights. Then we've got matrix to matrix multiplication.

    The code to multiply two matrices in C is in fact extremely easy to write. The problem is wholly in understandign what you are doing.


    Very well you have successfully pointed out that I do not understand matrices. But this information was already obvious, otherwise I wouldn't register on this forum to ask for help and not to hear you letting me know that I do not know what I am doing. Of course I do not know what I am doing and I want to learn, please can anyone show me how its done?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    As I said
    Code:
    m[i][j] = pow(i+1,j+1);
    pow() is in math.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2020
    Posts
    425
    Quote Originally Posted by Salem View Post
    As I said
    Code:
    m[i][j] = pow(i+1,j+1);
    pow() is in math.h
    m[i][j] = pow(i+1,j+1)+0.5;
    ... just to be sure...

  8. #8
    Registered User
    Join Date
    Apr 2021
    Posts
    8
    Just for an update this is where I am at with this problem right now. Still no success, but I am definitely closer.

    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    int main()
    {
        int x, i, j;
        do {
        printf("Insert Value for x: \n");
        scanf("%d", &x);
        } while (x<=1 || x>=21);
        int m[x][3];
        for (i=0; i<x; i++){
            for (j=0; j<3; j++){
                m[i][j] = pow (i+1, j+1);
            }
        }
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2021
    Posts
    8
    Ok after a couple of ours of googling and trial and error I have finally succeeded my first "difficult" exercise. Thanks @hamster_nz and Salem for your advices. I am posting here the final result.

    insert
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    
    
    int main()
    {
        int x, i, j;
        do {
        printf("Insert Value for x: \n");
        scanf("%d", &x);
        } while (x<=1 || x>=21);
        int array[21][21];
        for (i=0; i<x; i++){
            for (j=0; j<3; j++){
                array[i][j] = pow (i+1, j+1);
            }
        }
    
    
        printf("Two Dimensional array elements:\n");
        for(i=0; i<x; i++){
            for(j=0; j<3; j++){
                printf("%d ", array[i][j]);
            }
            printf("\n");
        }
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 01-11-2019, 08:28 PM
  2. Beginner C issue ( matrix 90*)
    By MarkussR in forum C Programming
    Replies: 3
    Last Post: 04-19-2017, 11:26 AM
  3. Replies: 10
    Last Post: 12-21-2016, 12:20 AM
  4. Replies: 5
    Last Post: 01-06-2016, 01:15 AM
  5. Replies: 2
    Last Post: 05-19-2014, 07:32 PM

Tags for this Thread