C# :: Creating A Magic Square

Feb 5, 2015

My university assignment requires us to create a magic square. The definition of which can be found on Wikipedia and such, but essentially it's a (n) times (n) grid where all the row, column and diagonal totals meet the formula [n(n2 + 1)]/2. So a 3x3 would provide totals of 15 for example.

It's been requested that we use brute force for this, nothing mathematically fancy. I've succeeded in the task and initially during testing was coming up with a result of a 3x3 magic square after between 10 million and 1 billion attempts over a few minutes to nearly an hour.

I proceeded to refine my code and have as little as possible in the while loop which I'd break out of once the function had found a magic square, nothing really made much difference until I moved the declaration of my random to just before my while loop instead of within.

Random RanGenerator = new Random();

Once I'd done this I was getting 3x3 magic squares after a few thousand iterations in less than a second, which has completely baffled me. I can understand if it would save time, but how could that reduce the amount of attempts required to solve the magic square?

View 6 Replies


ADVERTISEMENT

C/C++ :: 3x3 Magic Square That Sums Up To 15 On All Sides

Mar 10, 2015

I have an assignment that I have to make a 3x3 magic square that sums up to 15 on all sides. I can't use arrays, just Loops and If and Else statements.

My first attempt was like this

//Program for printing magic square that sums up to 15.

#include <iostream>
using namespace std;
int main() {
int a, b, c, d, e, f, g, h, i;
int r1, r2, r3;
int c1, c2, c3;

[Code] .....

I can't think of a way to loop that when it prints out, it will have a sum of 15 on all sides and/or print one of the 8 solution for a 3x3 magic square.

View 3 Replies View Related

C# :: Creating Normal Magic Squares

Jan 13, 2014

I have been asked to develop a program with 6 methods which I have presented below. The aim of the program is to find and generate a magic square with a given dimension. This is a console program and so the 'Main' is also provided. However, I am having a problem with my code. When ever I try to generate a magic square it continuously cycles through 'forever' and I have never yet got a magic square; no matter what dimension I enter.

I must use methods 'CreateRandomlyAssignedArray' and 'CheckSquareMatrix'. There is another method 'SearchForValue', which we were told to creat. How this can be useful.

I have provided my code below:

class Program {
static Random rand = new Random();
static void Main(string[] args) {
int[,] array = new int[5,5];
array = GenerateMagicSquare(5);

[Code] ....

View 1 Replies View Related

C/C++ :: Displaying Filled Square Next To Hollow Square?

Feb 28, 2015

I am able to display a filled and hollow square by themselves, but I can't seem to be able to get them side by side.

So this is my code so far:

[/
#include <iostream>
using namespace std;
int main()

[Code]....

I can get the hollow square to show up, but the first square just shows up as a single line instead of a square. It seems that it ignores the first if statement in the second loop. I've tried not using an if statement in the second loop but it didn't seem to work either.

View 4 Replies View Related

C++ :: Draw A Square Within A Square Pattern

Oct 25, 2013

I know how to draw a square hollow or filled but i need to draw an alternating pattern ....

ex
iPatternHeight = 1
X

iPatternHeight = 3
OOO
OXO
OOO

[Code] .....

View 6 Replies View Related

C++ :: Magic Pairs - Integer Numbers

Dec 10, 2013

Alexandra has some distinct integer numbers a1,a2...an.

Count number of pairs (i,j) such that:
1≤ i ≤ n
1≤ j ≤ n
ai < aj

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer n denoting the number of numbers Alexandra has. The second line contains n space-separated distinct integers a1, a2, ..., an denoting these numbers.

Output

For each test case, output a single line containing number of pairs for corresponding test case.

Constraints

1 ≤ T ≤ 4
1 ≤ n ≤ 100000
0 ≤ ai ≤ 109
All the ai are distinct

Example

2
2
2 1
3
3 1 2

Output:
1
3

Explanation

Case 1: Only one such pair: (2,1)
Case 2: 3 possible pairs: (2,1), (2,3), (3,1)

as I understand the problem is just counting how many Ai's are 1 <= Ai <= N and then apply ((R*(R-1))/2), R is the count of valid Ai's

My actual code is

#include <stdio.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define getNumber getchar()
#define getString getchar()

[Code] ....

View 4 Replies View Related

C :: Magic Letter For Making A Constant Number Be Int?

Sep 26, 2014

For example if I have typed 0xFF (a literal hex number that represents the value 255 for Unsigned Char or -1 for Signed Char) in part of my program. That 0xFF is treated as a Char not an Int, because the value is within the range supported by Char, the C compiler always tries to use the smallest datatype possible for the number that is needed for a literal value like this.

Unfortunately because Signed Char is the default Char type, 0xFF is translated into -1. I am wanting to use it to represent 255. So I'm trying to tell the compiler that 0xFF should be interpreted as either an Int or an Unsigned Char. How do I do this?

I already tried typing it with the magic letter "I", like this: 0xFFI

But that didn't work. What is the correct way to do this?

View 4 Replies View Related

C++ :: PPM Image File Magic Number And Max Color Value

Feb 28, 2015

When reading a PPM Image file, how would I find the magic number and the max color value?

int main() {
string imageFileName;
ifstream image;

imageFileName = "C:UsersDesktopdrink.ppm";
image.open(imageFileName.c_str(), ifstream::binary);

[Code] ....

The code gives me seemingly logical integer values for size, width and height. But how would I find the max color value and so called "magic number" for this PPM image file?

View 1 Replies View Related

C# :: How To Draw A Square Within A Square

Oct 25, 2013

I know how to draw a square hollow or filled but I need to draw an alternating pattern

ex
iPatternHeight = 1
X

iPatternHeight = 3
OOO
OXO
OOO

[Code] ......

View 2 Replies View Related

C/C++ :: Can't Get The Right Square Pattern

Jul 4, 2014

I've been working on this code that should be looking like

1 3 5 3 1
3 5 7 5 3
5 7 9 7 5
3 5 7 5 3
1 3 5 3 1

but mine appears like this

1 3 5 3 1
3 5 7 5 3
5 7 9 7 5
3 3 5 7 9
1 3 1 3 5

This is the code I've got for now, I just don't know which logic I need to fix

int main(){
int r;
cout << "Rows?" << endl;
cin >> r;
for (int i = 1; i <= r; i+=2){
for (int j = i; j <= r+i; j+=2){
cout << j<< " ";
} for (int k = i+2; k >= i-1; k-=2){

[code]....

View 2 Replies View Related

C++ :: Square Root Of Number

Dec 9, 2013

i have written a function that gives me the square root of a number. Yes i know there is already a function in <cmath> that gives square roots but i thought it would be pretty challenging (and therefore fun) to try writing one on my own. I wrote my code while i was in school, listening to my Irish teacher ramble on and on about useless shit, so it's probably not the best possible way of finding the square root of a number.

double msqrt(double x) {
double adder=1;
double y = x;
for(int a=0; ; a++) {
if(y*y > x) y = y/2;
if(y*y <= x) break;

[code]....

View 6 Replies View Related

C++ :: How To Move Square To The Left

Apr 20, 2013

Having trouble getting my square to move to the left my code and instructions of what i am suppose to do is below. No sure how to move my square or if I am even going in the right direction as to writing code to do so. Note that it is only part of my code ( part 2 of project)

Part 2:

Write a graphical application that draws a square and then moves it.

Get the x and y coordinates for the top left corner of the square from the user using the get_int() member function of cwin. Get the length of a side of the square from the user using get_int() as well. Now draw the square to cwin according to the user input.Ask the user how many units to the left they want to move it using get_int() again. Then move the square, clear the screen, and draw it again.

// Part 2 //
/* command output to declare the x,y value and 1 side length of a square through user interaction( Has user input intergers) */
int x_value = cwin.get_int("What is the x_value of the top left of the square?");
int y_value = cwin.get_int("What is the y_value of the top left of the square?");
int side_length = cwin.get_int("Input the length for one side of the square:");
/* Data type for the 4 corners of the square */
Point e;
Point f;

[Code]...

View 2 Replies View Related

C++ :: MFC Drawing Circle Instead Of Square

Mar 11, 2014

Found a good beginner's tutorial to learn Visual C++: [URL] ....

How to change the drawing so that instead of squares, the game draws circles. Can figure out that using Ellipse in the code results in circle outlines, but the fill remains as square color shapes.

The drawing code from the tutorial is below. How to substitute filled circles for filled squares.

Code:

// CSameGameView drawing
void CSameGameView::OnDraw(CDC* pDC){
// First get a pointer to the document
CSameGameDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(!pDoc)

[Code] ....

View 6 Replies View Related

C :: Drawing A Shape (Square) Into PGM Image

Nov 22, 2013

I'm doing a project and I need to draw a square into a pgm image if certain conditions are met, but I'm not sure how to draw the square into the image.

View 2 Replies View Related

C :: How To Reflect A Square Matrix Diagonally

Jun 10, 2013

I have progressed to a point but I don't know what have I done wrong.

Code:

#include <stdio.h>#include <conio.h>
int main () {
int i, j, n, temp;

[Code].....

View 5 Replies View Related

C++ :: Display Remainder Of The Square Of Numbers From 100 To 10

Mar 31, 2013

Display the remainder of the square of numbers from 100 to 10. This square of numbers must be divisible by the numbers from 100 to 10 respectively. what i need to in this

View 3 Replies View Related

C++ ::  Calculating Sum Of Square Of Numbers Between Two Inputs

Mar 28, 2014

Write a function which takes two parameter of integer type and both parameters are positive integer. These number should be provided by the user of your program. The first parameter is lower than the second parameter. You function should be able to calculate the sum of the square of each of the numbers between the two inputs and should display that. Please write a main function to display the working of your function. Call the function at least three times by using some loop in the main function.

This is what I came up with but it is not correct:

#include <iostream>
using namespace std;
void sumsquares(int number1, int number2) {
int sum = 0;
for(int i = number1; i<number2; i++)
sum =sum+ i*i;
cout<<"The Result is: "<<sum<<endl;

[Code] .....

View 2 Replies View Related

C++ :: How To Square And Cube Number In Program

Feb 18, 2014

#include <iostream>
#include <conio.h>
#include <iostream>

[Code].....

I can't find a way to square and cube the number I want, which the number will show in the notepad. Is there a way to cube and square a number in my program??

View 5 Replies View Related

C++ :: Printing Square Free Numbers?

Feb 27, 2014

I'm currently trying to write a program that takes an integer input from the user and displays that number of square-free numbers. I've gotten most of the program to work, but I encounter an error during the noSquare function that causes it to print incorrectly.

A square-free number is a number that is not evenly divisible by the square of any prime number. It's hard to see the bold in the code area, but it begins after the "//I think below is where the problem is"

#include <iostream>
#include <vector>
using namespace std;

[Code]........

View 1 Replies View Related

C/C++ :: 2D Arrays Function - Largest Square?

Mar 25, 2015

In this assignment, you are required to write a function called largest square. Your function should take a 2 dimensional array, 2 integers which represent the number of rows and columns in the array, and prints out a 2 X 2 array which represents the largest square in the array. The largest square means the square for which the sum of its elements is the greatest in the array. Submit your function in a file called largestSquare.c

Example: Given the array 1 2 3 4

5 6 7 8

9 10 11 12 the output should be 7 8

11 12

Example 2: Given the array 8 2 6

3 7 3

6 1 1 the output should be 8 2

3 7

The prototype of your function is void largestSquare(int[rows][columns], int rows, int columns).

Am I anywhere close with what I've started?

//
// main.c
// largestsquare
//

#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
int rows, columns;

[Code] ...

View 5 Replies View Related

C/C++ :: Printing Square Free Numbers

Feb 27, 2014

I'm currently trying to write a program that takes an integer input from the user and displays that number of square-free numbers. I've gotten most of the program to work, but I encounter an error during the noSquare function that causes it to print incorrectly. I have bolded the area where I believe the problem is what I am doing wrong.

The problem with the program is that it prints ALL numbers from 1 to the input number instead of just the square-free.

A square-free number is a number that is not evenly divisible by the square of any prime number ....

#include <iostream>
#include <vector>
using namespace std;
bool noSquare (int num); //Function to determine if the number is square-free
vector<int> getPrimes(int num); //Function to get a vector of all primes below num
bool isPrime (int number); //Function to determine if individual numbers are prime
int main()

[Code]...

By the way, the in the middle of the code was me trying to bold a section, not an actual part of the code. I can't seem to find where to edit my original post.

View 3 Replies View Related

C/C++ :: Make Program To Square Numbers From 1 To 20

Oct 21, 2012

I want to make program to square the numbers from 1 to 20. i have written this program, it's running but not giving the required answer.i think it is giving a garbage value...

void main(void)
{int a;
  int  b;
b=a*a;
for (a=1; a<21; a++)
{printf("%d",b);
printf("the square is%d=b");
}
getche();}

View 2 Replies View Related

C++ :: Program To Find Transpose Of Given Square Matrix

Mar 24, 2013

I want to find transpos of square matrix.but my program does not run complete.

Code:
#include<iostream>
using namespace std;
const int size=3;
void read(int a[size][size])

[Code] .....

View 2 Replies View Related

C :: Program To Find The Square Root Of A Number

Dec 5, 2013

I am writing a program to find the square root of a number. I am using the Newton-Raphson method..Just to clarify a bit of the code.. fabs(1/(x+1)) < 0.001 is to check for relative error..

EXAMPLE: for user entry of 100 if the iteration process ends in 10.055 the answer will return as 10 which is correct. But it isn't doing that.

It compiles I then proceed to run it..prompts me "Enter a number to find the square root of: I type 100 then hit enter...

"The square root of 100 is -1077834936"

My first time writing a program from complete scratch.

And I know there is a sqrt() function...just wanted to write my own.

Code:

#include <stdio.h>
#include <math.h>
double mysqrt(double a);
int main()
{
double a, result;
printf("Enter a number to find the square root of: ");

[Code]...

View 1 Replies View Related

C :: Prototype Function Square Root Velocity

Mar 30, 2013

I am trying to computed the time it takes for a projectile to hit the ground. The problem is that i need to square the input of velocity before i do the calculation. the question I have is that if it's possible to have multiple arguments inside the brackets after main.

#include <stdio.h>
#include <math.h>
double distance (double a, double v, double g);
int square(int y);
double height(double v, double a, double g);
double time (double v, double a, double g);
double sqrt(double num);

[Code] ....

View 2 Replies View Related

C++ :: Find Area Of Room In A Given Square Maze

Apr 1, 2013

Area of the room. Your task is to write a program that will find the area of room in a given square maze.

Note. Use recursion for solving this problem.

Input:
First line contains only one number N (3 <= N <= 10).The number that describes the size of the square maze.
On the next N lines maze itself is inputed ('.' - empty cell,'*' - wall).
And the last line contains two numbers - the row and column of the cell in the room those area you need to find.It is guaranteed that given cell is empty and that the maze is surrounded by walls on all sides.

Output:
Output only one number - total amount of empty cells in the selected room.

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved