Question

Project: Perfect Square Table (Figure 1) Figure 1 shows a number square table. A Perfect Square Table is a square of positi

// Use the following

Project: Perfect Square Table
   A “Perfect Square Table” is a square of positive integers such that the
   sum of each row, column, and diagonal is the same constant.
   This program reads square tables from files, checks if they are perfect squares,
   and displays messages such as “This is a Perfect Square Table with a constant of 34!”
   or “This is not a Perfect Square Table”.

   NAME:
   IDE:

   */

   #include <iostream>

   using namespace std;

   const int MAXTBLSIZE = 100; // the maximum table size is 100


   int main( void )
   {
       string fileName[] = {"T0.txt", "T1.txt", "T2.txt", "TA.txt", "T3.txt", "T4.txt", "T5.txt", "T6.txt", "T7.txt", ""};
       int table[MAXTBLSIZE][MAXTBLSIZE] = {0};
       int tblSize;     // the number of rows and columns
       int tblConstant; // -1 if it is not a perfect table or the table's constant otherwise
       int choice = 1; // to stop the program to allow the user to see the results one table at a time
  
       // test loop: takes the names of 7 input files from an array
       for (int i = 0; choice == 1 && fileName[i] != ""; i++)
       {
           if (readTable(fileName[i], table, tblSize))
           {
               tblConstant = testTable(table, tblSize);
               printTable(table, tblSize);
               printResults(tblConstant);
           }
           else
           {
               cout << "Error: Input file \"" << fileName[i] << "\" not found!" << endl;
           }
      
           cout << "Please enter 1 to continue 0 to stop" << endl;
           cin >> choice;
       }
  
       return 0;
   }   // main


   /** Save the output below


   */

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

If you have any doubts, please give me comment...

#include <iostream>

#include <fstream>

#include <iomanip>

using namespace std;

const int MAXTBLSIZE = 100; // the maximum table size is 100

bool readTable(string fileName, int table[][MAXTBLSIZE], int &tblSize);

int testTable(int table[][MAXTBLSIZE], int tblSize);

int printTable(int table[][MAXTBLSIZE], int tblSize);

int printResults(int tblStatus);

int main(void)

{

string fileName[] = {"T0.txt", "T1.txt", "T2.txt", "TA.txt", "T3.txt", "T4.txt", "T5.txt", "T6.txt", "T7.txt", ""};

int table[MAXTBLSIZE][MAXTBLSIZE] = {0};

int tblSize; // the number of rows and columns

int tblConstant; // -1 if it is not a perfect table or the table's constant otherwise

int choice = 1; // to stop the program to allow the user to see the results one table at a time

// test loop: takes the names of 7 input files from an array

for (int i = 0; choice == 1 && fileName[i] != ""; i++)

{

if (readTable(fileName[i], table, tblSize))

{

tblConstant = testTable(table, tblSize);

printTable(table, tblSize);

printResults(tblConstant);

}

else

{

cout << "Error: Input file \"" << fileName[i] << "\" not found!" << endl;

}

cout << "Please enter 1 to continue 0 to stop" << endl;

cin >> choice;

}

return 0;

} // main

bool readTable(string fileName, int table[][MAXTBLSIZE], int &tblSize){

ifstream in;

in.open(fileName.c_str());

if(in.fail())

return false;

in>>tblSize;

for(int i=0; i<tblSize; i++){

for(int j=0; j<tblSize; j++){

in>>table[i][j];

}

}

return true;

}

int testTable(int table[][MAXTBLSIZE], int tblSize){

int sum = 0;

for (int i = 0; i < tblSize; i++)

sum = sum + table[i][i];

for (int i = 0; i < tblSize; i++) {

int rowSum = 0;

for (int j = 0; j < tblSize; j++)

rowSum += table[i][j];

if (rowSum != sum)

return -1;

}

for (int i = 0; i < tblSize; i++) {

int colSum = 0;

for (int j = 0; j < tblSize; j++)

colSum += table[j][i];

if (sum != colSum)

return -1;

}

return sum;

}

int printTable(int table[][MAXTBLSIZE], int tblSize){

cout<<"Square Table Size: "<<tblSize<<endl;

cout<<setfill('-')<<setw((tblSize+1)*3 + 2)<<"-"<<endl;

for(int i=0; i<tblSize; i++){

cout<<"|";

for(int j=0; j<tblSize; j++){

cout<<setw(3)<<setfill(' ')<<table[i][j]<<"|";

}

cout<<endl;

}

cout<<setfill('-')<<setw((tblSize+1)*3 + 2)<<"-"<<endl;

}

int printResults(int tblStatus){

if(tblStatus>0)

cout<<"This is a Perfect Square Table with a constant of "<<tblStatus<<"!"<<endl;

else

cout<<"This is not a Perfect Square Table"<<endl;

}

nagaraju@nagaraju-Vostro-3550:~/Desktop/CHEGG/2019/April/25042019$ g++ perfect_square.cpp nagarajugnagaraju-Vostro-3550:~/Des

Add a comment
Know the answer?
Add Answer to:
// Use the following Project: Perfect Square Table    A “Perfect Square Table” is a square...
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 am having trouble figuring out why my program will not make any columns, just rows....

    I am having trouble figuring out why my program will not make any columns, just rows. I need to display a square. The instructions are provided below. (This for my C++ class) Write a program that displays a square. Ask the user for the square’s size. Only accept positive integer numbers (meaning choose a data type that holds positive integer values). The size the user gives you will determine the length and width of the square. The program should then...

  • Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the...

    Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the sums of its diagonals. Function Description Complete the diagonalDifference function described below to calculate the absolute difference between diagonal sums. diagonalDifference( integer: a_size_rows, integer: a_size_cols, integer array: arr) Parameters: a_size_rows: number of rows in array a_size_cols: number of columns in array a: array of integers to process Returns: integer value that was calculated Constraints -100 < = elements of the matrix < = 100...

  • C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <...

    C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) {    // Add your code here.    string line;    getline(inFile, line);    int sp = 0;    for (int i = 0; i < line.size(); i++)    {        if (line[i] == ' ')            sp++;    }    sp++;    return sp; } int measureLines(ifstream& inFile) {    // Add your code here.    string line;    int n = 0;    while (!inFile.eof())    {        getline(inFile,...

  • Read your notes concerning 2D arrays with particular attention to the syntax of passing arrays as...

    Read your notes concerning 2D arrays with particular attention to the syntax of passing arrays as parameters to functions. Write the pseudocode (algorithm) for the following program definition: A text file contains a square matrix of integer numbers. The file contains an integer number indicating the identical number of rows and columns followed by the data itself (that number cannot be larger than 100). For example, a file containing a 3x3 matrix would contain 3 followed by 9 other integer...

  • Can I get help with adding the following to this code please? 1. When buying multiple...

    Can I get help with adding the following to this code please? 1. When buying multiple tickets, if one of the seats selected is already taken, give a message like "Sorry, one or more of the seats selected is already taken." and prompt the user to select the seats all over. (Or if possible to make it suggest a location where the seats are together that the user can select instead) 2. Print the total cost after all of the...

  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • Can you fix this program and run an output for me. I'm using C++ #include using...

    Can you fix this program and run an output for me. I'm using C++ #include using namespace std; //function to calculate number of unique digit in a number and retun it int countUniqueDigit(int input) {    int uniqueDigitCount = 0;    int storeDigit = 0;    int digit = 0;    while (input > 0) {        digit = 1 << (input % 10);        if (!(storeDigit & digit)) {            storeDigit |= digit;       ...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • I've created a functioning program but I can't figure out how to implement class structure into...

    I've created a functioning program but I can't figure out how to implement class structure into it. I'm currently studying classes and I'm wondering if you could help me recreate my program with classes so I can have a better understanding of how classes work. Thank you. #pragma once #include <iostream> using namespace std;    bool DeclareResults(char ch, char gameboard[][3]);    bool FilledBoard(char gameboard[][3]);    void printGameBoard(char gameboard[][3]); #include "fnt.h"; #include <iostream> using namespace std; int main() {    //declare...

  • This is a C++ assignment that I'm trying to create and would like some help understanding...

    This is a C++ assignment that I'm trying to create and would like some help understanding while loops, with possible integration of for loops and if statements. This program only uses while, for, and if. I have some code that I have started, but where to go from there is what's giving me some trouble. This is involves a sentinel controlled while loop, and there are a lot of specifications below that I must have in the program. The program...

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