0

So I need to make a code with these functions. the checkMatrix has to be a boolean and the getData has to pass the 1D array. How do I check the magic square with a 2D array? instructions :Write a program that will prompt the user for the integers to fill a 3 x 3 two dimensional array that builds a table. Once the data is entered the program checks to see if the table forms a magic square. When complete have the program prompt the user to see if they want to test another square and loop back accordingly. Note: Your solution should validate the input to the program from erroneous input. This means that your program checks to see that the data entered is numeric and in the range of 1 and 9 inclusive and not duplicate data can be entered. Hint: A one dimensional array could be used to keep a tally of which numbers have been assigned to the magic square. Program Style= The program must contain the following functions that carry out the operations in the given descriptions. Use the function names given. All data must be passed to required functions by value or reference; no global variables are allowed. Function Description= main= This function explains the magic square directions, organizes calls to other functions, and reports final result. getData= This function gets and validates the 9 digits entered by the user to fill the 3 x 3 table. Must implement passing of array data. checkMatrix= This function is passed the array data matrix entered by the user determine if it is a magic square sequence and returns a Boolean as result of test.

            #include <iostream>
            #include <windows.h>

            using namespace std;

            int checkMatrix()
            int getData(input[9] );
            //prototypes

            int main()
            {
                    char repeat;
                    //declaration of variables
                do
                {
                    system("cls");
                    //clear screen between calculations

                    cout << "**********************************************************" << endl;
                    cout << "**         CSC 175 Program #6 by Morgan Kelly           **" << endl;
                    cout << "**                                                      **" << endl;
                    cout << "**  Enter a magic square sequence by assigning a value  **" << endl;
                    cout << "**  from 1 to 9 to each location of a 3 by 3 table.     **" << endl;                                                                **" << endl;
                    cout << "**********************************************************" << endl;
                    //header




                    getData(input[9]);
                    //call getData function

                    cout << "*************" << endl;
                    cout << "*" << input[0] << " " << input[1] << " " input [2] << "*" << endl;
                    cout << "*" << input[3] << " " << input[4] << " " input [5] << "*" << endl;
                    cout << "*" << input[6] << " " << input[7] << " " input [8] << "*" << endl;
                    cout << "*************" << endl;

                    if checkMatrix()= true
                        cout << "This is a magic square!" << endl;  
                    else
                        cout << "This is not a magic square!" <<endl;

                    cout << "Want to check another table? (y/n): ";
                    cin >> repeat;

                } while (repeat == 'y' or repeat == 'Y');
                //repeat program and be able to calculate another table

                return 0;
            }


             int getData(input[9])
            {
                int input[9];
                //declaration of 1 dimensional array

                while (input[9] >0 and input[9]<10)
                {
                    cout << "Enter row 1 and column 1: ";
                    cin >> input[0];
                    cout << endl;
                    cout << "Enter row 1 and column 2: ";
                    cin >> input[1];
                    cout << endl;
                    cout << "Enter row 1 and column 3: ";
                    cin >> input[2];
                    cout << endl;
                    cout << "Enter row 2 and column 1: ";
                    cin >> input[3];
                    cout << endl;
                    cout << "Enter row 2 and column 2: ";
                    cin >> input[4];
                    cout << endl;
                    cout << "Enter row 2 and column 3: ";
                    cin >> input[5];
                    cout << endl;
                    cout << "Enter row 3 and column 1: ";
                    cin >> input[6];
                    cout << endl;
                    cout << "Enter row 3 and column 2: ";
                    cin >> input[7];
                    cout << endl;
                    cout << "Enter row 3 and column 3: ";
                    cin >> input[8];
                    cout << endl;
                }
                if cin.fail()
                cin.clear();
                cin.sync();
                cout << "INVALID INPUT - ";
                //validates for input(numbers) that are between 1 and 9 only

                //input numbers from user and validate

                 return input[9];     
            }


            bool checkMatrix()
            {
               if array1[]=array2[]
                {return true;}
               else
               {return false;}  
            }
  • 1
    What is your question? Making sure the values of the 3x3 matrix are numeric in your checkMatrix() function? – Easton Bornemeier Jun 09 '17 at 15:25
  • while (input[9] >0 and input[9]<10) What are you checking here? – Easton Bornemeier Jun 09 '17 at 15:27
  • You should revise your question a bit. Your statement is a bit broad. You are asking `How do I check the magic square with a 2D array?` but that sounds more like you're asking for how to do your assignment. Do you mean, "How do I iterate through the contents of an array to verify the data is correct?" because it does indeed seem like you are not correctly reading the data. There are other things wrong, but let's start with one focus per question. You need to read user input, store data, check the data, and return an array of data, from what I gather. Which do you need help with? – Yoshi_64 Jun 09 '17 at 15:34
  • So first lets start with how do you store the data from the array? I need to allow a user to input 9 numbers. all have to be between 1 and 9 and have to be different from the previous input. how do I save each input as a number in the array? Also, I am confused as to how you pass the array from function to function. – Morgan Kelly Jun 09 '17 at 16:36
  • You should perhaps consider using `std::array` instead of C-style arrays, unless you're doing one of those ridiculous courses run by standard-library-phobes. It won't fix your major misunderstanding in `checkMatrix`, but it will stop a few other categories of simple mistake. – Rook Jun 09 '17 at 16:39

0 Answers0