Question

Concepts tested by the program:

  • Working with one dimensional parallel arrays
  • Use of functions
  • Use of loops and conditional statements

Description

The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below.

The Lo Shu Magic Square has the following properties:

  • The grid contains the numbers 1 – 9 exactly
  • The sum of each row, each column and each diagonal all add up to the same number.

s is shown below:

Write a program that simulates a magic square using 3 one dimensional parallel arrays of integer type.

Each one the arrays corresponds to a row of the magic square.

The program asks the user to enter the values of the magic square row by row and informs the user if the grid is a magic square or not.

See the sample outputs for more clarification.

Specifications

Input for this project:

  • Vales of the grid (row by row)

Output for this project:

  • Whether or not the grid is magic square
  • Programmer’s full name
  • Project number
  • Project due date

Requirements

Use the following template to start your project:

#include<iostream>

using namespace std;

// Global constants

const int ROWS = 3; // The number of rows in the array

const int COLS = 3; // The number of columns in the array

const int MIN = 1; // The value of the smallest number

const int MAX = 9; // The value of the largest number

// Function prototypes

bool isMagicSquare(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size);

bool checkRange(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size, int min, int max);

bool checkUnique(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size);

bool checkRowSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);

bool checkColSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);

bool checkDiagSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);

void fillArray(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size);

void showArray(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size);

int main()

{

  

     /* Define a Lo Shu Magic Square using 3 parallel arrays corresponding to each row of the grid */

int magicArrayRow1[COLS], magicArrayRow2[COLS],

    magicArrayRow3[COLS];

// Your code goes here

      return 0;

}

// Function definitions go here

Create and use following functions:

  • void fillArray(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size) - Accepts 3 int arrays and a size as arguments, and fills the arrays out with values entered by the user. First argument corresponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square
  • void showArray(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size) - accepts 3 int arrays and a size as arguments and displays their content.

Example:    

1 3 5    (arrayRow1)

6 7 9    (arrayRow2)

8 2 4    (arrayRow3)

  • bool isMagicSquare(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size) - accepts 3 int arrays and a size as arguments and returns true if all the requirements of a magic square are met. Otherwise, it returns false. First argument corresponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
  • bool checkRange(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size, int min, int max) - accepts 3 int arrays, a size and a min and max value as arguments and returns true if the values in the arrays are within the specified range min and max. Otherwise, it returns false. First argument corresponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
  • bool checkUnique(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size) - accepts 3 int arrays and a size as arguments, and returns true if the values in the arrays are unique (only one occurrence of numbers between 1-9). Otherwise, it returns false. First argument corresponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
  • bool checkRowSum(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size) - accepts 3 int arrays and a size as arguments and returns true if the sum of the values in each of the rows are equal. Otherwise, it returns false. First argument corresponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
  • bool checkColSum(int arrayRow1[], int arrayRow2[], int arrayRow3[], int size) - accepts 3 int arrays and a size as arguments and returns true if the sum of the values in each of the columns are equal. Otherwise, it returns false. First argument corresponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square.
  • bool checkDiagSum(int arrayrow1[], int arrayrow2[], int arrayrow3[], int size) - accepts 3 int arrays and a size as arguments and returns true if the sum of the values in each of the array's diagonals are equal. Otherwise, it returns false. First argument corresponds to the first row of the magic square, second argument to the second row and the third argument to the third row of the magic square. It Numotmiwel int ToDayAbsentint number double Average Abonnifint NumberOfEmployers in Total Number O senta) START FROGRAM DE

Can you guys help me in a flowchart, If i got wrong, plz correct me. thank you so much.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;

class GFG

{

static void generateSquare(int n)

    {

        int[][] magicSquare = new int[n][n];

          

        // Initialize position for 1

        int i = n/2;

        int j = n-1;

   

        // One by one put all values in magic square

        for (int num=1; num <= n*n; )

        {

            if (i==-1 && j==n) //3rd condition

            {

                j = n-2;

                i = 0;

            }

            else

            {

                //1st condition helper if next number

                // goes to out of square's right side

                if (j == n)

                    j = 0;

                      

                //1st condition helper if next number is

                // goes to out of square's upper side

                if (i < 0)

                    i=n-1;

            }

              

            //2nd condition

            if (magicSquare[i][j] != 0)

            {

                j -= 2;

                i++;

                continue;

            }

            else

                //set number

                magicSquare[i][j] = num++;

                  

            //1st condition

            j++; i--;

        }

   

        // print magic square

        System.out.println("The Magic Square for "+n+":");

        System.out.println("Sum of each row or column "+n*(n*n+1)/2+":");

        for(i=0; i<n; i++)

        {

            for(j=0; j<n; j++)

                System.out.print(magicSquare[i][j]+" ");

            System.out.println();

        }

    }

      

    // driver program

    public static void main (String[] args)

    {

        // Works only when n is odd

        int n = 7;

        generateSquare(n);

    }

}

Add a comment
Know the answer?
Add Answer to:
Concepts tested by the program: Working with one dimensional parallel arrays Use of functions Use of...
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
  • I need help as quick as possible, thanks beforehand. Please provide the test output The Lo...

    I need help as quick as possible, thanks beforehand. Please provide the test output The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown below. 35 The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 - 9 exactly The sum of each row, each column and each diagonal all add up to the same number. This is shown below: 15 4 92 15 - 81 + 15 15 15...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • 9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that h...

    9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown in the following figure · 15 4 9 2-15 3 5 7-15 81 615 15 15 15 15 Write a program that simulates a magic square using...

  • please use java language please used ArrayList The Lo Shu Magic Square is a grid with...

    please use java language please used ArrayList The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-31. The • The sum of each row, each column, and each diagonal all add up to the same number 20. Lo Shu Magic Square Lo Shu Magic Square has the following properties: • The grid contains the numbers 1 through 9 exactly. This is shown in Figure 7-32. In a program you can simulate a...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • I need help with the following and written in c++ thank you!: 1) replace the 2D...

    I need help with the following and written in c++ thank you!: 1) replace the 2D arrays with vectors 2) add a constructor to the Sudoku class that reads the initial configuration from a file 3) adds a function to the Sudoku class that writes the final Sudoku grid to a file or to the standard output device, cout. Sudoku.h #pragma once /* notes sudoku() default constructor precondition : none postcondition: grid is initialized to 0 sudoku(g[][9]) 1-parameter constructor precondition...

  • Help pls! and kindly explain how you do this as well. Thaank you Write a program...

    Help pls! and kindly explain how you do this as well. Thaank you Write a program to test whether a square is a 3x3 magic square. A magic square is a grid with 3 rows and 3 columns, like the figure below. A magic square has the following properties: the grid contains only the numbers 1 through 9 the sum of each row, each column, and each diagonal all add up to the same number Notes: I have provided the...

  • C++ Single Dimensional Arrays

    Exercise #1: Design and implement a program (name it AssignGrades) that stores and processes numeric scores for a class. The program prompts the users to enter the class size (number of students) to create a single-dimensional array of that size to store the scores. The program prompts the user to enter a valid integer score (between 0 and 100) for each student. The program validates entered scores, rejects invalid scores, and stores only valid scores in the array.  The program...

  • Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

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