开发者

modifying 2d array in a function

开发者 https://www.devze.com 2023-02-16 03:19 出处:网络
So I\'ve written a function that turns a 2D array 90 degrees and I\'ve been told on IRC that I can\'t pas开发者_如何学JAVAs 2D array by reference (for example void test(char A[][10]&)) and that I

So I've written a function that turns a 2D array 90 degrees and I've been told on IRC that I can't pas开发者_如何学JAVAs 2D array by reference (for example void test(char A[][10]&)) and that I just should pass my array the usual way, however when I do that, this function doesn't change the actual array. So how do I modify my original array in a function ?

void one(char A[][10], int N)
{
    char B [10][10];
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            B[j][N-i-1] = A[i][j];
    A = B;
}


A = B ; doesn't copy the elements of array B to A permanently. It's an invalid assignment to change to elements of A permanently. A retains it's original values upon function return. You need to do a member wise copy to permanently copy the elements of B to A.


When you pass an array (such as char A[][10]), you are actually passing a pointer to the original array, so doing A = B makes A point to B and doesn't change the original array. Instead, you can use a function such as memcpy to actually copy the contents of B to A:

memcpy(A, B, sizeof(B));


Read suggested by @FredOverflow link: How do I use arrays in C++?.

To rotate 90° clock-wise NxN array you could divide the task in two smaller steps:

  • flip the matrix in up/down direction
  • transpose it

void rot90cw(char A[][N]) {
  // flip in up/down direction (swap rows)
  for (int i = 0; i < N/2; i++)
    std::swap_ranges(&A[i][0], &A[i][0] + N, &A[N-i-1][0]);

  // transpose (swap top-right and bottom-left triangles)
  for (int i = 0; i < N-1; i++)
    for (int j = i+1; j < N; j++)
      std::swap(A[i][j], A[j][i]);
}

I've used swap() and swap_ranges() to perform operations inplace.

Example

// -*- coding: utf-8 -*-
#include <algorithm>
#include <iostream>

namespace {
  const int N = 3;

  // rotate 90° clock-wise
  void rot90cw(char A[][N]) {
      // ... defined above
  }

  void print(char a[][N]) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
        std::cout << a[i][j];
      std::cout << '\n';
    }
    std::cout << std::endl;
  }
}

int main() {
  char a[][N] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
  print(a);
  rot90cw(a);
  std::cout << "Rotated 90° clock-wise:" << std::endl; //note: utf-8
  print(a);
}

Output

abc
def
ghi

Rotated 90° clock-wise:
gda
heb
ifc


Arrays don't work like that in C++. When you pass an array in a function, you are passing a pointer to the first element and nothing more, so what you are doing is creating a locally defined array B, then setting the pointer passed into your function to point to the head of the B array. At no point does the memory assigned to your original A actually change. Then when the function returns, the A pointer from your function is discarded, leaving your original A array untouched. If you want to modify an array passed as an argument to a function, you will have to modify the elements directly.


What you are trying to pass the array by reference and change that reference. This works, but is not necessary.

You can modify the array directly element by element:

void one(char A[][10], int N)
{
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            char b = A[j][N-i-1];
            A[j][N-i-1] = A[i][j];
            A[i][j] = b;    
        }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消