Magic Square C++

This is my C++ project homework. I am required to follow the instruction below. And I really need helps on fixing my existing codes.
By Professor:
Your program prompts the user for a positive odd integer, n, the dimension of the magic square. If n is not a positive odd number less than 1000, the program displays a message indicating that it can only generate magic squares for positive odd integers less than 1000. If n is a positive odd integer less than 1000, the user is prompted for an output file name. The magic square is then generated and displayed in tabular format on the screen and written to the ouput file. Be sure to close the file stream object, in the main function, that you create to write to the file. Also, be sure to zero out the array that will store the entries of magic square prior to generating the magic square. To print the magic square, use a half loop: a loop that has an if statement in it. Whenever the loop counter is divisible by n, the dimension of the magic square, go to the next line. Also, open the output files that the program creates to verify that the magic square is being written to them.



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

/**
 * zeroes out the entries of the specified array
 * @param magic a 1-D array that will store the entries of a
 *        magic square.
 * @param dim an odd positive integer representing the
 *        dimension of the magic square to be generated
 */

void initialize (int magic[],int dim)
{
    magic[1000000] = 0;
}

/**
 * generates a magic square with the specified dimension
 * and stores its entries in the specified array
 * @param magic a 1-D array containing the entries of the magic square.
 * @param dim an odd positive integer representing the dimension of the magic square to be generated
 */

void generate(int magic[],int dim)
{
    int row = dim - 1;
    int col = dim % 2;
    int i;
    int magicsquare[row][col];
    for (i = 1;i < dim*dim + 1; i++ )
    {
        magicsquare[row][col] = i;
        if ( magicsquare[row][col] == 0 )
        {
            row = (row + 1) % dim;
            col = (col + 1) % dim;
        }
        else
        {
            row = (row + dim - 2) % dim;
            col = (col + dim - 1) % dim;
        }
    }
}

/**
 * displays a magic square in tabular format on the screen.
 * @param magic a 1-D array containing the entries of the magic square.
 * @param dim an odd positive integer representing the dimension of the magic square to be generated
 */
void print(const int magic[],int dim)
{
    int row;
    int col;
    int magicsquare [row][col];
    for(row = 0; row < dim; row ++)
    {
        for(col = 0; col < dim; col++)
        {
            cout << setw(5) << magicsquare[row][col];
        }
        cout << endl;
    }
    cout<<"A "<<dim<<"x "<<dim<<" Magic Square"<<endl;
}

/**
 * writes a magic square in tabular format to the specified
 * output file stream
 * @param magic a 1-D array containing the entries of the magic square.
 * @param dim an odd positive integer representing the dimension of the magic square to be generat
 * @param outFile output file stream
 */

void write2File(const int magic[],int dim, fstream outFile)
{
    outFile<< "Writing this to a file.\n";
    outFile.close();
}


int main ()
{
    int n;
    int magic[1000000];
    string fileName;
    cout<<"Enter the dimension of the magic square -> ";
    cin>>n;
    if (n > 1000 || n%2 == 0)
        cout<<"The program can only generate a magic square whose dimension is a positive odd number less than 1000."<<endl;
    else
    {
        initialize (magic,n);
        generate (magic,n);
        cout<<"Enter the name of the output file -> ";
        cin>>fileName;
        ofstream outFile;
        outFile.open(fileName.c_str(),ios::out);
    }
    return 0;
}

Hi,

Some questions for you:

Put a for loop into your initialize function that prints out the values in the array up to magic[5] say. Does it verify they are zero?

On line 32, what is the value of col when the user enters 99 (or any other number for that matter) would the array still be square? Look in the reference to see what the % operator does.

Investigate how to create a dynamically sized array using new. With normal arrays, the size must be known and constant at compile time.

On line 35, the for loop starts at 1, so magicsquare[0][0] will not have a value set. for loops should start at 0, because arrays start at 0.

Hopefully this will help you a bit.

In the future, can you please post you compiler errors, or specify exactly what problems you are having :+)
Last edited on
Line 19 is like writing
1
2
int arr[3];
arr[42] = 7;

* It does not "initialize" the array.
* It writes to a memory location that is not part of the array.

Then look at
int arr[3] {};
All three actual elements are value-initialized, which for int happens to mean 0.


Rather than using new (which requires appropriate use of delete), do use std::vector. Dynamic allocation and can be used almost like an array, but has much more useful, convenient, and safe features.
Here below is the compiler errors. I am a cs beginner and I really need helps on this project please...

magicmaker.cpp:109:15: warning: missing terminating " character
magicmaker.cpp:109: error: missing terminating " character
magicmaker.cpp:110:51: warning: missing terminating " character
magicmaker.cpp:110: error: missing terminating " character
magicmaker.cpp: In function ‘void initialize(int*, int)’:
magicmaker.cpp:30: error: expected primary-expression before ‘{’ token
magicmaker.cpp:30: error: expected `;' before ‘{’ token
magicmaker.cpp: In function ‘int main()’:
magicmaker.cpp:110: error: ‘dimension’ was not declared in this scope
magicmaker.cpp:110: error: expected `;' before ‘is
Hi,

Can you edit your code, so the error messages match the code you have above. Your code does not have line 109,110.

I don't see the problems the compiler is talking about - are you sure the code is the same?

magicmaker.cpp: In function ‘void initialize(int*, int)’:
magicmaker.cpp:30: error: expected primary-expression before ‘{’ token
magicmaker.cpp:30: error: expected `;' before ‘{’ token


The initialize function starts on line 17, the error is on line 30?
Topic archived. No new replies allowed.