Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8800717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:39:49+00:00 2026-06-14T00:39:49+00:00

so basically i need help on taking an existing array and moving a specific

  • 0

so basically i need help on taking an existing array and moving a specific number in that array that is 3X3 and move it up down, etc.., after its moved the values around it need to move accordingly.
example
123
321
123

i want to move spot (3, 2) to the left
123
213
123

also the same for up and down elements
the program will also keep up with the spot you in and change them accordingly

my program will need to keep track of the coordinates too, that i have yet to figure out how to implement.

#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

void printarray(int num[3][3])
{
    for(int i = 0;i < 3; i++)
    {
        for(int j = 0; j< 3; j++)
        {
            cout << num[i][j] << " ";
        }
        cout << "\n";
    }
}

void swapValues(int (&array)[3][3], int i1,int j1,int i2,int j2)
{
    int temp = array[i1][j1];
    array[i1][j1] = array[i2][j2];
    array[i2][j2] = temp;
}

void moveleft(int (&array)[3][3], int i, int j)
{
    if (i>0 && i<3 && j>=0 && j<3)
    {
        swapValues(array, i, j, i-1, j);
        swapValues(array, i, j, i+1, j);
    }
    else
    {
        cout << "wrong move!" << endl;
    }
    //swapValues(array, i, j, i, j-1);
    //swapValues(array, i, j, i, j+1);
}

void movedown(int (&array)[3][3], int i, int j)
{
    swapValues(array, i+1, j, i, j);
    swapValues(array, i-1, j, i, j);
}

void moveright(int (&array)[3][3], int i, int j)
{
    swapValues(array, i, j+1, i, j);
    swapValues(array, i, j-1, i, j);
}

void moveup(int (&array)[3][3], int i, int j)
{
    //here you MUST check the indexes to make sure you CAN move left
    //int lim = (3*3)-1;
    //for(int x = lim; x >= 1; x--)
    //{

    swapValues(array, i, j, i-1, j);
    swapValues(array, i, j, i+1, j);
    int coord1d = j * 3 + i;
}

class Puzzle
{
public:
    Puzzle();
    void swapValues(int num[3][3], int i1, int j1, int i2, int j2);
    //void moveleft(int[3][3], int start1, int start2);
    void moveup(int (&array)[3][3], int i, int j);
    void moveright(int (&array)[3][3], int i, int j);
    void moveleft(int (&array)[3][3], int i, int j);
    void movedown(int (&array)[3][3], int i, int j);
    void printarray(int[3][3]);
};

int main()
{
    int state1[3][3] = {{2, 8, 3}, {1, 6, 4}, {7, 0, 5}};
    int state2[3][3] = {{2, 8, 1}, {4, 6, 3}, {0, 7, 5}};
    int gstate[3][3] = {{1, 2, 3}, {8, 0, 4}, {7, 6, 5}};
    cout << "this is state 1" << endl;
    printarray(state1);
    cout << "\n\n";
    cout << "now this is state 2" << endl;
    printarray(state2);
    cout << "\n\n";
    cout << "now this is the goal state" << endl;;
    printarray(gstate);
    int start1, start2;
    cout << endl << endl << "please enter your starting point in the states listed above on the x axis(left to right)" << endl;
    cin >> start1;
    cout << "now enter the point on the y axis(up or down)" << endl;
    cin >> start2;
    //moveleft(state1, start1, start2);
    cout << "this is moving coordinate (1, 1) upwards" << endl;
    moveup(state1, start1, start2);
    printarray(state1);
    cout << endl;
    cout << "this is moving coordinate (1, 1) to the left" << endl;
    moveleft(state1, start1, start2);
    printarray(state1);
    cout << endl;
    cout << "this is moving coordinate (1, 1) downwards" << endl;
    movedown(state1, start1, start2);
    printarray(state1);
    cout << endl;
    cout << "this is moving coordinate (1, 1) to the right" << endl;
    moveright(state1, start1, start2);
    printarray(state1);
    cout << endl;

    cin.get();
    cin.get();
    return 0;
}

please give me code examples, explanations are good, but never clear enough for me.
not asking you to give answer, just help me see the right answer

  • so i have updated my code and it sorta kinda works, the only problem is that when i use any other coordinates besides the ones (1, 1) then i get memory addresses instead of array numbers. Any explanation on this would be greatly appreciated, i have no idea how to fix a problem like this because im not really sure how the problem is even there?

also that code is completely compilable and ready to go, so run to see what i mean if i didnt explain well enough.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:39:50+00:00Added an answer on June 14, 2026 at 12:39 am

    This is a possible solution

    Use this function to swap the values at given indexes

    void swapValues(int (&array)[3][3], int i1,int j1,int i2,int j2)
    {
        int temp = array[i1][j1];
        array[i1][j1] = array[i2][j2];
        array[i2][j2] = temp;
    }
    

    Then, for example, your moveLeft will be like this

    void moveLeft(int (&array)[3][3], int i, int j)
    {
        if (i>0 && i<3 && j>=0 && j<3)
        {
            swapValues(array, i, j, i-1, j);
        }
        else
        {
            cout << "wrong move!" << endl;
        }
    }
    

    Note that I pass the array as reference, otherwise your modifications won’t be visible in your main function

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just need help. Basically I am creating a windows appllication that sends bulk
I need help with binding back after I unbind an element. Basically, I have
I need help with auto-saving a textarea. Basically, whenever a user is typing in
I need little help for url rewriting in wordpress. basically what i need: mydomain.com/mysomewordpresspost
I need some help choosing databases for my application. My web application will basically
basically need to change the value that - admin_url() returns any idea?
I need help validating an input's value against the current date. Basically i need
I need some help editing a csproj file using PowerShell. I basically need to
I have an array of doubles and need to do a calculation on that
I need a little help on finding a tutorial or sample on taking a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.