Question: C PROGRAM NEED SAME OUTPUT PLZ DONOT GIVE WRONG OUTPUT 1 - WAP to generate a n*n magic square Description: In recreational mathematics, a magic

C PROGRAM NEED SAME OUTPUT PLZ DONOT GIVE WRONG OUTPUT

1 - WAP to generate a n*n magic square

Description:

In recreational mathematics, a magic square is an arrangement of distinct numbers (i.e. each number is used once), usually integers, in a square grid, where the numbers in each row, and in each column, and the numbers in the main and secondary diagonals, all add up to the same number

A magic square has the same number of rows as it has columns, and in conventional math notation, "n" stands for the number of rows (and columns) it has. Thus, a magic square always contains n2 numbers, and its size (the number of rows [and columns] it has) is described as being "of order n".

Example: if n = 3, the magic square

8 1 6

3 5 7

4 9 2

Read an odd number n from user.

Do error checking.

Check the number is odd or not.

If not, continue step a.

Create an n X n matrix.

Insert 1 to (n * n) numbers into matrix.

Arrange the numbers in such a way that, adding the numbers in any direction, either row wise column wise or diagonal wise, should result in same answer.

Allocate the memory dynamically using calloc.

Pr-requisites:-

Loops

Arrays

Pointers

DMA

Functions

Objective: -

To understand the concept of 2D Arrays

Pointers on 2d arrays

Inputs: -

An integer N

Sample execution: - Test Case 1: user@emertxe] ./magic_square Enter a number: 3 8 1 6 3 5 7 4 9 2 Test Case 2: Enter a number: 6 Error : Please enter only positive values

requested file

#include

void magic_square(int **, int);

int main() { }

///

2 - WAP to implement fragments using Array of Pointers

Description:

Implement fragments using array of pointers.

Rows are static and columns are dynamic. Fixed no.of rows and columns will vary for each row.

Example:

Read no.of rows from user and allocate the memory statically for rows.

Read no.of columns for each row and allocate the memory dynamically.

Let us Assume, Row = 3.

Row[0] = 4 columns, Row[1] = 3 columns and Row[2] = 5 columns.

While allocating the memory for columns you have allocate for no.of columns + 1 dynamically.

After that read the values from user and calculate the average for each row seperatly and store that average in that extra memory block which you added while allocating the memory.Sample Execution: Test case 1: Enter no.of rows : 3 Enter no of columns in row[0] : 4 Enter no of columns in row[1] : 3 Enter no of columns in row[2] : 5 Enter 4 values for row[0] : 1 2 3 4 Enter 3 values for row[1] : 2 5 9 Enter 5 values for row[2] : 1 3 2 4 1

Before sorting output is:

1.000000 2.000000 3.000000 4.000000 2.500000

2.000000 5.000000 9.000000 5.333333

1.000000 3.000000 2.000000 4.000000 1.000000 2.200000

After sorting output is:

1.000000 3.000000 2.000000 4.000000 1.000000 2.200000

1.000000 2.000000 3.000000 4.000000 2.500000

2.000000 5.000000 9.000000 5.333333

requeted file

#include

int fragments(int, int *[]);

int main() { }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!