jb20
jb20

Reputation: 19

In C++, how can I write a function that flips a 2d array at a specified position?

For my 2d array in C++, the 2d array needs to be flipped at a certain position. I have to write a function that flips the array Foe instance, Before:

double A[][2] = {{0,0}, {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}}
                                         A     B     C       D

call function invert(or flip): invert(A, 8, 3, 4); after:

double A[][2] = { {0, 0}, {1, 1}, {2, 2},{6, 6}, {5, 5}, {4, 4}, {3, 3}, {7, 7}}
                                            D      C        B      A

Here is the attempt I have tried

    void invert ( double A[][2], int n, int start, int len ) {
          int(*tmp)[2] = new int[][2];
          for(int i = 0; i >= A.length; i--){
               for(int j = 0; j >= A[i].length; j--){
                    if( i > start)
                        tmp = A[i][j];
                 }
           }
               for(i = start; i < A.length; i++)
                  for(j = start; j < A[i].length; j++){
                     while (i <= end){
                        tmp = A[i][j];
                   }
                }
       }

The errors I have are

I am fully aware that most of the errors in my code are evident to find, but I needed to start somewhere.

Upvotes: 0

Views: 237

Answers (2)

That's how I'd write the function if I knew it would always be used with 2 column arrays

void invert(double cities[][2], int size, int start, int len) {
    if (size < 0 || len < 0)
        return;

    double tempCoordsX, tempCoordsY;
    int endPos = start + len - 1;

    for (int i = start; i < (start + len/2); i++) {
        int mirroredPos = (endPos - (i - start)) % size;

        tempCoordsX = cities[i][0];
        tempCoordsY = cities[i][1];
        cities[i][0] = cities[mirroredPos][0];
        cities[i][1] = cities[mirroredPos][1];
        cities[mirroredPos][0] = tempCoordsX;
        cities[mirroredPos][1] = tempCoordsY;
    }

}

I repeat: please name your stuff

Upvotes: 0

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122268

I admit, I don't know how to do it with a 2D C-array. I can only tell you about the simple way to do it.

First, a general advice: Name your stuff. What if I had to read only your code to see that you are dealing with locations of cities, that have x and y coordinates, instead of having to read your text, wouldn't that be great?

Next, for resizable arrays, you can (/should) use std::vector instead of C-arrays. C-arrays decay to pointers when passed to functions. C-arrays have their size as part of their type, but it is inconvenient to access it (and impossible once decayed to a pointer). And manually resizing dynamic C-arrays isn't much fun either.

Eventually, the "simple way" is to use an existing algorithm. To reverse elements in a range it is std::reverse:

#include <iostream>  
#include <vector>
#include <algorithm>

struct location {
    int x;
    int y;
};

int main() {

    std::vector<location> cities{{0,0}, {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}};
    for (const auto& loc : cities){
        std::cout << loc.x << " " << loc.y << "\n";
    } 
    std::cout << "\n";
    std::reverse(cities.begin()+ 3,cities.begin() + 7);
    for (const auto& loc : cities){
        std::cout << loc.x << " " << loc.y << "\n";
    } 
    
}

Output:

0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7

0 0
1 1
2 2
6 6
5 5
4 4
3 3
7 7

Actually with a 1-D c-array it is almost the same. The major difference is that c-arrays do not have begin as member. This produces same output as above:

location cities2[] = {{0,0}, {1,1}, {2,2}, {3,3}, {4,4}, {5,5}, {6,6}, {7,7}};
for (const auto& loc : cities2){
    std::cout << loc.x << " " << loc.y << "\n";
} 
std::cout << "\n";
std::reverse(std::begin(cities2)+ 3,std::begin(cities2) + 7);
for (const auto& loc : cities2){
    std::cout << loc.x << " " << loc.y << "\n";
} 

And if you want to wrap it in a function you need to take care of the array decaying to a pointer:

void my_reverse(location* loc, size_t len, size_t first, size_t last){
    std::reverse(loc + first, loc + last + 1);
}

(I choose last to be the last element to be reversed. Note that the algorithm takes an iterator to the element one past the last element to be reversed).

Complete example with all three variants: https://godbolt.org/z/WMdea7WP3

Upvotes: 2

Related Questions