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.
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..
// Create New Array.cpp : main project file.
#include <iostream>
#include <ctime> // For srand()
usingnamespace 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 = newint*[SIZE];
for (int i = 0; i < SIZE; ++i)
{
ary[i] = newint[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.
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 = newint*[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