Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. passing variables

passing variables

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Timothy Grabrian
    wrote on last edited by
    #1

    I 2 global variables:

    bool PrintBoard[5][5];
    bool MemoryBoard[5][5];

    Then I have a function:

    void DoMove(int x,int y)
    {
    x--;
    y--;

    PrintBoard\[x\]\[y\] = !PrintBoard\[x\]\[y\];
    
    if (y > 0) {//up
    	PrintBoard\[x\]\[y - 1\] = !PrintBoard\[x\]\[y - 1\];
    }
    
    if (y < 4) {//dow
    	PrintBoard\[x\]\[y + 1\] = !PrintBoard\[x\]\[y + 1\];
    }
    
    if (x > 0) {//left
    	PrintBoard\[x - 1\]\[y\] = !PrintBoard\[x - 1\]\[y\];
    }
    
    if (x < 4) {//right
    	PrintBoard\[x + 1\]\[y\] = !PrintBoard\[x + 1\]\[y\];
    }
    
    return;
    

    }

    As you can see, the DoMove function only changes PrintBoard. I would like it to be able to change either variable with out much more code (I don't see the point of re-using code here). I was thinking I could do something like adding another argument to the function that I could pass the global to it, but I couldn't figure out how. Any Ideas? Thanks

    P 1 Reply Last reply
    0
    • T Timothy Grabrian

      I 2 global variables:

      bool PrintBoard[5][5];
      bool MemoryBoard[5][5];

      Then I have a function:

      void DoMove(int x,int y)
      {
      x--;
      y--;

      PrintBoard\[x\]\[y\] = !PrintBoard\[x\]\[y\];
      
      if (y > 0) {//up
      	PrintBoard\[x\]\[y - 1\] = !PrintBoard\[x\]\[y - 1\];
      }
      
      if (y < 4) {//dow
      	PrintBoard\[x\]\[y + 1\] = !PrintBoard\[x\]\[y + 1\];
      }
      
      if (x > 0) {//left
      	PrintBoard\[x - 1\]\[y\] = !PrintBoard\[x - 1\]\[y\];
      }
      
      if (x < 4) {//right
      	PrintBoard\[x + 1\]\[y\] = !PrintBoard\[x + 1\]\[y\];
      }
      
      return;
      

      }

      As you can see, the DoMove function only changes PrintBoard. I would like it to be able to change either variable with out much more code (I don't see the point of re-using code here). I was thinking I could do something like adding another argument to the function that I could pass the global to it, but I couldn't figure out how. Any Ideas? Thanks

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      void DoMove(bool Array[][5], int x, int y)
      {
      x--;
      y--;
      Array[x][y] = !Array[x][y];
      ...
      }

      ...
      DoMove(PrintBoard, 2, 3);
      DoMove(MemoryBoard, 3, 4);


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!

      T 1 Reply Last reply
      0
      • P PJ Arends

        void DoMove(bool Array[][5], int x, int y)
        {
        x--;
        y--;
        Array[x][y] = !Array[x][y];
        ...
        }

        ...
        DoMove(PrintBoard, 2, 3);
        DoMove(MemoryBoard, 3, 4);


        "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!

        T Offline
        T Offline
        Timothy Grabrian
        wrote on last edited by
        #3

        Thank you.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups