Question

Hi I got a problem with C++. I tried to open a file in a 2D...

Hi

I got a problem with C++. I tried to open a file in a 2D array and output it but it doesn't work. The input file is like this

3
1 6 8
9 2 4
5 7 3

This is an n*n matrix which n=3, and 3 is given in the file as the first number above matrix.

I tried to find a solution not only for 3*3 but also for 4*4, 5*5 until 10*10. This part of my project required output like this

csh> magic
      Enter input file name: sq3d.dat
      The matrix
           1  6  8
           9  2  4
           5  7  3
      is NOT a magic square.

Which is output the matrix without the n (first number).

This is what I have right now

#include <cstdlib>
#include <fstream>
#include <iomanip>

#include <iostream>
#include <string>
using namespace std;
int factiorial(int a);

int main()
{
  
   int fact, fact2;
   int count,muti,c;
   int i,j, row, col;
   int sq[100];
   string init[10][10];
   string file;
  
   cout << "Please enter a file name: ";
   getline(cin, file);
   while (file!="sq.dat")
   cout << "File does not exist." << endl;
  
  
   ifstream inputfile;
   inputfile.open("sq.dat");
   count = 0;
  
   while (!inputfile.eof())
   {
       inputfile >> sq[count];
       count++;
   }
      

   c = sq[0];
  
   cout << c << endl;
   fact = factiorial(c);
   muti = 1;
   count = 1;
   while (count <= (c*c))
   {

       muti = muti*sq[count];
       count++;
   }
   cout << muti << endl;
   cout << count << endl;
   cout << fact << endl;
  


   if (muti != fact)
   {
       cout << "The matrix \n";
  
       for (i = 0; i < c; i++)
       {
           for (j = 0; j < c; j++)
           {
               inputfile >> init[i][j];
               cout << init[i][j];
           }
           cout << '\n';
       }
       cout << "is NOT a magic square." << endl;
      
   }

   system("pause");
   return 0;
}
int factiorial(int a)
{
   unsigned int n;
   unsigned long fact = 1;

   n = a;

   for (int i = 1; i <= n*n; ++i)
   {
       fact *= i;
   }

   n = fact;
   return n;
}

I have highlighted the part of the code and I think it is where the problem is. Thank you for your help!

I am looking for help with my code, please don't just google the magic square problem and copy everything from google. Thank you!

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

Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include <cstdlib>
#include <fstream>
#include <iomanip>

#include <iostream>
#include <string>
using namespace std;

int main()
{

int fact, fact2;
int count,c;
int i,j, row, col;
int sq[100];
int init[10][10];
string file;

cout << "Please enter a file name: ";
getline(cin, file);
while (file!="sq.dat")
cout << "File does not exist." << endl;


ifstream inputfile;
inputfile.open("sq.dat");
count = 0;

while (inputfile >> sq[count++])
{
}


c = sq[0];
bool muti=true;
cout << c << endl;
muti = 1;
count = 1;
int count1=0;
int count2=0;
while (count <= (c*c))
{

muti = muti*sq[count];
init[count1][count2++]=sq[count];
if(count%c==0)
{
count1++;
count2=0;
}
count++;
}
cout << count << endl;
int N=c;
int sum = 0;
for (int i = 0; i < N; i++)
sum = sum + init[i][i];

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

int rowSum = 0;
for (int j = 0; j < N; j++)
rowSum += init[i][j];


if (rowSum != sum)
muti=false;
}

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

int colSum = 0;
for (int j = 0; j < N; j++)
colSum += init[j][i];

if (sum != colSum)
muti=false;
}

if (muti != true)
{
cout << "The matrix ";

for (i = 0; i < c; i++)
{
for (j = 0; j < c; j++)
{
cout << init[i][j]<<" ";
}
cout << endl;
}
cout << "is NOT a magic square." << endl;

}
else
{
cout << "The matrix ";

for (i = 0; i < c; i++)
{
for (j = 0; j < c; j++)
{
cout << init[i][j]<<" ";
}
cout << endl;
}
cout << "is a magic square." << endl;
}
system("pause");
return 0;
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Hi I got a problem with C++. I tried to open a file in a 2D...
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
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