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,...

  • 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...

  • 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...

  • 81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5...

    81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5 );                    int BoxVolume(int length = {1}, int width = {1}, int height = {1});                                                     T__   F__                                                                     82. The following function is implemented to swap in memory the          argument-values passed to it:         void swap(int a, int b)                   {           int temp;             temp = a;             a = b;             b = temp;        ...

  • I am trying to run this program in Visual Studio 2017. I keep getting this build...

    I am trying to run this program in Visual Studio 2017. I keep getting this build error: error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". 1>Done building project "ConsoleApplication2.vcxproj" -- FAILED. #include <iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; void showChoices() { cout << "\nMAIN MENU" << endl; cout << "1: Addition...

  • Finish the following program which adds up all integers from 0 to the user's given number inclusively using a While Loop

    // Finish the following program which adds up all integers from 0 to// the user's given number inclusively using a While Loop. The total should be// assigned to the variable 'total'.#includeusing namespace std;int main() {int number;int total = 0;int counter = 0; //initialize the variable// user enters a numbercout << "Enter a positive integer to find the summation of ";cout << "all numbers from 0 to the given number up to 100." << endl;cin >> number;// check for invalid user...

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