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


ADVERTISEMENT

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 View Related

C/C++ :: Can Make Squares Appear Inside The Other

Mar 21, 2015

[URL] ....

Can I make these squares appear around the other instead of having them appear under the others. Also if it can be done how can I make it so there is a space between the squares

int main() {
char a;
int size = 0;
cout << "Enter a character: " << endl;
cin >> a;
while (size < 1 || size > 10)

[Code] ....

View 1 Replies View Related

C++ :: How To Calculate Total Surface Covered - Overlapping Squares

May 24, 2013

How would i calculate a surface that up to 10^5 squares cover. The info you get is xpos,ypos and width for each square.

How would you calculate the total surface covered (if any overlap only one counts).

Cannot use a big array of bools as the memory limit is 512 MiB.

View 2 Replies View Related

C++ :: Pointer Arrays - Program To Simulate Squares Most Landed On

Jun 8, 2014

I have not been using pointers that long i most of my stuff has not needed them but now they do i need to learn how to use them more. I'm writing a program that simulates the squares most landed on(for fun) and i cant get a method to work because of an array pointer.

The error is no matching function for call to 'GameLoop::Loop(int, int (*)[4])'

Here is the latest version of the code. I've been editing a lot of things so there might be some stupid errors but I've tring everything.

rolling.h and rolling.cpp are just some dice function they are working perfect.

main.cpp

#include <iostream>
#include "Rolling.h"
#include "GameLoop.h"
Rolling Roll;
GameLoop Game;

[Code] ....

View 2 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/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++ :: Display Random Sequence Of Colored Squares - Matching Color

Sep 13, 2014

I have to write a program for school that displays a random sequence of colored squares, each square is a different color. Then the squares disappear and the 5 more pop up at the bottom. The user has to guess which color was first, second, and so on. They click on the squares on the bottom that matches the first square on the top.

My question is how do you get the computer to say ok this square (that the user clicked on) matches this square here.

is there a command that says something like if this color matches this color?

View 2 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++ :: Getting A Point Rotation Or Normal From 4 Vectors?

Dec 31, 2014

I'm trying to get a rotation of a spefic point in a 3D space using 3 or 4 coordinate/vector.

For example, I want to know the rotation of (5,5,5)

Using 4 vectors for example (0,0,0), (10,10,10), or 10,0,10, etc. (3 minimum)

View 2 Replies View Related

C :: Normal Distribution Histogram - Random Numbers

Feb 6, 2013

Write a function that generates 1000 normally distributed (Gaussian Probability Distribution) random numbers. Range should be between -3 and +3. Numbers should be double floating point.

There's more to it than that, but I've got it from there.

View 7 Replies View Related

C :: Difference Between Functions As Pointer And Normal Function

Aug 30, 2014

whats the difference between functions as pointer and normal function, eg:

void function1(void)
void *function1(void)

What is the difference between the two?I'm doing parallel programming and we use pointer functions (void *function1(void)) when calling threads. I want to why it is done.

View 6 Replies View Related

C++ :: Normal Rand Generator - Class Functions

Sep 19, 2014

So I have an object of class NRG (Normal Rand Generator) which takes, as an argument to it's constructor, an object of class RG (Rand generator).

template<typename RG>
class NRG {
RG rg;
public:
NRG(RG r):rg(r){}
double operator()();

An object of this class will return a normal random when it's member function operator()() is called:

template<typename RG>
double class NRG::operator()() {
static int flag = 0;
static double N2 = 0.0;
if(flag==0)

[Code] ....

However, when I run this I get an error which says:

C:UsersavadhootDesktopb.cpp|69|error: 'template<class RG> class NRG' used without template parameters|
C:UsersavadhootDesktopb.cpp|69|error: expected identifier before 'operator'|
C:UsersavadhootDesktopb.cpp|69|error: two or more data types in declaration of 'operator()'|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

View 3 Replies View Related

C++ :: Generating Random Number Of Normal Distribution

Mar 12, 2013

I am generating random number of normal distribution (0,1) but i suspect maybe I have done it wrong. The generator I use right now is

srand(time(0));
std::random_device rd;
std::mt19937 gen(rd());
std::default_random_engine generator;
std::normal_distribution<double> distribution(0,1);

Am I doing the right thing? Whether this is a real random number generator?

View 4 Replies View Related

C++ :: Find Normal To A Terrain Represented By A Texture

Mar 27, 2014

The following function finds the normal to a terrain represented by a texture. I found it somewhere online , it works but i couldn't understand the math behind it. So , How (or Why ?) does it works ?

//psuedo code
Vector2 normal(x,y) {
Vector2 avg;
for(int w = -3; w <= 3; w++) {
for(int h = -3; h <= 3; h++)

[Code] ....

View 6 Replies View Related

C++ :: Converting Cmdline Into Normal Function So Can Put Into Library

Apr 23, 2013

I need to convert this code into a normal function that can be put into a library.

Code:

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

[Code].....

Like on line 21&22, I need to get the stuff from within the program, not through cmdline.

View 1 Replies View Related

C :: Convert Roman Numeral Inputs To Normal Numbers?

May 4, 2014

I have a fascination with Roman numerals and would like to create a C program that converts Roman numeral inputs to normal numbers.

I want to keep everything simple and only use the C library for the coding.

View 4 Replies View Related

C :: How To Create A Document Where All Text Is Normal But One Word Is Flashing

Feb 18, 2013

So i have to create a document where all the text is normal, but on the sides one word, a important word flashes...

how would i code that in c ?????????

oh yeah i am useing complier Bloodshed Dev C++ on windows 7

View 9 Replies View Related

C++ :: How To Generate Real Random Number From Normal Distribution

Jun 26, 2013

I want to generate random numbers of normal distribution and use scentence like

std::default_random_engine generator;
std::normal_distribution<double> distribution(0,1);
arrayX[i][j]=distribution(generator);

But I find that each time the array I got are the same. So how should I generate random numbers with different seedings with normal distribution?

View 2 Replies View Related

C++ :: Produce Normal Distribution Random Number Several Times

Aug 22, 2014

I wrote the following program shown below that produces a normally distributed random number several times, and then takes the average of them. The problem that I have been having though is that the output it has been producing is 0.0288385 despite the fact that I set the mean of the normal distribution to be 0.1. Why this output value would be so far off from 0.1 despite having averaged over such a large number of random numbers namely 10,000 of them? Also, how to randomly seed this random number generator such that it gives a different value each time its run perhaps by seeding it with the windows timer? Below is the program.

#include <iostream>
#include <random>
using namespace std;
int main() {
default_random_engine generator;
normal_distribution<double> distribution1(0.1,3.0);
double number1,sum,n;

[code].....

View 2 Replies View Related

C++ :: Normal Distribution Range - Select Objects On A Vector

May 23, 2014

I'm implementing an normal_distribution to select objects on a vector. The thing is I can't use values greater then 1 or less then -1. Here is what could be done:

gausx=gaus_distributionx(generator);
while((gausx>1) || (gausx<-1))
gausx=gaus_distributionx(generator);

The question is if the distribution would loose it's power to be a normal distribution, because some of the gerated numbers wouldn't be used. Any way to set ranges for the distribution?

View 6 Replies View Related

C/C++ :: Unable To Calculate Normal Vertex For Terrain Mesh

Apr 17, 2014

Okay I'm trying to calculate vertex normals for my terrain mesh. Obviously it's not working, but I can't work out why. Either its the way I calculate the normals, or the way I assign the final normal.

line 416 is where the normals are assigned to each point in my mesh. line 735 is the function for normals

class Normal3d {
private:
double x;
double y;
double z;
public:
Normal3d(double nx, double ny, double nz) : x(nx), y(ny), z(nz) {
}
Normal3d() {};

[Code]...

View 6 Replies View Related

C++ :: Unique Form Of Binary Tree - Recursion With Normal Distribution

Nov 8, 2013

I need creating a unique form of binary tree. I will eventually drop 256 balls into this to see the distribution, but its creating a pascals triangle recursively.

To create it I need to use two classes, a tree and a node class, and friend them create it recursively.

View 2 Replies View Related

C++ :: Program To Convert Normal Text To Mobile Text

Mar 26, 2013

Program that reads in a normal text file and converts it into mobile phone text. that is if the word is 3 characters or less then ther is no changes to the word and if the word is four or more letters then remove all the vowels from the word except for vowels that are capitals.

View 5 Replies View Related

C++ ::  Creating A New Directory?

May 11, 2013

How do you create a new directory in C++? I would like to do this using the standard library and no external, third party or non standard libraries/headers. I need to do this without calling system() or system("mkdir").

View 7 Replies View Related







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