矩阵的循环列 [英] Cycle columns of a matrix

查看:83
本文介绍了矩阵的循环列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,要求我循环MxN矩阵Z的列数次.我目前的代码在下面,但是当我运行它时,某些列消失了.

I have a problem that is asking me to cycle columns of a MxN matrix Z number of times. The code I have currently is below but when I run it some of the columns disappear.

我的代码应将第一列移至第二列,将第二列移至第三列,然后将最后一列移至第一列.

My code should move the first column into the second, the second into the third etc and then the last into the first column.

int first[5][5], second[5][5], i, j;    

int temp[5][5];
       for(i = 0; i < 5; i++){
         for(j = 0; j < numprocs; j++){
            temp[i][j] = second[i][j];
            second[i][j] = second[i--][j];
            second[i++][j] = temp[i][j];
         }
       }

推荐答案

  • 没有必要复制整个大小为 的数组.您可以将元素循环到一个数组本身中.在一张纸上试一下.
  • 您每次都需要备份第一列,因为这将被覆盖.然后,还原此备份.
    • There is no need to have a duplicate array of entire size. You can cycle the elements in one array itself. Try it on a piece of paper.
    • You'll need to take backup of the 1st column every time, because that will be overwritten. Then, restore this backup.
    • 我使用简单的数组和一些循环来做到这一点.看一下代码,它可以自我解释,我已经发表了适当的评论:

      I did it using simple array and some looping. Have a look at the code, it's self explanatory, and I've commented appropriately:

      #include <stdio.h>
      #include <stdlib.h>
      
      #define M 2 //no of rows
      #define N 5 //no of columns
      
      int print(int (*matrix)[N]);
      
      int main(void)
      {
          int matrix[M][N];
          int backup[M];
      
          int Z; //no of times to cycle
          int i, j, k;
      
          //get the i/p
          printf("Enter matrix:\n");
          for(i = 0 ; i < M ; i++)
              for(j = 0 ; j < N ; j++)
                  scanf("%d", &matrix[i][j]);
      
          //get Z
          printf("How many times to cycle?\n");
          scanf("%d", &Z);
      
          //for Z = 0
          if(Z == 0)
          {
              print(matrix);
              return 0;
          }
      
      
      
          Z = (Z%N);  //adjust Z to avoid unnecessary rotations because
      //rotating an array of 5 columns 25 times is same as rotating 0 times
      //(it will end up in original position after 25 rotations)
      
          for(k = 0 ; k < Z ; k++)    //first loop for Z rotations
          {
              //take backup of 1st col of matrix
              for(int i = 0 ; i < M ; i++)
              backup[i] = matrix[i][0];
      
              for(i = N - 1 ; i >= 0 ; i--)   //second loop for copying each column
              {
                  //copy ith column into (i+1)%n th column
                  for(j = 0 ; j < M ; j++)        //loop to copy all elements of column
                  {
                      matrix[j][(i+1)%N] = matrix[j][i];  //logic to wrap the last col to first one
                  }
              }
      
              //restore backup into 1st col
              for(j = 0 ; j < M ; j++)
                  matrix[j][1] = backup[j];
      
          }
      
          print(matrix);
      }
      
      int print(int (*matrix)[N])
      {
          int i, j;
          for(i = 0 ; i < M ; i++)
          {
              for(j = 0 ; j < N ; j++)
              {
                  printf("%d ", matrix[i][j]);
              }
              printf("\n");
          }
      }
      

      这是示例程序的运行:

      Enter matrix:
      1 2 3 4 5
      1 2 3 4 5
      How many times to cycle?
      1
      5 1 2 3 4 
      5 1 2 3 4 
      aditya@aditya-laptop:~/Desktop$  cc so.c -std=c11&& ./a.out
      Enter matrix:
      1 2 3 4 5 
      1 2 3 4 5
      How many times to cycle?
      3
      3 4 5 1 2 
      3 4 5 1 2
      

      这篇关于矩阵的循环列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆