Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3684456 • Letter: L

Question

Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in Figure. Figure 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 in Figure. Figure In a program you can simulate a magic square using a two-dimensional array. Write a function that accepts a two-dimensional array as an argument, and determines whether the array is a Lo Shu Magic Square. Test the function in a program.

Explanation / Answer

magicSq.h
#ifndef MAGICSQ_H
#define MAGICSQ_H

//Returns True if the file is a magic square
bool isMagicSquare(std::string filename);

//This function takes in a 2d array and proceeds to sum up the values of the array horizontally, vertically, and diagonally.
bool sumUp (int arr[3][3]);

bool NotaRepeatOrNeg( std::string file);
// bool NotaRepeat( int magicSq[][3]);
// Return True if the files IS NOT A MAGIC SQUARE

#endif

magicSq.cpp

#include <iostream>
#include <fstream>
#include "magicSq.h"
#include <algorithm>

using namespace std;

bool sumUp (int arr[3][3]){
   //for loops used to determine whether the square is a Lo Shu square.
   int sum = 0;

   // first two for loops checks for diagonal sums
   for (int i = 0; i <3; i++){
       sum += arr[i][i];
        }

   if (sum==15){
       sum = 0;
       for (int i = 0; i <3; i++){
       sum += arr[2-i][i];
        }
       
   }else{return false;}
  
   //Checks sum horizontally
   if (sum==15){
       for (int i=0; i<3;i++){
       sum = 0;

           for (int j = 0; j<3; j++){
               sum+= arr[i][j];
           }
       }
   }else{return false;}

   //Checks sum vertically
   if (sum==15){
       for (int i=0; i<3;i++){
       sum = 0;

           for (int j = 0; j<3; j++){
               sum+= arr[j][i];
           }

           }
       }else{return false;}

   return true;
};

bool NotaRepeatOrNeg(std::string file){
   //this file will check if the inputs are a lo shu square.
   //it will check if the number is positive and within range using the
   //std::count function from the algorithm library.
   std::ifstream inp(file);
   int check[9] = {1,2,3,4,5,6,7,8,9}, tocheck[9];
    int num= 0;

   for (int i = 0; i <9; i++){
       inp>>num;
       if (num<0 |num>9){
           cout<<"Negative numbers or numbers outside the range of 1-9 are not accepted ";
           return false;}
       tocheck[i] = num;
    }
    for (int i= 0; i<9; i++){
        int myCounts = std::count(tocheck,tocheck+9, check[i]);
       if (myCounts==0){
            cout<<"You need the number '"<<check[i]<<"' in this square. ";
            return false;
        } else if(myCounts>1){
            cout<<"There is a repeat of the number '"<<check[i]<<"' in this square. ";
            return false;
        }
    }
    inp.close();
   return true;
}


bool isMagicSquare(std::string filename){
   //Takes a file name and uses NotaRepeatOrNeg and sumUp to
   //determine wheter the file is a Lo Shu square.
   ifstream Input;
   std::string line;
    int magicSquare [3][3], num;
  
   Input.open( filename);
   bool magic = NotaRepeatOrNeg(filename), is_lo_shu;

   if (magic){
        for (int i = 0 ; i<3; i++){
            // cout<<endl;

           for (int j= 0; j<3; j++){
               Input>>num;
               magicSquare[i][j]= num;
               // cout<<"This is valid: "<<num <<endl;
           }
               }
   }else{
       cout<<"This is not a magic square. ";
       }
Input.close();
  
   is_lo_shu = sumUp(magicSquare);
   if (!is_lo_shu){
        return false;}
  
return true;
};


main.cpp
#include <iostream>
#include "tests.h"

//this runs all the tests
using namespace std;

int main(){
  
    cout<<"Starting Magic Square tests"<<endl;
    runMagic("magic1.txt");
    runMagic("")

   //creates our 2-d arrays:
   double** array;
   array = new double *[5];
   for (int i=0; i<5;i++){
       array[i] = new double[5];
    }
   for (int i =0; i<5; i++){
       for (int j =0 ; j <5 ; j++){
           array[i][j] = i+j*2*2;
       }
   }

   cout<<"Running c string tests ";
   char manystr[] ="agfas13@! 3e32wef##$@dqw d21tb";
   char empty[] = "";
   char oneword[]="what";
   char twoword[]="is love";
   char longs[]="Onomonotopeia";
   char shorts[]= "ah";
   char* ptr = manystr;
    char* a=oneword;
    char* b= twoword;
    char* c= shorts;
    char* d= longs;
    char* e= empty;
   runReverse(a,b,c,d,e );
   cout<<"Done with reverse Strings ";
}

magic1.txt
4 9 2
3 5 7
8 1 6

test.h
#ifndef TESTS_H
#define TESTS_H

//Returns True if the file is not a magic square
bool isNotMagic(std::string filename);

//Returns true if magic squares has repeating numbers
bool hasRepeatingNumb(std::string filename);
//Returns true if magic squares has negative numbers
bool hasNegNumb(std::string filename);

//run program
void runMagic(std::string filename);

//testing cstrings

bool reverseOneWord (char* pointer);
bool reverseTWOWord (char* pointer);
bool reverseShortwords( char* pointer);
bool reverseLongwords(char* pointer);
bool reverseEmpty(char* pointer);
void runReverse(char* a,char* b,char* c,char* d,char* e);
#endif

test.cpp
#include <iostream>
#include <fstream>
#include "tests.h"
#include "magicSq.h"
#include "reverseC.h"
#include "stats.h"
#include <algorithm>

using namespace std;
void wtf(){
   cout<<"wtf ";
};
bool isNotMagic(std::string filename){
   bool y_n;

   y_n =isMagicSquare(filename);

   if (y_n){
       cout<<filename<<" is a magic square."<<endl;
   }
   else{
       cout<<filename<<" is NOT a magic square."<<endl;  
   }
   return y_n;
};

bool hasRepeatingNumb(std::string filename){
   bool Y_N;

   Y_N= isMagicSquare(filename);
   if (Y_N){
       cout<<filename<<" is a magic square AND has no repeating numbers."<<endl;
   }
   else{
       cout<<filename<<" is NOT a magic square AND has repeating numbers."<<endl;  
   }
   return Y_N;
};

bool hasNegNumb(std::string filename){

   bool magic;

   magic = isMagicSquare(filename);

   if (magic){
       cout<<filename<<" is a magic square AND is not negative."<<endl;
   }
   else{
       cout<<filename<<" is NOT a magic square AND has negative numbers."<<endl;  
   }
   return magic;
};

void runMagic(std::string filename){
   bool a,b,c;
   cout<<"Runing is isNotMagic test"<<endl;
   a = isNotMagic(filename);
   cout<<"Runing is hasNegNumb test"<<endl;
   b = hasNegNumb(filename);
   cout<<"Runing is hasRepeatingNumb test"<<endl;
   c = hasRepeatingNumb(filename);
   return;
};

bool reverseOneWord (char* pointer){

   return reverseCString(pointer);

}

bool reverseTWOWord (char* pointer){
   return reverseCString(pointer);
}

bool reverseShortwords( char* pointer){

   return reverseCString(pointer);

}

bool reverseLongwords(char* pointer){

   return reverseCString(pointer);

}

bool reverseEmpty(char* pointer){

   return reverseCString(pointer);

}

void runReverse(char* a,char* b,char* c,char* d,char* e){
bool reverseOneWord (a);
bool reverseTWOWord (b);
bool reverseShortwords( c);
bool reverseLongwords(d);
bool reverseEmpty(e);
return;
}

stats.h
#ifndef STATS_H
#define STATS_H

//Algorithm to compute the sums for the array of a certain size.
double getTotal(double** arr, int numRows, int numCols) ;


//Algorithm to compute the average of an array using the getTotal function.
double getAverage(double** arr, int numRows, int numCols) ;


//Algorithm to find the sum a certain array's row.
double getRowTotal(double** arr, int numRows, int numCols, int whichRow) ;


//Algorithm to find the sum a certain array's column.
double getColumnTotal(double** arr, int numRows, int numCols, int whichCol) ;


//Algorithm to find the highest value of an array in a peculiar row.
double getHighestInRow(double** arr, int numRows, int numCols, int whichRow) ;


//Algorithm to find the lowest value of an array in a peculiar row.
double getLowestInRow(double** arr, int numRows, int numCols, int whichRow) ;

#endif

stats.cpp
#include <iostream>
#include "stats.h"

using std::cout;
using std::endl;

//Algorithm to compute the sums for the array of a certain size.
double getTotal(double** arr, int numRows, int numCols) {
   double sum =0.0;

   for (int i =0; i<5; i++){
       for (int j =0 ; j <5 ; j++){
           sum+= arr[i][j];
       }
   }
       cout<<"Sum is: "<<sum<<endl;
       return sum;
}

//Algorithm to compute the average of an array using the getTotal function.
double getAverage(double** arr, int numRows, int numCols){
   double sum, avg, tot= numCols*numRows;
   sum = getTotal(arr,numRows,numCols);
   avg = sum /tot;
   cout<<"Your avg is: "<<avg<<endl;
   return avg;
}

//Algorithm to find the sum a certain array's row.
double getRowTotal(double** arr, int numRows, int numCols, int whichRow){
   double RowT=0.0;
   if (whichRow<=0 || whichRow>numRows){
       cout<<"Please enter a valid row number in a non indexing format. ";
       return 0.0;
   }
   for (int i = 0; i<numRows;i++){
       RowT+= arr[whichRow-1][i];
   }
   cout<<"Row total: "<<RowT<<endl;
   return RowT;
}


//Algorithm to find the sum a certain array's column.
double getColumnTotal(double** arr, int numRows, int numCols, int whichCol){
   double CowT=0.0;
   if (whichCol<=0 || whichCol>numCols){
       cout<<"Please enter a valid Col number in a non indexing format. ";
       return 0.0;
   }
   for (int i = 0; i<numCols;i++){
       CowT+= arr[i][whichCol-1];
   }
   cout<<"Col total: "<<CowT<<endl;
   return CowT;
}

//Algorithm to find the highest value of an array in a peculiar row.
double getHighestInRow(double** arr, int numRows, int numCols, int whichRow){
   double HighR, curr;
   if (whichRow<=0 || whichRow>numRows){
       cout<<"Please enter a valid row number in a non indexing format. ";
       return 0.0;
   }

   for (int i = 0; i<numRows;i++){
           curr = arr[whichRow-1][i];
           if (curr>HighR)
               HighR=curr;
       }
   cout<<"Highest Value: "<<HighR<<endl;
   return HighR;
}

//Algorithm to find the lowest value of an array in a peculiar row.
double getLowestInRow(double** arr, int numRows, int numCols, int whichRow){
   double LowR, curr;
   if (whichRow<=0 || whichRow>numRows){
       cout<<"Please enter a valid row number in a non indexing format. ";
       return 0.0;
   }

   for (int i = 0; i<numRows;i++){
           curr = arr[whichRow-1][i];
           if (curr<LowR)
               LowR=curr;
       }
   cout<<"Lowest Value: "<<LowR<<endl;
   return LowR;

}


reverseC.h
#ifndef REVERSEC_H
#define REVERSEC_H

//Using two pointers, this function will continue swapping until entire string is reversed
char* reverseCString( char* letter);

#endif

reverseC.cpp
#include <iostream>
#include "reverseC.h"

#include <cstring>
using namespace std;


char* reverseCString( char* letter){

   int size =strlen(letter), cnt=0;
   
   if (size==0){
       cout<<"It's an empty string :("<<endl;
           return letter;
   }
   char *head = &letter[0], *tail = &letter[size-1];
      cout<<" Here is the regular string "<<letter<<endl;

   for (cnt = 0; cnt<size/2; cnt++){
        char temp = *head;
        cout<<letter<<endl;
       *head = *tail;
       *tail = temp;
       tail-- , head++;
    }

   cout<<"Here's the reversed string! "<<letter<<endl;

   return letter;
}

output

Compile and Execute C++ Online
Starting Magic Square tests                                                                                                                                 
Runing is isNotMagic test                                                                                                                                   
magic1.txt is a magic square.                                                                                                                               
Runing is hasNegNumb test                                                                                                                                   
magic1.txt is a magic square AND is not negative.                                                                                                           
Runing is hasRepeatingNumb test                                                                                                                             
magic1.txt is a magic square AND has no repeating numbers.                                                                                                  
Runing is isNotMagic test                                                                                                                                   
You need the number '1' in this square.                                                                                                                     
This is not a magic square.                                                                                                                                 
is a magic square.                                                                                                                                         
Runing is hasNegNumb test                                                                                                                                   
You need the number '1' in this square.                                                                                                                     
This is not a magic square.                                                                                                                                 
is a magic square AND is not negative.                                                                                                                     
Runing is hasRepeatingNumb test                                                                                                                             
magic1.txt is a magic square AND has no repeating numbers.                                                                                                  
Runing is isNotMagic test                                                                                                                                   
You need the number '1' in this square.                                                                                                                     
This is not a magic square.                                                                                                                                 
is a magic square.                                                                                                                                         
Runing is hasNegNumb test                                                                                                                                   
You need the number '1' in this square.                                                                                                                     
This is not a magic square.                                                                                                                                 
is a magic square AND is not negative.                                                                                                                     
Runing is hasRepeatingNumb test                                                                                                                             
You need the number '1' in this square.                                                                                                                     
This is not a magic square.                                                                                                                                 
is a magic square AND has no repeating numbers.                                                                                                            
Running c string tests                                                                                                                                      
Done with reverse Strings                                                                                                                                   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote