• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 09:34
CET 14:34
KST 22:34
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
Team Liquid Map Contest #22 - Presented by Monster Energy5ByuL: The Forgotten Master of ZvT30Behind the Blue - Team Liquid History Book19Clem wins HomeStory Cup 289HomeStory Cup 28 - Info & Preview13
Community News
Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool29Weekly Cups (March 9-15): herO, Clem, ByuN win32026 KungFu Cup Announcement6BGE Stara Zagora 2026 cancelled12Blizzard Classic Cup - Tastosis announced as captains18
StarCraft 2
General
Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool Team Liquid Map Contest #22 - Presented by Monster Energy Serral: 24’ EWC form was hurt by military service Weekly Cups (March 9-15): herO, Clem, ByuN win Weekly Cups (August 25-31): Clem's Last Straw?
Tourneys
RSL Season 4 announced for March-April Sparkling Tuna Cup - Weekly Open Tournament WardiTV Team League Season 10 KSL Week 87 [GSL CK] #2: Team Classic vs. Team Solar
Strategy
Custom Maps
Publishing has been re-enabled! [Feb 24th 2026] Map Editor closed ?
External Content
The PondCast: SC2 News & Results Mutation # 517 Distant Threat Mutation # 516 Specter of Death Mutation # 515 Together Forever
Brood War
General
Gypsy to Korea ASL21 General Discussion JaeDong's form before ASL BGH Auto Balance -> http://bghmmr.eu/ BSL Season 22
Tourneys
[BSL22] Open Qualifiers & Ladder Tours [Megathread] Daily Proleagues Small VOD Thread 2.0 IPSL Spring 2026 is here!
Strategy
Simple Questions, Simple Answers Soma's 9 hatch build from ASL Game 2 Fighting Spirit mining rates
Other Games
General Games
Nintendo Switch Thread General RTS Discussion Thread Path of Exile Stormgate/Frost Giant Megathread Dawn of War IV
Dota 2
Official 'what is Dota anymore' discussion The Story of Wings Gaming
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Five o'clock TL Mafia Mafia Game Mode Feedback/Ideas Vanilla Mini Mafia TL Mafia Community Thread
Community
General
US Politics Mega-thread Canadian Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Mexico's Drug War
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Req][Books] Good Fantasy/SciFi books [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion Tokyo Olympics 2021 Thread General nutrition recommendations Cricket [SPORT]
World Cup 2022
Tech Support
Laptop capable of using Photoshop Lightroom?
TL Community
The Automated Ban List
Blogs
Funny Nicknames
LUCKY_NOOB
Money Laundering In Video Ga…
TrAiDoS
Iranian anarchists: organize…
XenOsky
FS++
Kraekkling
Shocked by a laser…
Spydermine0240
Unintentional protectionism…
Uldridge
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 3875 users

The Big Programming Thread - Page 256

Forum Index > General Forum
Post a Reply
Prev 1 254 255 256 257 258 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
Arnstein
Profile Blog Joined May 2010
Norway3381 Posts
February 25 2013 18:23 GMT
#5101
On February 25 2013 17:58 nunez wrote:
Show nested quote +
On February 25 2013 17:36 Arnstein wrote:
Yes, that's the line that is messing up! I don't know how to do this inside of a operator overload function, so I just copied some stuff from the header in the class, and also tried to copy something from the book. In the book you had leftHandSide and rightHandSide, but it didn't seem like these were declared. Then how does C++ know which one they mean?


i am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question!

you've got all the info you need to solve that exercise lined up in just the right order the institution you are attending deems best. i think you need to trace back a couple of exercises and make sure you understand what the words in the code you are compiling mean (in this context)!


Hmm, it seems like I've done it right all along, but that const messed it up for me! I just removed const, and everything was fine :D

I feel like a programming god, mohahaha!
rsol in response to the dragoon voice being heard in SCII: dragoon ai reaches new lows: wanders into wrong game
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-02-25 19:16:49
February 25 2013 19:15 GMT
#5102
Guys could you please tell me or show a link with an algorithm to find
the greatest product of two adjacent numbers in any direction (up, down, left, right, or diagonally)
? I've asked Google, but I didn't find something useful enough.

Edit: within an n x n grid
Zeke50100
Profile Blog Joined February 2010
United States2220 Posts
February 25 2013 19:26 GMT
#5103
On February 26 2013 04:15 darkness wrote:
Guys could you please tell me or show a link with an algorithm to find
Show nested quote +
the greatest product of two adjacent numbers in any direction (up, down, left, right, or diagonally)
? I've asked Google, but I didn't find something useful enough.

Edit: within an n x n grid


You could just try doing it yourself - a special algorithm shouldn't really be necessary. If you have a number at position (x, y), you can find all eight adjacent numbers with (x-1, y) - if (0, 0) is the top-left, then this is the number directly to the left - and so on. Multiply each adjacent number with the number at (x, y), and you have the product between a number and its adjacent numbers.

Add in checks to make sure you aren't trying to multiply by a number that doesn't exist (e.g., there are no numbers to the left of (0, y)), and you should be good to go.
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
Last Edited: 2013-02-25 19:35:27
February 25 2013 19:33 GMT
#5104
On February 26 2013 04:15 darkness wrote:
Guys could you please tell me or show a link with an algorithm to find
Show nested quote +
the greatest product of two adjacent numbers in any direction (up, down, left, right, or diagonally)
? I've asked Google, but I didn't find something useful enough.

Edit: within an n x n grid

Write an O(n) search algorithm. Your operation would be a function like so:

int32 CheckIndex( uint32 i )
{
check left
right
up
down
diagonals
return largest
}


Call this operation on each index. Be sure to make sure you don't index out of the array on fringe elements. Then with the results just keep track of the largest returned value.
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
February 25 2013 20:29 GMT
#5105
On February 26 2013 04:33 CecilSunkure wrote:
Show nested quote +
On February 26 2013 04:15 darkness wrote:
Guys could you please tell me or show a link with an algorithm to find
the greatest product of two adjacent numbers in any direction (up, down, left, right, or diagonally)
? I've asked Google, but I didn't find something useful enough.

Edit: within an n x n grid

Write an O(n) search algorithm. Your operation would be a function like so:

int32 CheckIndex( uint32 i )
{
check left
right
up
down
diagonals
return largest
}


Call this operation on each index. Be sure to make sure you don't index out of the array on fringe elements. Then with the results just keep track of the largest returned value.


This can be optimized with memoization since you would end up doing two multiplications per product by checking every index.

Alternatively, you can go through each direction (horizontal, vertical, diagonal left to right, and diagonal right to left, and multiply each pair one after another, saving the largest result. Compare the results from every direction, and choose the largest. If you need to know the index as well, then save that whenever you have a new maximum in a direction.


int arr[N][N];
for(i:0->N-1) {
for(j:0->N-1) {
horizontalMax = max(horizontalMax,arr[i][j]*arr[i][j+1];
verticalMax = max(verticalMax,arr[i][j]*arr[i+1][j];
//diagnonals....
}
}
//fix the edge cases as well


Any sufficiently advanced technology is indistinguishable from magic
obesechicken13
Profile Blog Joined July 2008
United States10467 Posts
February 25 2013 23:32 GMT
#5106
On February 26 2013 05:29 RoyGBiv_13 wrote:
Show nested quote +
On February 26 2013 04:33 CecilSunkure wrote:
On February 26 2013 04:15 darkness wrote:
Guys could you please tell me or show a link with an algorithm to find
the greatest product of two adjacent numbers in any direction (up, down, left, right, or diagonally)
? I've asked Google, but I didn't find something useful enough.

Edit: within an n x n grid

Write an O(n) search algorithm. Your operation would be a function like so:

int32 CheckIndex( uint32 i )
{
check left
right
up
down
diagonals
return largest
}


Call this operation on each index. Be sure to make sure you don't index out of the array on fringe elements. Then with the results just keep track of the largest returned value.


This can be optimized with memoization since you would end up doing two multiplications per product by checking every index.

Alternatively, you can go through each direction (horizontal, vertical, diagonal left to right, and diagonal right to left, and multiply each pair one after another, saving the largest result. Compare the results from every direction, and choose the largest. If you need to know the index as well, then save that whenever you have a new maximum in a direction.


int arr[N][N];
for(i:0->N-1) {
for(j:0->N-1) {
horizontalMax = max(horizontalMax,arr[i][j]*arr[i][j+1];
verticalMax = max(verticalMax,arr[i][j]*arr[i+1][j];
//diagnonals....
}
}
//fix the edge cases as well



You mispelled memorization
+ Show Spoiler +
jkjk
How do you think of this? Does your brain just go like "huh, big numbers... how do I reduce?"
Albeit I don't think a simple multiplication is enough to warrant such steps.
I think in our modern age technology has evolved to become more addictive. The things that don't give us pleasure aren't used as much. Work was never meant to be fun, but doing it makes us happier in the long run.
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2013-02-25 23:44:04
February 25 2013 23:42 GMT
#5107
On February 26 2013 08:32 obesechicken13 wrote:
Show nested quote +
On February 26 2013 05:29 RoyGBiv_13 wrote:
On February 26 2013 04:33 CecilSunkure wrote:
On February 26 2013 04:15 darkness wrote:
Guys could you please tell me or show a link with an algorithm to find
the greatest product of two adjacent numbers in any direction (up, down, left, right, or diagonally)
? I've asked Google, but I didn't find something useful enough.

Edit: within an n x n grid

Write an O(n) search algorithm. Your operation would be a function like so:

int32 CheckIndex( uint32 i )
{
check left
right
up
down
diagonals
return largest
}


Call this operation on each index. Be sure to make sure you don't index out of the array on fringe elements. Then with the results just keep track of the largest returned value.


This can be optimized with memoization since you would end up doing two multiplications per product by checking every index.

Alternatively, you can go through each direction (horizontal, vertical, diagonal left to right, and diagonal right to left, and multiply each pair one after another, saving the largest result. Compare the results from every direction, and choose the largest. If you need to know the index as well, then save that whenever you have a new maximum in a direction.


int arr[N][N];
for(i:0->N-1) {
for(j:0->N-1) {
horizontalMax = max(horizontalMax,arr[i][j]*arr[i][j+1];
verticalMax = max(verticalMax,arr[i][j]*arr[i+1][j];
//diagnonals....
}
}
//fix the edge cases as well



You mispelled memorization


jkjk
How do you think of this? Does your brain just go like "huh, big numbers... how do I reduce?"
Albeit I don't think a simple multiplication is enough to warrant such steps.


Just to clarify, my solution doesn't involve memoization, it is an alternate algorithm which just doesn't need to compute it twice.

Memoizing would be taking the previously mentioned implementation, and skipping calculations that you have already done. When you take that code, and reduce it, it turns into the algorithm I mentioned. There is not a magic trick, I just don't like it when code repeats itself.
Any sufficiently advanced technology is indistinguishable from magic
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
February 26 2013 00:28 GMT
#5108
On February 26 2013 08:32 obesechicken13 wrote:
Show nested quote +
On February 26 2013 05:29 RoyGBiv_13 wrote:
On February 26 2013 04:33 CecilSunkure wrote:
On February 26 2013 04:15 darkness wrote:
Guys could you please tell me or show a link with an algorithm to find
the greatest product of two adjacent numbers in any direction (up, down, left, right, or diagonally)
? I've asked Google, but I didn't find something useful enough.

Edit: within an n x n grid

Write an O(n) search algorithm. Your operation would be a function like so:

int32 CheckIndex( uint32 i )
{
check left
right
up
down
diagonals
return largest
}


Call this operation on each index. Be sure to make sure you don't index out of the array on fringe elements. Then with the results just keep track of the largest returned value.


This can be optimized with memoization since you would end up doing two multiplications per product by checking every index.

Alternatively, you can go through each direction (horizontal, vertical, diagonal left to right, and diagonal right to left, and multiply each pair one after another, saving the largest result. Compare the results from every direction, and choose the largest. If you need to know the index as well, then save that whenever you have a new maximum in a direction.


int arr[N][N];
for(i:0->N-1) {
for(j:0->N-1) {
horizontalMax = max(horizontalMax,arr[i][j]*arr[i][j+1];
verticalMax = max(verticalMax,arr[i][j]*arr[i+1][j];
//diagnonals....
}
}
//fix the edge cases as well



You mispelled memorization
+ Show Spoiler +
jkjk
How do you think of this? Does your brain just go like "huh, big numbers... how do I reduce?"
Albeit I don't think a simple multiplication is enough to warrant such steps.

Yeah it's pretty simple. Just realize that you'll do a lot of extra calculations. Then think of ways of how to avoid.
enigmaticcam
Profile Blog Joined October 2010
United States280 Posts
February 26 2013 00:35 GMT
#5109
A simpler solution would be to, going from left to right for each row, take the current row/col number and multiply it by the number to the right, the number diagonally down and to the right, and the number directly below it. Save the highest product as you find it (and highest index combination, if you need it). You'll never do any more calculations then you need, and the code would be very simple to write.
Try
Profile Blog Joined August 2007
United States1293 Posts
February 26 2013 09:13 GMT
#5110
Hey guys,

I'm a 3rd year CS student and I'm looking to get involved with the open source community; however I have no idea how to get started. Anyone want to point me in the right direction? I know that this is a super broad topic, sorry that I can't be more specific. If it helps at all, I would be really interested in getting involved with GNU type stuff.
adwodon
Profile Blog Joined September 2010
United Kingdom592 Posts
February 26 2013 09:23 GMT
#5111
On February 26 2013 18:13 Try wrote:
Hey guys,

I'm a 3rd year CS student and I'm looking to get involved with the open source community; however I have no idea how to get started. Anyone want to point me in the right direction? I know that this is a super broad topic, sorry that I can't be more specific. If it helps at all, I would be really interested in getting involved with GNU type stuff.


Find an open source project you like and contact people involved, go on message boards / mailing lists and ask questions and ask how you can help.

Also a quick google search can always help:

http://www.makeuseof.com/tag/the-10-best-open-source-projects-you-should-be-volunteering-to-help-with/
Aphasie
Profile Blog Joined February 2010
Norway474 Posts
Last Edited: 2013-02-26 09:58:20
February 26 2013 09:57 GMT
#5112
Hi everyone,

I hope this is the right thread. Im interested in learning to program, however I have to admit I have very basic knowledge of computers in general. Ive been (forced to) using Macs (i.e. every small part, script, etc is "hidden" for regular users) untill a couple of years ago and im not at all familiar with the underlying processes in a computer. Could anybody link me a comprehensive page on what makes computers work on certain levels, what processes are ongoing, etc. I feel I need to start at the very bottom, building my knowledge of computers from scratch. Then work towards more advanced subjects like programming, etc.

Much appreciated,
Peace
Terranist
Profile Blog Joined March 2009
United States2496 Posts
February 26 2013 10:36 GMT
#5113
On February 26 2013 18:57 Aphasie wrote:
Hi everyone,

I hope this is the right thread. Im interested in learning to program, however I have to admit I have very basic knowledge of computers in general. Ive been (forced to) using Macs (i.e. every small part, script, etc is "hidden" for regular users) untill a couple of years ago and im not at all familiar with the underlying processes in a computer. Could anybody link me a comprehensive page on what makes computers work on certain levels, what processes are ongoing, etc. I feel I need to start at the very bottom, building my knowledge of computers from scratch. Then work towards more advanced subjects like programming, etc.

Much appreciated,
Peace


just read a good C book. you will learn how machines fundamentally work and get your feet wet programming at the same time. programming is actually more about logic than it is being familiar with an operating for example.
The Show of a Lifetime
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
February 26 2013 20:30 GMT
#5114
On February 26 2013 18:57 Aphasie wrote:
Hi everyone,

I hope this is the right thread. Im interested in learning to program, however I have to admit I have very basic knowledge of computers in general. Ive been (forced to) using Macs (i.e. every small part, script, etc is "hidden" for regular users) untill a couple of years ago and im not at all familiar with the underlying processes in a computer. Could anybody link me a comprehensive page on what makes computers work on certain levels, what processes are ongoing, etc. I feel I need to start at the very bottom, building my knowledge of computers from scratch. Then work towards more advanced subjects like programming, etc.

Much appreciated,
Peace

Code the Hidden Language of Computers by Charlez Petzhold. Modern C Programming 2nd Edition by K. N. King. http://www.randygaul.net/2011/11/16/i-want-to-learn-programming-but-i-know-nothing/
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-02-27 00:04:16
February 26 2013 23:50 GMT
#5115
Edit: C language

I'm disgusted at how I've implemented my code to get the greatest product of 2 adjacent numbers.

First off, an example of what I'm talking about (feel free to skip if you know what the goal is):

You have an n x n grid (2D array). You're are asked to find the largest multiplication (product) of 2 adjacent (close to each other) numbers in any direction (left, right, down, up or diagonally).

Example:
21 22 23
20 00 11
17 06 15

22 x 23 is the largest, so output would be 506.

Now my implementation:

Code:
+ Show Spoiler +


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
/* check left */
if (!(j-1 < 0)) {
if (grid[i][j-1] * grid[i][j] > product) {
product = grid[i][j-1] * grid[i][j];
}
}
/* check right */
if (!(j+1 > n-1)) {
if (grid[i][j+1] * grid[i][j] > product) {
product = grid[i][j+1] * grid[i][j];
}
}
/* check up */
if (!(i-1 < 0)) {
if (grid[i-1][j] * grid[i][j] > product) {
product = grid[i-1][j] * grid[i][j];
}
}
/* check down */
if (!(i+1 > n-1)) {
if (grid[i+1][j] * grid[i][j] > product) {
product = grid[i+1][j] * grid[i][j];
}
}
/* check diagonally */
/* top left */
if (!(i-1 < 0 || j-1 < 0)) {
if (grid[i-1][j-1] * grid[i][j] > product) {
product = grid[i-1][j-1] * grid[i][j];
}
}
/* top right */
if (!(i-1 < 0 || j+1 > n-1)) {
if (grid[i-1][j+1] * grid[i][j] > product) {
product = grid[i-1][j+1] * grid[i][j];
}
}
/* bottom left */
if (!(i+1 > n-1 || j-1 < 0)) {
if (grid[i+1][j-1] * grid[i][j] > product) {
product = grid[i+1][j-1] * grid[i][j];
}
}
/* bottom right */
if (!(i+1 > n-1 || j+1 > n-1)) {
if (grid[i+1][j+1] * grid[i][j] > product) {
product = grid[i+1][j+1] * grid[i][j];
}
}
}
}



I'm honestly not pleased with how I wrote this. It doesn't seem clean to me. Any help/optimisation? What about 'x' adjacent numbers instead of only 2?

Thanks.

Edit: Possible step to make it more clean:

I should perhaps have at the start of the 2nd loop something like this:
leftColumn = j-1;
rightColumn = j+1;
rowAbove = i-1;

etc

Any feedback will be greatly appreciated.
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
February 26 2013 23:57 GMT
#5116
Looks fine to me. You can clean things up a little by using some helper functions. You can also use std::max.

product = std::max( grid[i][j] * grid[i+1][j+1], product )
Phunkapotamus
Profile Joined April 2010
United States496 Posts
Last Edited: 2013-02-27 00:01:19
February 27 2013 00:00 GMT
#5117
On February 27 2013 08:50 darkness wrote:
I'm disgusted at how I've implemented my code to get the greatest product of 2 adjacent numbers.

First off, an example of what I'm talking about (feel free to skip if you know what the goal is):

You have an n x n grid (2D array). You're are asked to find the largest multiplication (product) of 2 adjacent (close to each other) numbers in any direction (left, right, down, up or diagonally).

Example:
21 22 23
20 00 11
17 06 15

22 x 23 is the largest, so output would be 506.

Now my implementation:

Code:
+ Show Spoiler +


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
/* check left */
if (!(j-1 < 0)) {
if (grid[i][j-1] * grid[i][j] > product) {
product = grid[i][j-1] * grid[i][j];
}
}
/* check right */
if (!(j+1 > n-1)) {
if (grid[i][j+1] * grid[i][j] > product) {
product = grid[i][j+1] * grid[i][j];
}
}
/* check up */
if (!(i-1 < 0)) {
if (grid[i-1][j] * grid[i][j] > product) {
product = grid[i-1][j] * grid[i][j];
}
}
/* check down */
if (!(i+1 > n-1)) {
if (grid[i+1][j] * grid[i][j] > product) {
product = grid[i+1][j] * grid[i][j];
}
}
/* check diagonally */
/* top left */
if (!(i-1 < 0 || j-1 < 0)) {
if (grid[i-1][j-1] * grid[i][j] > product) {
product = grid[i-1][j-1] * grid[i][j];
}
}
/* top right */
if (!(i-1 < 0 || j+1 > n-1)) {
if (grid[i-1][j+1] * grid[i][j] > product) {
product = grid[i-1][j+1] * grid[i][j];
}
}
/* bottom left */
if (!(i+1 > n-1 || j-1 < 0)) {
if (grid[i+1][j-1] * grid[i][j] > product) {
product = grid[i+1][j-1] * grid[i][j];
}
}
/* bottom right */
if (!(i+1 > n-1 || j+1 > n-1)) {
if (grid[i+1][j+1] * grid[i][j] > product) {
product = grid[i+1][j+1] * grid[i][j];
}
}
}
}



I'm honestly not pleased with how I wrote this. It doesn't seem clean to me. Any help/optimisation? What about 'x' adjacent numbers instead of only 2?

Thanks.


For this problem you're going to have to go through the whole grid.

Optimization 1) Don't calculate the reverse of a pair you've already calculated.
Optimization 2) Only need to go through the grid once, not twice (n ^ 2) as you're doing.


int maxProduct = 0;
int maxProductX1 = 0;
int maxProductY1 = 0;
int maxProductX2 = 0;
int maxProductY2 = 0;
int curProduct = 0;
int curX = 0;
int curY = 0;

void TestProduct(int x1, int y1, int x2, int y2)
{
int product = grid[x1][y1] * grid[x2][y2];
if(product > maxProduct) {
maxProduct = product;
maxProductX1 = x1;
maxProductX2 = x2;
maxProductY1 = y1;
maxProductY2 = y2;
}
}

for each element in grid {
// only ever need to test right and "down"
// note: not checking bounds as this is pseudocode
TestProduct(curX, curY, curX + 1, curY);
TestProduct(curX, curY, curX, curY + 1);
}
"Do a barrel roll"
Shield
Profile Blog Joined August 2009
Bulgaria4824 Posts
Last Edited: 2013-02-27 00:05:46
February 27 2013 00:02 GMT
#5118
On February 27 2013 08:57 CecilSunkure wrote:
Looks fine to me. You can clean things up a little by using some helper functions. You can also use std::max.

product = std::max( grid[i][j] * grid[i+1][j+1], product )


Sorry, I forgot to mention it is C not C++. But yeah, I guess this would work in my case:


#ifndef max
#define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
#endif

#ifnef min
#define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
#endif


Erm, maybe not. I'm not aware what std::max actually does.
Phunkapotamus
Profile Joined April 2010
United States496 Posts
February 27 2013 00:17 GMT
#5119
std::max is basically your macro, but is uses templates so it is typesafe.
http://www.cplusplus.com/reference/algorithm/max/
"Do a barrel roll"
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
Last Edited: 2013-02-27 00:21:27
February 27 2013 00:19 GMT
#5120
If you care about efficiency I'd imagine you'll want to make your own max function for C, so that your macros don't calculate a bunch of multiplications and array accesses redundantly.

int IntMax( int lhs, int rhs )
{
return lhs > rhs ? lhs : rhs;
}


std::max uses C++ templates:

namespace std
{
template <typename T>
bool max( T lhs, T rhs )
{
return lhs > rhs ? lhs : rhs;
}
}


There are of course other clever tricks using bit manipulations to perform min and max operations, although I wouldn't be surprised if more modern architectures have machine instructions for min and max that commonly replace std::max when std::max is used.
Prev 1 254 255 256 257 258 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 1h 26m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SortOf 308
ProTech123
MindelVK 40
trigger 12
StarCraft: Brood War
Hyuk 14973
Calm 7050
Horang2 1594
Jaedong 1180
Zeus 380
Stork 363
EffOrt 289
Mong 134
ggaemo 133
Mind 132
[ Show more ]
Last 119
Pusan 117
Hyun 65
JulyZerg 64
hero 57
Backho 54
Aegong 48
Killer 37
[sc1f]eonzerg 36
sSak 31
ToSsGirL 28
sorry 23
Hm[arnc] 20
IntoTheRainbow 14
soO 14
SilentControl 11
zelot 11
Sacsri 10
Icarus 6
eros_byul 1
Terrorterran 1
Dota 2
XaKoH 687
canceldota324
LuMiX0
League of Legends
JimRising 177
Counter-Strike
fl0m3391
oskar45
Heroes of the Storm
Khaldor172
Trikslyr36
Other Games
singsing1418
B2W.Neo996
DeMusliM332
byalli284
Lowko220
Hui .172
Fuzer 170
Sick144
RotterdaM135
KnowMe26
Organizations
Dota 2
PGL Dota 2 - Main Stream225
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 11 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Jankos2883
Upcoming Events
uThermal 2v2 Circuit
1h 26m
BSL
6h 26m
RSL Revival
20h 26m
herO vs MaxPax
Rogue vs TriGGeR
BSL
1d 6h
Replay Cast
1d 10h
Replay Cast
1d 19h
Afreeca Starleague
1d 20h
Sharp vs Scan
Rain vs Mong
Wardi Open
1d 22h
Monday Night Weeklies
2 days
Sparkling Tuna Cup
2 days
[ Show More ]
Afreeca Starleague
2 days
Soulkey vs Ample
JyJ vs sSak
Replay Cast
3 days
Afreeca Starleague
3 days
hero vs YSC
Larva vs Shine
Kung Fu Cup
3 days
Replay Cast
4 days
KCM Race Survival
4 days
The PondCast
4 days
WardiTV Team League
4 days
Replay Cast
5 days
WardiTV Team League
5 days
RSL Revival
6 days
Cure vs Zoun
WardiTV Team League
6 days
Liquipedia Results

Completed

Proleague 2026-03-20
WardiTV Winter 2026
Underdog Cup #3

Ongoing

KCM Race Survival 2026 Season 1
Jeongseon Sooper Cup
BSL Season 22
CSL Elite League 2026
RSL Revival: Season 4
Nations Cup 2026
NationLESS Cup
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual

Upcoming

ASL Season 21
Acropolis #4 - TS6
2026 Changsha Offline CUP
CSL 2026 SPRING (S20)
CSL Season 20: Qualifier 1
Acropolis #4
IPSL Spring 2026
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.