Double arrays with magic square

Hey guys, so I need to create a program that read in from a file. That file contains a single number on the first line and that number sets a matrix ( so if the first number is a 4, the matrix will be a 4x4). The after that is a matrix of numbers that is the size of the first number stated. The file looks like this.
3
8 1 6
3 5 7
4 9 2
What i need to do is read in the first number to create a double array to create the matrix and store each row and column in their appropriate array. Then I need to add the columns, rows, and diagonal to see if it is a magic square. Im pretty sure I can do that myself. The only problem I have is trying to set and develop the double array so that I can start storing the numbers. If anyone could help me get started and show me how to set up the double array so that it is the size of the first number in the file it would be appreciated. I dont really have much code to start from so far but I will post what I have even though I know it isnt correct. I should be able to get everything else after i set up the array. Please keep note this program needs to be able to read from any file and take any number to create a matrix so instead of just making a 3x3 matrix, the files first number could be 41 so it would have to be a large 41x41 matrix. Thanks guys appreciate it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  #include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
int main()
{
	ifstream myfile;
	int x;
	myfile.open("C:\\Users\\Scott\\Desktop\\text1.txt");
	myfile >> x;
	cout << x;
	int row(x);
	int column(x);
	int a[row][column];
	

	return 0;
}
	
@stmaki16

Here's a small program that can create a 2D array upon execution. Your way, with lines 12 to 14, won't do it.

You can create a 41x41 matrix, but it won't fit on a console screen, unless you enlarge the screen first. I'd stick with 25 or less. Anyway, here it is..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Create New Array.cpp : main project file.

#include <iostream>
#include <ctime> // For srand()

using namespace std;

int main()
{
	srand(static_cast<int>(time(0))); // seed the random number generator
	int SIZE, x,y;
	cout << endl << "MaxSize for the array, to equal what ? : ";
//  Above is where you would read in the number to create the array
	cin >> SIZE;
	//int* numbers = new int[SIZE];
	// --- 2D arrays ---
	int **ary = new int*[SIZE];
	for (int i = 0; i < SIZE; ++i)
 {
	 ary[i] = new int[SIZE];
 }

	for (x = 0; x < SIZE; x++)// Just to fill array with random numbers
		for (y = 0; y < SIZE; y++)
			ary[x][y] = 1 + rand() % SIZE;

	for (x = 0; x < SIZE; x++) // Print out newly created array
	{
		for (y = 0; y < SIZE; y++)
		{
			if (ary[x][y] < 10)
				cout << " "; // Print space if number < 10 to keep numbers aligned
			cout << ary[x][y] << " ";
		}
		cout << endl;
	}
 // Clean-up would be 
	for (int i = 0; i < SIZE; ++i)
  {
    delete [] ary[i];
  }

  delete [] ary;
  
}
Thank you for the post. Some of this in here is helping me, but one thing that I still an unable to figure out is how to read in that first number and use hat number to create the double array. So up above the first number before the square is 3 so I need to make a double array that is [3][3]. Thats what im having most of my trouble with. After that I have a good plan of what im going to do. Thank you for the response and I appreciate it.
Actually I was able to figure that out with what you gave me It just took me 2 times to see it. Interesting how to set it in a loop. Can i ask why you approached it that way? Thank you.
@stmaki16

Interesting how to set it in a loop. Can i ask why you approached it that way?


Well, actually I don't think there is any other way to do it.
( If there is, I would appreciate another programmer showing me how.. )

int **ary = new int*[SIZE]; creates an array named ary, of SIZE.
Of course, you can call it any name you wish, if you don't want the name, ary.
Then the for loop afterwards, creates multiples of the original. You access each cell as you would any 2D array. The row, 0 to SIZE-1, then column, 0 to SIZE-1, as I'm doing from lines 23 thru 36
Topic archived. No new replies allowed.