Write a C program that prints an n by n magic square

Assignment Help C/C++ Programming
Reference no: EM131121663

Question:
(Main issue is in bold, other parts is already coded)
Write a C program that prints an n by n "magic square" (a square arrangement of the numbers 1, 2, ..., n2 in which the sums of the rows, columns, and diagonals are all the same). The user will specify the value of n.
Store the magic square in a two dimensional array. Start by placing the number 1 in the middle of row 0. Place each of the remaining numbers 2, 3, ... , n2 by moving up one row and over one column. Any attempt to go outside the bounds of the array should "wrap around" to the opposite side of the array. For example, instead of storing the next number in row -1 (row negative 1), we would store it in row n - 1 (the last row). Instead of storing the next number in column n, we would store it in column 0. If a particular element is already occupied, put the number directly below the previously stored number.
The magic square only needs to be up to 11 by 11 and can only be created from odd numbers.
I want you to use functions for this project. I would like you to separately declare and define 3 separate functions. One for creating the magic square and putting it the two dimensional array.One for printing the magic square (for your printf, use 5 minimum field width).Finally, one for getting and returning the sum of the numbers in the magic square array.
Take in input in the main function. If the input is an even number or is a number not between 1 and 11, exit the program. Also, print the value of the sum of magic numbers via the main function, since your function to calculate this should only be returning a integer.
For the sum of all the numbers in the magic square array, I want you to use while loops. You can use either for loops or while loops for the print and create magic number functions.
HINT: The only function that needs to return something is the sum magic square function (integer). The other two function don't need to return anything. C is a pass by value language, meaning that it typically can't change the values of arguments in functions. This doesn't apply to arrays because they are references (more in chapter 12). That means one argument in the functions should be a two dimensional array. You will be able to operate on this array.
Sample I/O:
Enter size of magic square: 3
8 1 6
3 5 7
4 9 2
Sum of Magic Numbers: 45
Enter size of magic square: 2
Invalid Input: 2
Enter size of magic square: 103
Invalid Input: 103
Enter size of magic square: 7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
Sum of Magic Numbers: 1225

Reference no: EM131121663

Questions Cloud

What exactly do it yourself product relocating supplier next : What exactly do it yourself product relocating supplier next? Some do it yourself service company refers to professionals that supply a very restricted system.
Which components of this description correspond to the terms : The enzyme catalyzes the dehydration (release to air) of up to 107 CO2 molecules per second Which components of this description correspond to the terms enzyme, substrate, and turnover number?
Identify the benefits and drawbacks of using the capm : Explain how the CAPM assists in calculating the weighted average costs of capital (WACC) and its components.  Illustrate why some managers have difficulty applying the Capital Asset Pricing Model (CAPM) in financial decision making.
Market entry-market development-brand building activities : Develop a concise set of highly focussed marketing strategies for your most attractive and highest priority markets at Duke Energy. These strategies will guide your market entry, market development, and brand building activities. Establish budgets fo..
Write a C program that prints an n by n magic square : Write a C program that prints an n by n "magic square" (a square arrangement of the numbers 1, 2, ..., n2 in which the sums of the rows, columns, and diagonals are all the same). The user will specify the value of n.
Evaluate the validity and reliability of the data : Describe why each piece of evidence is relevant to patients, the community, and the hospital. Summarize the analysis used to create the recommendation. Evaluate the validity and reliability of the data
Mentions the theory of transaction cost analysis : 1. The chapter mentions the theory of transaction cost analysis (TCA). What role does TCA play in the decision to use a company sales force rather than independent manufacturers' agents?
Respond the - business ethics and stakeholder theory : Read the article titled, "Recognition, Reification, and Practices of Forgetting: Ethical Implications of Human Resource Management," located here. Be prepared to discuss. Next, analyze one (1) human resource ethical issue that you believe is promi..
What was the thickness of the resulting oil slick : By mistake, a quart of oil was dumped into a swimming pool that measures 25.0cm by 30.0cm (assume the oil spread entirely across the surface of the water in the pool)

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Write a function fromhosttonetwork in c

Write a function fromHosttoNetwork in C that will accept an integer variable and covert it from host byte order to network byte order and write a function fromNetworktoHost in C that will accept an integer in network byte order and return an integ..

  Functions to make a main program

write the required functions to make a main program that is already written

  Give brief definition of the database concept

First let us start with a brief definition of the Database concept. A database is a collection of data records stored in a computer in a systematic way. In this way, a computer program can be used to answer queries about the data stored in the dat..

  Implementing calculator functionality

Implementing Calculator Functionality. If when you load your project you have an error panel appear that tells you the Designer view could not be loaded, close this panel and right click on CalculatorForm.h in the Solution Explorer and select View ..

  Compute triangle area

Write a program with a form that contains three labels & text boxes (Base, Height & Area) in addition to a button labeled compute Triangle Area. The user must enter the base and height to find the area. (Triangle Area = 0. 5base x Height)

  What are the primary differences between object-oriented

1. what is instruction explosion?2. what are the primary differences between object-oriented programming languages and

  Create a program that draws a two-dimensional house

Create a program that draws a two-dimensional house seen from the front, the way a child would see it: with a door, two windows, and a roof with a chimney

  Write a c program that reads in five numbers

Q1. (Bar Chart /Histogram printing) Write a C program that reads in five numbers (each between 1 and 30). For each number read, your program should print a line containing that number of adjacent asterisks. For example, if your program reads the numb..

  Performance of disk memory system

Suppose a regional computer centre A wants to evaluate the performance of its disk memory system. One measure of performance is the average time between failures of its disk drive. To estimate this value, the centre recorded the time between failu..

  Write a driver program called testrationalnumber.cpp

Write a driver program called testRationalNumber.cpp to test each of the above functions and overloaded operators in the RationalNumber class.

  Write a function that collects integers from the user

Write a function that collects integers from the user until a 0 is encountered and returns them in a list in the order they were input (ML only).

  Write a program to determine the cost of painting a room

implement the program to find the cost of painting a room - Write a program to determine the cost of painting a room.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd