0

I just initalized a grid of random value and now I want to create a function named SwapItem that will swap 2 elements of my grid

Here is my grid initialization:

CPlateau::CPlateau()
    : m_iColonnes(8)
    , m_iLignes(8)
    , m_arrPlateau(NULL)

    void CPlateau::CreatePlateau()
{

  m_arrPlateau = new int*[m_iLignes];
      for (int ligne = 0; ligne < m_iLignes; ligne++)
      {
          m_arrPlateau[ligne] = new int[m_iColonnes];

          for (int col = 0; col < m_iColonnes; col++)
              m_arrPlateau[ligne][col] = 0;
      }
}

void CPlateau::SetupPlateau()
{ 
  if (m_arrPlateau == NULL)
      CreatePlateau();


  for (int ligne = 0; ligne < m_iLignes; ligne++)
      for (int col = 0; col < m_iColonnes; col++)
          m_arrPlateau[ligne][col] = (rand() % 7);
}

I'm using visual c++ with MFC aplication, I will manage the mouse click in the View.cpp but before I need to create the function that will swap 2 value of my grid, although the task seems simple,I have no idea of how to do it, can you help me? Many thks

SimonW.
  • 33
  • 6
  • I recommend making an attempt and then asking questions about the attempt if it doesn't work. Since it sounds like you are struggling with where to start, start by breaking the problem down into smaller parts until all of the parts are small enough that you have a good idea of how to accomplish them. – user4581301 Feb 29 '20 at 01:13
  • How would you swap the values of `int a = 1` and `int b = 2`? – JohnFilleau Feb 29 '20 at 01:16
  • @John by putting a=b? sorry I'm really new to programming – SimonW. Feb 29 '20 at 01:21
  • Unrelated: Rather than raw pointers to hold your arrays, consider using a `vector>`. It looks after itself so you don't need to. – user4581301 Feb 29 '20 at 01:22
  • 1
    Generally to swap two values, you put `tmp = a`, `a = b`, and then `b = tmp`. If you just do `a=b` then both `a` and `b` will contain whatever value was in `b`, and you'll lose the original value in `a`. This is why you need to save it in a temporary value. – JohnFilleau Feb 29 '20 at 01:24
  • 1
    While @John 's suggestion is not hard to write, it is used so frequently that there is a function built into the language to do it for you: [`std::swap`](https://en.cppreference.com/w/cpp/algorithm/swap) – user4581301 Feb 29 '20 at 01:25
  • @user4581301 thank you for this advice, my lack of programming knowledge limits me in my capacities – SimonW. Feb 29 '20 at 01:25
  • Thank you guys for your help, it's clearer now – SimonW. Feb 29 '20 at 01:28
  • 1
    Understood. I'm going to put on my head hat and make another suggestion: Shelve this project for a few weeks. Grab [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn the basics of C++ before proceeding. MFC is not for the faint-hearted and especially not if you are considering graphics. Adding learning the most complicated general purpose language used outside of academia to wrangling MFC is just going to slow the process down. – user4581301 Feb 29 '20 at 01:30

0 Answers0