• C programming matrix issue

    WinMan Member

    First of all, thanks for taking the time to look at my post. I’m new to C programming in general, so i need a little help with some homework.

    I have to first insert the number of rows and columns, then the elements and show the whole matrix. After that to arrange the columns in a mirror like way. For example,
    If the matrix has 2 rows and 5 columns, it should look like this:

    Matrix
    1 2 3 4 5
    6 7 8 9 0
    Should become
    5 4 3 2 1
    0 9 8 7 6
    

    First column changes places with the last. The second with the penultimate and so on.
    I wrote half of the code. All i need now is a way to change the columns.
    This is what i did so far:

    #include 
     
        void main()
        {
        int m, n, i, j, matrix[10][10];
        printf("Insert the number of rows and columns:\n");
        scanf("%d%d", &m, &n);
        printf("Insert the desired elements:\n");
        for (i = 1; i <= m; i++)
            for (j = 1; j <= n; j++)
                scanf("%d", &matrix[i][j]);
        printf("Your matrix has the following elements:\n");
        for (i = 1; i <= m; i++)
        {
          for (j = 1 ; j <= n; j++)
          {
            printf("%d\t", matrix[i][j]);
          }
          printf("\n");
        }
        }
    
  • Adan Member

    Think of the two matrices in relation and try to express them with an n n+1 syntax. You will clearly see a rule to make the mirrored matrix.

  • Amit Member

    I just printed the matrix from the end to the beginning.
    From n to 0 and from m to 0.
    This is what i added:

    for (i = m; i > 0; i--)
        {
          for (j = n ; j >0; j--)
          {
            printf("%d\t", matrix[i][j]);
          }
          printf("\n");
        }
    
Viewing 2 reply threads
  • You must be logged in to reply to this topic.
en_USEnglish