G+_Dean T Posted March 12, 2014 Share Posted March 12, 2014 ARRAYS!!?? Can someone who programs regularly tell me if they're actually useful in real programming? Im currently reading up on them and theyre boring me as other than using their sort ability I cant see much use in them since I need to declare their values anyway I cant grasp why I wouldnt instead of using an array like this int[] arrays { 0, 1 ,2 } int total = arrays[0] + arrays[1] + arrays[2] why i cant just keep using what im using int int0 = 0 int int1 = 1 int int2 = 2 int total = int0 + int1 +int2 Link to comment Share on other sites More sharing options...
G+_John Mink Posted March 12, 2014 Share Posted March 12, 2014 Dean T arrays are amazingly useful! However, you don't really start getting much of a benefit until you are using them in a loop...because otherwise you are exactly correct, you need to specify each & every value either way....every time you want to change anything! What really showed it off to me was a simple program for class, where we had to roll two six sided dice, then record how many times we saw each value (meaning the total, 2-12) appear. So you could say: if dice_total=2 then dice_log_2=dice_log_2+1 else if dice_total=3 then dice_log_3=dice_log_3+1 else if dice_total=4 then and so on... (down to dice_total=12) or you could say: dice_log(dice_total)=dice_log(dice_total)+1 maybe 11 times isn't so bad, but what happens when you change it to 5 dice...or 42 dice...or let the user pick, so you don't know how many dice will be needed when you're writing the code? Further we can look at your example, summing an array: I can rewrite that to say int array_index=3; int[array_index] int_arrays; int counter=0; int array_total for counter = 0 to (array_index-1) int_array(counter)= counter; array_total=array_total+int_array(counter); next counter Now, yours is shorter for 3 values...but what about 10? You need to add the lines yourself, I simply need to change one value (my index) Hope this helps! Link to comment Share on other sites More sharing options...
G+_Dean T Posted March 12, 2014 Author Share Posted March 12, 2014 Thanks for the response guys. Im getting the impression then that its due to my programs code being quite small / limited at the moment (like you say with the dice) but as I start to advance to larger code that then I will probably see more of a need for it. I didnt think of the foreach loop either as Ive had no use for it yet really. Link to comment Share on other sites More sharing options...
G+_Charles Kelly Posted March 12, 2014 Share Posted March 12, 2014 I would also add that arrays may be used as parameters and return values when calling functions. This allows a function to work with large data sets. Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted March 13, 2014 Share Posted March 13, 2014 Here's an example of an interesting use for an array. A few times in the past I've needed to switch a bunch of text boxes on the screen back and forth between read-only and editable modes with a different color for each mode. I declared an array of type textbox and assigned each box to an element of the array at program start. Then I had a function that has the mode as a parameter that just uses a for loop to go through the array and set the mode and color of all of the text boxes. I think I had another function that cleared them all. Link to comment Share on other sites More sharing options...
G+_S. LeBeau Kpadenou Posted March 14, 2014 Share Posted March 14, 2014 Arrays are invaluable! There are several kinds of problems that I don't think could be solved without them (or without reinventing them within the confines of your chosen language.) They are, in fact, clunky, but they are generally the basis of other, more specific, more convenient 'collections'. This link: http://euler.vcsu.edu:7000/523/ shows a good example. In summary, suppose you were coding a digital coin purse. You could simply add the value of every coin added to a running total, and subtract the value for each coin removed. But you'd want to be able to tell what actual coins are in there, and how many. By using an array, we could keep track of the number and type of coins in the purse. We would only create variables for (and use memory for) the coins actually in the purse. We wouldn't need to keep a running total of value or count (we could figure it out from the array when we need it.) We could move all the coins to another purse in one or two lines, vs one or two lines for every coin (fewer lines == fewer typos.) An analogy: avoiding arrays in a program because you don't yet get them is like avoiding plurals in a written article because you don't understand the irregular forms (why "children" instead of "childs"?). It is likely possible, but will certainly involve more work than learning or looking up the way to use the tool. Link to comment Share on other sites More sharing options...
G+_Nevrin O Posted March 14, 2014 Share Posted March 14, 2014 Another example where Arrays are useful are situations where you need a varying number of variables or really large number of variables (one array of size 1000 instead of hard coding 1000 variables). When i was learning java I had a class that was a deck of cards and its much easier to shuffle an array of size 52 then 52 different variables. Link to comment Share on other sites More sharing options...
Recommended Posts