Question

Your program should declare an array with following initial setting: 1 4 15 7 8 10...

Your program should declare an array with following initial setting:

1 4 15 7

8 10 2 11

14 3 6 13

12 9 5 16

In this table, 16 represents a blank. Your main function should have two variables named blankCol and blankRow which should keep track of position of blank. So for this initial setting blankRow and blankCol should both be set to 3. Your program should have a function named "Display" which takes the array as argument and an integer for total number of moves and should display the contents of this 4x4 array. Your main function should have a loop taking input for a character to represent moves. 'd' for moving a number down, 'u' for moving a number up, 'l' for moving a number on left, 'r' for moving a number on right. Your program should have a function named "UpdatePuzzle" which takes array as an argument and should take two integers (by reference) representing blank row and blank column and a character representing left, right, up and down. This function should update the puzzle if it is a valid move. This function should return the new value of blankRow and blankCol in the reference parameters. Every time user presses 'u','d','l' or 'r' , your program should be able to show the total number of moves taken, Your program should have a function named PuzzleComplete which should return 1 when array has following elements in this order

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Your program should stop when the user completes the puzzle and or any time user wants to exit. Program should display the total number of moves before exit from the program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Hello here is your complete updated code

Here is Code Text

#include <iostream>
#include <cstdio>

void Display(int[4][4], int);
void UpdatePuzzle(int[4][4], int&,int&,char&);

int PuzzleComplete(int[4][4]);

int main() {
    printf("Welcome to puzzle game,\n");
    printf("Press l for left move, r for right, u for up and d down.\n");
    printf("Anytime, press q for quit the program.");
    /* puzzle array*/
    int array[4][4] = {
            {1, 4, 5, 7},
            {8, 10, 2, 11},
            {14, 3, 6, 13},
            {12, 9, 5, 16}
    };

    /*Empty row and column i.e. 16*/
    int blankCol = 3;
    int blankRow = 3;

    char move; /*user input*/
    int moves=0; /*no of moves */
    int solved = 0; /*flag for checking puzzle is solved or not. */

    /* run the loop until puzzle solved or user choose to exit */
    while(true) {
        /* Display the puzzle */
        Display(array, moves);
        /*Check puzzle is complete or not */
        solved = PuzzleComplete(array);
        if (solved == 1) {
            printf("You solved the puzzle.");
            break;
        }
        /*get the move from user*/
        printf("Enter the move: ");
        scanf("%c", &move);
        /*consume new line*/
        getchar();
        /*if user press q, then break the loop*/
        if (move == 'q') {
            break;
        }
        /*update puzzle function*/
        UpdatePuzzle(array, blankRow, blankCol, move);
        /* increase the move */
        moves++;
    }
    return 0;
}


void Display(int arr[4][4], int moves){
    /* print the move */
    printf("Total number of move: %d\n\n",moves);
    /* print the puzzle table */
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            printf("%d\t",arr[i][j]);
        }
        printf("\n");
    }
}

void UpdatePuzzle(int arr[4][4],int &blankRow, int &blankCol, char &move){
    //If user press u,
    //its mean move a cell up, and empty space must be above the cell
     //If empty space is in the last row, we can't move up,
    //Similarly for down, left, right.

    //If there is way to move,
    //swap the empty cell with movable cell.
    if(move == 'u'){
        if(blankRow == 3){
            printf("Cant move");
        }else{
            arr[blankRow][blankCol] = arr[blankRow+1][blankCol];
            arr[blankRow+1][blankCol] = 16;
            blankRow = blankRow + 1;

        }
    }
    else if(move == 'd'){
        if(blankRow == 0){
            printf("Cant move");
        }else{
            arr[blankRow][blankCol] = arr[blankRow-1][blankCol];
            arr[blankRow-1][blankCol] = 16;
            blankRow = blankRow - 1;
        }
    }

    else if(move == 'l'){
        if(blankCol == 3){
            printf("Cant move");
        }else{
            arr[blankRow][blankCol] = arr[blankRow][blankCol+1];
            arr[blankRow][blankCol+1] = 16;
            blankCol = blankCol + 1;
        }
    }else if(move == 'r'){
        if(blankCol == 0){
            printf("Cant move");
        }else{
            arr[blankRow][blankCol] = arr[blankRow][blankCol-1];
            arr[blankRow][blankCol-1] = 16;
            blankCol = blankCol - 1;
        }
    }else{
        //If user choose the wrong move. don't make a move.
        printf("Cant move. Unknown command");
    }
    printf("\n");
}

int PuzzleComplete(int arr[4][4]){
    /*If puzzled is solved, it look like this:
      1  2  3  4
      5  6  7  8
      9 10 11 12
     13 14 15 16
    */
    //It means every previous cell has lesser value
    // in compare to next cell.
    //We started with 0 and start comparing
    // previous cell with next cell
    int temp = 0;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            //If the previous cell is more than next cell,
            //It means puzzle not solved.
            if(temp > arr[i][j]) {
                return 0;
            }else{
                temp = arr[i][j];
            }
        }
    }
    return 1;
}

Output:


 
Add a comment
Know the answer?
Add Answer to:
Your program should declare an array with following initial setting: 1 4 15 7 8 10...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program Design a program that uses an array to store 10 randomly generated...

    Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...

  • 8. (15 marks) Write a complete C program, which uses an array of structures and two...

    8. (15 marks) Write a complete C program, which uses an array of structures and two user defined functions. The program deals with a library database. The program will ask the user to input required information about each book and store it in a structure in a user defined function called get info. The second user defined function display_info will display database information on the screen. The output should look as follows: Book Title Book ld 123456 C Programming 10...

  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

  • Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1....

    Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1. Select a word at random from the wordBank. This is done for you. 2. On each turn display the word, with letters not yet guessed showing as *'s, and letters that have been guessed showing in their correct location 3. The user should have 10 attempts (?lives?). Each unsuccessful guess costs one attempt. Successful guesses do NOT count as a turn. 4. You must...

  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Write a program that dynamically allocates an integer array of size of 20 to hold a...

    Write a program that dynamically allocates an integer array of size of 20 to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to • a function that calculates the average score, the minimal score, and the maximal score in the array, and returns these scores to the main function (hint: use pass by reference, I gave an example in the class) • a function that searches a specific number. The...

  • Write a program that lets the user enter 10 values into an array. The program should...

    Write a program that lets the user enter 10 values into an array. The program should then display the largest and the smallest values stored in the array. The program should display the following message to the user at the beginning "This program will ask you to enter ten values. Then it will determine the largest and smallest of the values you entered.Please enter 10 integers separated by spaces: " And then after user entered the numbers, it should display...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • With your partner, brainstorm a program that will ask the user for a number of digits...

    With your partner, brainstorm a program that will ask the user for a number of digits to be stored. The program should then create an array of that size and then prompt the user for numbers to fill each position in the array. When all of the positions are filled, display the contents of the array to the user. Create a new Project named FillArray and a new class in the default packaged named FillArray.java. You and your partner should...

  • This code should be in C please!! Your file should meet the following criteria: • Function...

    This code should be in C please!! Your file should meet the following criteria: • Function prototype and implementation for a calculateDivisors function that takes in an integer parameter, calculates the sum of its divisors, and returns that sum (as an integer value) • A structure that represents what is represented on each line of output, e.g., o Line number o Sum of the divisors for that line number o Character array containing “Perfect”, “Deficient”, or “Abundant” • Pointer declared...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT