Click here to Skip to main content
15,887,946 members
Home / Discussions / C#
   

C#

 
GeneralRe: Need help for perform some operation parallely Pin
Ron Beyer21-Jan-14 2:26
professionalRon Beyer21-Jan-14 2:26 
GeneralRe: Need help for perform some operation parallely Pin
GuyThiebaut21-Jan-14 5:41
professionalGuyThiebaut21-Jan-14 5:41 
GeneralRe: Need help for perform some operation parallely Pin
GuyThiebaut21-Jan-14 6:27
professionalGuyThiebaut21-Jan-14 6:27 
GeneralRe: Need help for perform some operation parallely Pin
Ron Beyer21-Jan-14 7:01
professionalRon Beyer21-Jan-14 7:01 
QuestionHelp writing a program which adds up values in a 2D array to determine if it is a magic square Pin
Member 1052812320-Jan-14 3:43
Member 1052812320-Jan-14 3:43 
AnswerRe: Help writing a program which adds up values in a 2D array to determine if it is a magic square Pin
Peter Leow20-Jan-14 4:20
professionalPeter Leow20-Jan-14 4:20 
AnswerRe: Help writing a program which adds up values in a 2D array to determine if it is a magic square Pin
OriginalGriff20-Jan-14 5:50
mveOriginalGriff20-Jan-14 5:50 
AnswerRe: Help writing a program which adds up values in a 2D array to determine if it is a magic square Pin
BillWoodruff20-Jan-14 9:08
professionalBillWoodruff20-Jan-14 9:08 
You do realize that if you generate a #Nx#N matrix of random numbers the chance the result will satisfy the conditions for being a magic square are very low ... and, that as the size of the matrix increases, the chances get exponentially less and less ?

For validating whether your #Nx#N matrix is a magic square: traversing it using for-loops is quite straightforward, if tedious, but it's a good exercise to improve your mastery of for-loops to implement, I think. If you are new to C#, you might benefit from actually writing out the series of for-loop indexes you require for each test case (rows, columns, right-left diagonal, left-right diagonal).

If you are studying Linq and switch to using something like a List<List<int>> to hold your matrix, then Linq's operators like 'Sum can make validation much easier.
C#
// requires System.Linq
// requires System.Collections.Generic

// test the rows of the Magic Square
private bool testMagic(List<List<int>> testSquare)
 {
     // get first [0] row sum
     int firstRowSum = testSquare[0].Sum();

     // check rows #1 to #n
     for (int i = 1; i < magicNSides; i++)
     {
         // stop immediately on mis-match
         if (testSquare[i].Sum() != firstRowSum) return false;
     }

     return true;
}
For testing the Rows using Linq it can be even simpler:
bool IsMagicRows = squareList.All(x => (x.Sum() == firstRowSum));
Whichever strategy you use, validation in this case, imho, is best done in a way where you do a series of tests, and stop testing the moment any one of them fails.

For example, if you calculate the sum of the items on the first row of your array (or in the first List<string> inside your List<List<int>>) then you can start testing the other rows and stop immediately if any row does not have a sum equal to the sum of the first row.
“But I don't want to go among mad people,” Alice remarked.

“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”

“How do you know I'm mad?” said Alice.

“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll


modified 20-Jan-14 15:24pm.

QuestionCustom Data Types Pin
sunsilk1020-Jan-14 3:04
sunsilk1020-Jan-14 3:04 
AnswerRe: Custom Data Types Pin
Manfred Rudolf Bihy20-Jan-14 3:25
professionalManfred Rudolf Bihy20-Jan-14 3:25 
AnswerRe: Custom Data Types Pin
BillWoodruff20-Jan-14 5:13
professionalBillWoodruff20-Jan-14 5:13 
AnswerRe: Custom Data Types Pin
Freak3020-Jan-14 23:30
Freak3020-Jan-14 23:30 
QuestionC# with Oraclei Pin
Nightbird.14319-Jan-14 21:00
Nightbird.14319-Jan-14 21:00 
AnswerRe: C# with Oraclei Pin
V.19-Jan-14 21:49
professionalV.19-Jan-14 21:49 
QuestionMulti Desktop app, Need to take Screen short of a given desktop using handle. Pin
ptr_Electron19-Jan-14 19:23
ptr_Electron19-Jan-14 19:23 
AnswerRe: Multi Desktop app, Need to take Screen short of a given desktop using handle. Pin
Mycroft Holmes19-Jan-14 21:02
professionalMycroft Holmes19-Jan-14 21:02 
GeneralRe: Multi Desktop app, Need to take Screen short of a given desktop using handle. Pin
ptr_Electron20-Jan-14 0:40
ptr_Electron20-Jan-14 0:40 
AnswerRe: Multi Desktop app, Need to take Screen short of a given desktop using handle. Pin
Bernhard Hiller20-Jan-14 3:47
Bernhard Hiller20-Jan-14 3:47 
GeneralRe: Multi Desktop app, Need to take Screen short of a given desktop using handle. Pin
ptr_Electron20-Jan-14 7:11
ptr_Electron20-Jan-14 7:11 
QuestionSuggestion required for Accounting Software core infrastructure VS2010 C# MySql Pin
ahmed_one19-Jan-14 18:48
ahmed_one19-Jan-14 18:48 
AnswerRe: Suggestion required for Accounting Software core infrastructure VS2010 C# MySql Pin
Mycroft Holmes19-Jan-14 20:57
professionalMycroft Holmes19-Jan-14 20:57 
GeneralRe: Suggestion required for Accounting Software core infrastructure VS2010 C# MySql Pin
ahmed_one19-Jan-14 21:11
ahmed_one19-Jan-14 21:11 
GeneralRe: Suggestion required for Accounting Software core infrastructure VS2010 C# MySql Pin
Trak4Net20-Jan-14 9:01
Trak4Net20-Jan-14 9:01 
GeneralRe: Suggestion required for Accounting Software core infrastructure VS2010 C# MySql Pin
ahmed_one20-Jan-14 17:05
ahmed_one20-Jan-14 17:05 
Questioncreate and use .DLL Pin
DrooBo19-Jan-14 6:28
DrooBo19-Jan-14 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.