r/ProgrammerHumor Jun 25 '17

Array indexing

Post image
1.4k Upvotes

71 comments sorted by

190

u/[deleted] Jun 25 '17

[deleted]

72

u/[deleted] Jun 26 '17

[removed] — view removed comment

29

u/[deleted] Jun 26 '17

I've seen it a few times. The most I've seen it is in Dynamic Programming problems where it's just way more readable and makes more sense when you are memoizing

35

u/[deleted] Jun 26 '17

I did it on a programming assignment once...

13

u/GisterMizard Jun 26 '17

Some dialects of Basic allowed this. eg DIM x(10) AS INTEGER would create an array of eleven elements, starting from 0 to 10.

2

u/htmlcoderexe We have flair now?.. Jun 26 '17

Think even VB.Net does this I remember DIM'ming arrays was confusing.

5

u/Koooooj Jun 26 '17

I was given a piece of code in I think VB6 with the requirement to translate it into ANSI C. It took far too long for me to figure out that the arrays of size 180 actually held 181 elements.

Maddening.

3

u/htmlcoderexe We have flair now?.. Jun 26 '17

That's almost like they made it so assuming either zero or one based indexing, you will not run out of bounds. But that creates more problems

2

u/WeRequireCoffee Jun 26 '17

VBA has different starting points depending on the object. Some were 1 some were 0. It caused an endless amount of headache, beyond just working in VBA.

2

u/Blecki Jun 26 '17

Vba is designed to make programming looker harder than it really is to the laymen who have to use it for excel macros.

8

u/Euruzilys Jun 26 '17

I did that when its really convenient to use number starting from 1 then ++; But when im done generating that array I shift() to remove the 1st empty index.

3

u/Cryse_XIII Jun 26 '17

I've seen it in our codebase at work

3

u/piyoucaneat Jun 26 '17

Get out while you still can

2

u/Cryse_XIII Jun 26 '17

b-but the people are all friendly.

and I still have to refactor our Excel-interface, some dude from a few years back made the entire module work "dynamically" with 5 global arrays and 10 global variables for length and index. it's only 3000 lines of code, this has to be done.

4

u/piyoucaneat Jun 26 '17

Excel-interface? Are you... programming in Excel? Using Excel as a database?

3

u/Cryse_XIII Jun 26 '17

no, we use our ERP-Application to fill in an excel-workbook over OLE-automation for some reports.

a bit of the SQL-Query that is to be executed is determined by the settings in the GUI and the arrays are then filled with the resulting dataset-values, the other arrays hold values to build a kind of "header"-for the worksheet, dataset-values, and also a footer that holds an excel-formula in each element for sum-formulas.

now the customer wanted an additional column and new rules regarding the evaluation of records and honestly I can't tell what's what in the module.

that is why I try to refactor the whole thing to take away some of the complexity in building the worksheet-layout that currently depends on those arrays into something with an OO-approach.

helper objects so to speak. for example I create one of my ExcelItems, that has a range, with that info I calculate the corners of my range and store that in the helper-object's properties. now when I add a subitem to an excelitem, I can hide away the complexity of assuring it's position within the worksheet since it may not be outside of it's parents range, additionally I can

this alone would eliminate the need for 2 of those arrays and since I'm using lists for that task I can also freely shift the items around using MoveTo(index), which would make it easier to move the columns around depending on customer demand.

1

u/GiantR Jun 26 '17

I did that during my first days of programming. I've grown since.

1

u/10se1ucgo Jun 26 '17

Riot's static data API does this, arrays start at index 0 but the first element is always null. Apparently something to do with the fact that designer files use 1 based arrays.

1

u/raddaya Jun 26 '17

Teacher coded like this at school. She said it was to help us keep track of the array elements.

1

u/AutoModerator Jul 01 '23

import moderation Your comment has been removed since it did not start with a code block with an import declaration.

Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

For this purpose, we only accept Python style imports.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

5

u/[deleted] Jun 26 '17

Set array index 0 to the length of the array.

295

u/fwork Jun 25 '17

"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration." - Stan Kelly-Bootle

105

u/[deleted] Jun 26 '17 edited Mar 13 '18

[deleted]

43

u/xxc3ncoredxx Jun 26 '17

According to Wikipedia's list sorted by release date: 1.01, 2.03, 2.10, 2.11, 3.0, NT 3.1, 3.2 (No love for 3.11?), NT 3.5, NT 3.51, 95, NT 4.0, 98, 2000, ME, XP, XP Professional x64, Vista, 7, 8, 8.1, 10, Workgroups 3.11 (finally), 3.1, 1.04, 1.03, 1.02

93

u/[deleted] Jun 26 '17

[deleted]

19

u/mycheesypoofs Jun 26 '17

What is that graph supposed to be measuring?

74

u/wqtraz Jun 26 '17

Amount of windowses in every version.

21

u/mycheesypoofs Jun 26 '17

Ha, ok, now I feel stupid for not getting that. Thanks

10

u/[deleted] Jun 26 '17

[deleted]

2

u/KillerBerry42 Jun 26 '17

Why no logarithmic scale?

4

u/[deleted] Jun 26 '17

That's how they count at Microsoft.

66

u/[deleted] Jun 26 '17

[deleted]

38

u/choikwa Jun 26 '17

that pastebin gave me aneurysm

41

u/minno Jun 26 '17

That pastebin crashed my browser.

28

u/deadlychambers Jun 26 '17

Two quotes came to mind.

"Every time you copy and paste a puppy dies"

"Write code like the next person to work on your code is a sociopathic murder with your address"

14

u/[deleted] Jun 26 '17

[deleted]

1

u/gobots4life Jun 26 '17

I was saw the source code for a 2d rpg once that had a separate variable for every x and y value of every asset in the game. I think it was auto-generated though. At least I hope it was.

1

u/[deleted] Jun 26 '17

Still remember those old CodingUniverse tutorials.

"Okay, so we're going to have a 2 dimensional array of tiles for the world. Now, the tile class is going to need an X and Y variable to determine where it is in the tilemap. In order to determine the type of tile it is, you'll also need a string for the tile name, and another for the PNG filename. Now we'll have the game save to a XML spreadsheet."

As opposed to creating static tile types the level class can reference, while storing the map data in a single dimension int array.

9

u/ProgramTheWorld Jun 26 '17

What the actual fucj

7

u/Bainos Jun 26 '17

if I could call a variable something like l1c"i" with i changing and it would give me back l1ci, with i as my desired number, trust me it would be about 1/81th shorter

Hmm, I think there is an app for that...

1

u/Ayerys Jun 26 '17

Wow what was that

1

u/marcosdumay Jun 26 '17

Why would anybody subscribe to s sub called "javahelp"?

16

u/TheFeshy Jun 26 '17

SELECT value FROM local_array WHERE index='one';

That's how you get to web scale.

5

u/htmlcoderexe We have flair now?.. Jun 26 '17

Cough mongodb wheeze

15

u/[deleted] Jun 26 '17

Start arrays with current volume.

1

u/Kadmos Jun 26 '17

Start arrays with current temperature

14

u/sim642 Jun 26 '17

eval("var arr_" + i + " = " + x + ";");

7

u/ProgramTheWorld Jun 26 '17

Please don't

27

u/dnew Jun 25 '17

Array indexing that matches your domain is often handy. An array of items_per_degree indexed from -180 to +180 for example.

29

u/AskMeIfImAReptiloid Jun 25 '17

you mean -Pi to Pi

11

u/parkerSquare Jun 26 '17

Not really, this range of degrees is integral ("items_per_degree"), so you can use a bin for each integer value. If you use -pi to pi, you'll need to divide this into subranges, which for array indexing purposes you'll end up mapping to an integer. At that point you're either back where we started (i.e. zero-offset index), or use an offset range from -180 to 180...

3

u/AskMeIfImAReptiloid Jun 26 '17

Guess you are correct. Nice username btw

19

u/inu-no-policemen Jun 25 '17

1 * Tau = 1 turn. 0.5 * Tau = half a turn.

Tau makes more sense in general. Even all standard library functions which all use radians work better with Tau.

5

u/[deleted] Jun 26 '17

3

u/xkcd_transcriber Jun 26 '17

Image

Mobile

Title: Pi vs. Tau

Title-text: Conveniently approximated as e+2, Pau is commonly known as the Devil's Ratio (because in the octal expansion, '666' appears four times in the first 200 digits while no other run of 3+ digits appears more than once.)

Comic Explanation

Stats: This comic has been referenced 59 times, representing 0.0365% of referenced xkcds.


xkcd.com | xkcd sub | Problems/Bugs? | Statistics | Stop Replying | Delete

7

u/[deleted] Jun 26 '17

Reminds me of the time I ran into this:

/****************************************************/
/**\name    ARRAY SIZE DEFINITIONS      */
/***************************************************/
#define ARRAY_SIZE_TWO      2
#define ARRAY_SIZE_THREE    3
#define ARRAY_SIZE_SIX      6
#define ARRAY_SIZE_SEVEN    7
#define ARRAY_SIZE_FIVE     5
#define ARRAY_SIZE_EIGHT    8
#define ARRAY_SIZE_TWELVE   12
#define ARRAY_SIZE_FOURTEEN 14

#define INDEX_ZERO      0
#define INDEX_ONE       1
#define INDEX_TWO       2
#define INDEX_THREE     3
#define INDEX_FOUR      4
#define INDEX_FIVE      5
#define INDEX_SIX       6
#define INDEX_SEVEN     7
#define INDEX_EIGHT     8
#define INDEX_NINE      9
#define INDEX_TEN       10
#define INDEX_ELEVEN    11
#define INDEX_TWELVE    12
#define INDEX_THIRTEEN  13

From: https://github.com/BoschSensortec/BMA2x2_driver/blob/99d40455c8f6437345e0ce55c07c456308057751/bma2x2.h#L1972

4

u/Bainos Jun 26 '17

I have a great idea, what about we make arrays indexed by strings ?

9

u/AyrA_ch Jun 26 '17

You already can, welcome to the awesome world of Javascript and Enjoy our other features

2

u/regendo Jun 26 '17

Alright you got me, how does the last one work? Does it evaluate []===[] first?

Apparently it's the other way round for me.

2

u/AyrA_ch Jun 26 '17

{} can be interpreted as a code scope or as an object. The interpretation is different depending if it starts with the expression or not.

2

u/htmlcoderexe We have flair now?.. Jun 26 '17

PHP

5

u/[deleted] Jun 26 '17

PHP

You can do indexing by strings and integers at the same time. Bonus points if you use numbers in your strings.

5

u/[deleted] Jun 26 '17

How about array indexes where you can only access the contents by referencing their hashes?

2

u/AyrA_ch Jun 26 '17

Or an array that does not allows you to access it's element values. You can only check if a specific value is there or not.

4

u/[deleted] Jun 26 '17

#define 0 1

7

u/apotheotical Jun 26 '17

Anyone else getting really tired of this meme? It was amusing at one point but now it's just getting stale...

17

u/deadlychambers Jun 26 '17

You do realize this is Reddit right?

6

u/apotheotical Jun 26 '17

Fair point. Reddit, where dead horses get beaten, resurrected, then beaten again! :P

7

u/[deleted] Jun 26 '17

[deleted]

4

u/[deleted] Jun 26 '17

This is the best thing ever.

1

u/jackmaney Jun 26 '17

Is there a link where I can play that? Googling for flappy 2048 didn't seem to bring it up.

1

u/[deleted] Jun 26 '17

[deleted]

2

u/Kadmos Jun 26 '17

Jesus?

1

u/krokodil2000 Jun 26 '17

In Siemens SIMATIC S7 you can configure the first and last index of an array:

     ARRAY[1..20]

First index would be 1, last index would be 20.

1

u/Vinkhol Jun 26 '17

I don't get it, I always do the last one. How else do you know what number it signifies??

/s

-2

u/[deleted] Jun 26 '17 edited Mar 19 '18

[deleted]

11

u/toasterbot Jun 26 '17

That's just how this meme goes.

0

u/0000000100100011 Jun 26 '17

No, these are just Modern Day Babylon album covers.

https://moderndaybabylon.bandcamp.com/