User Tools

Site Tools


c:basic_arrays

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
c:basic_arrays [2021/04/28 08:38] peterc:basic_arrays [2021/04/28 08:49] (current) peter
Line 7: Line 7:
 ---- ----
  
-===== Array basics ===== +===== A single variable is used to store the age of a person =====
- +
-A single variable is used to store the age of a person.+
  
 ==== C Code Listing 1 ==== ==== C Code Listing 1 ====
Line 41: Line 39:
 </code> </code>
      
-Not much to it.  The variable age is created at line (5) as a short.  A value is assigned to age.  Finally, age is printed to the screen.+<WRAP info> 
 +**NOTE:**  The variable **age** is created at line (5) as a short. 
 + 
 +  A value is assigned to age. 
 +  Finally, age is printed to the screen. 
 + 
 +</WRAP>
  
 Image: A simple variable in memory Image: A simple variable in memory
Line 47: Line 51:
 {{:c:c_address_age_as_var.jpg?400|}} {{:c:c_address_age_as_var.jpg?400|}}
  
 +----
 +
 +===== Keep track of 4 ages instead of just one =====
  
-Now let's keep track of 4 ages instead of just one.  We could create 4 separate variables, but 4 separate variables have limited appeal.  (If using 4 separate variables is appealing to you, then consider keeping track of 93843 ages instead of just 4).  +4 separate variables could be used, but this does not scale easily if the number to track moves into the thousands.
-Rather than using 4 separate variables, we'll use an array.+
  
-Here's how to create an array and one way to initialize an array:+Instead of using separate variables to store each age, it is better to use an array.
  
-C Code Listing 2+==== C Code Listing 2 ====
  
 <code c> <code c>
Line 69: Line 75:
 </code> </code>
  
-C++ Code Listing 2+---- 
 + 
 +==== C++ Code Listing 2 ====
  
 <code cpp> <code cpp>
Line 85: Line 93:
 </code> </code>
  
-On line (5), an array of 4 short'is created.  Values are assigned to each variable in the array on line (6) through line (9).+<WRAP info> 
 +**NOTE:**  This creates and initializes an array. 
 + 
 +  * On line (5), an array of 4 shorts is created. 
 +  Values are assigned to each variable in the array on line (6) through line (9). 
 + 
 +</WRAP>
  
 Image: Array with 4 elements Image: Array with 4 elements
Line 91: Line 105:
 {{:c:c_address_age_as_arr.jpg?400|}} {{:c:c_address_age_as_arr.jpg?400|}}
  
-Accessing any single short variable, or element, in the array is straightforward.  Simply provide a number in square braces next to the name of the array.  The number identifies which of the 4 elements in the array you want to access.+----
  
-The program above shows that the first element of an array is accessed with the number 0 rather than 1.  Later in the tutorialWe'll discuss why 0 is used to indicate the first element in the array.+<WRAP info> 
 +**NOTE:**  Accessing any single short variableor elementin the array is straightforward.
  
 +Simply provide a number in square braces next to the name of the array.  The number identifies which of the 4 elements in the array you want to access.
 +
 +The program above shows that the first element of an array is accessed with the number 0 rather than 1.
 +
 +</WRAP>
 +
 +----
  
 ===== Printing arrays ===== ===== Printing arrays =====
  
-Our program is a bit unrevealing in that we never print the array to screen.  Here is the same program with an attempt to print the array to the screen:+Print the array to the screen:
  
-C Code Listing 3+==== C Code Listing 3 ====
  
 <code c> <code c>
Line 118: Line 140:
 </code> </code>
  
-C++ Code Listing 3+---- 
 + 
 +==== C++ Code Listing 3 ====
  
 <code c++> <code c++>
Line 136: Line 160:
 </code> </code>
  
-Line (11) is meant to print the 4 ages to the screen.  But instead of printing out the four short variables, what appears to be nonsense prints out instead.+<WRAP info> 
 +**NOTE:**  Line (11) is meant to print the 4 ages to the screen.
  
-What the "nonsense" output actually is and why the 4 array values were not printed will be addressed later in the tutorial.  For nowthe important point to come away with is that simply providing the name of the array in an output statement will not print out the elements of the array.+But instead of printing out the four short variableswhat appears to be nonsense prints out instead.
  
 +What the "nonsense" output actually is and why the 4 array values were not printed will be addressed later.
 +
 +For now, the important point to come away with is that simply providing the name of the array in an output statement will not print out the elements of the array.
 +
 +</WRAP>
 +
 +----
  
 How about printing out each of the values separately?  Try this: How about printing out each of the values separately?  Try this:
  
-C Code Listing 4+==== C Code Listing 4 ====
  
 <code c> <code c>
Line 163: Line 195:
 </code> </code>
  
-C++ Code Listing 4+---- 
 + 
 +==== C++ Code Listing 4 ====
  
 <code c++> <code c++>
Line 183: Line 217:
 </code> </code>
  
-Lines (10) through line (13) produce the output we are expecting.+<WRAP info> 
 +**NOTE:**  Lines (10) through line (13) produce the output we are expecting.
  
-There is no single statement in the language that says "print an entire array to the screen".  Each element in the array must be printed to the screen individually.+There is no single statement in the language that says "print an entire array to the screen".
  
 +Each element in the array must be printed to the screen individually.
 +
 +</WRAP>
 +
 +----
  
 ===== Copying arrays ===== ===== Copying arrays =====
Line 192: Line 232:
 Suppose that after filling our 4 element array with values, we need to copy that array to another array of 4 short's?  Try this: Suppose that after filling our 4 element array with values, we need to copy that array to another array of 4 short's?  Try this:
  
-C Code Listing 5+==== C Code Listing 5 ====
  
 <code c> <code c>
Line 216: Line 256:
 </code> </code>
  
-C++ Code Listing 5+---- 
 + 
 +==== C++ Code Listing 5 ====
  
 <code c++> <code c++>
Line 240: Line 282:
 </code> </code>
  
-Line (12) tries to copy the age array into the same_age array.  What happened when you tried to compile the program above?+<WRAP info> 
 +**NOTE:**  Line (12) tries to copy the age array into the same_age array.  What happened when you tried to compile the program above?
  
 The point here is that simply assigning one array to another will not copy the elements of the array.  The hard question to answer is why the code doesn't compile.  Later in the tutorial this example will be re-examined to explain why line (12) doesn't work.  This code should not compile on either C or C++ compilers.  However, some older C++ compilers may ignore the ISO C++ standard and allow line 12 to compile.  If it does compile on your C++ compiler, make a mental note that it is incorrect behavior. The point here is that simply assigning one array to another will not copy the elements of the array.  The hard question to answer is why the code doesn't compile.  Later in the tutorial this example will be re-examined to explain why line (12) doesn't work.  This code should not compile on either C or C++ compilers.  However, some older C++ compilers may ignore the ISO C++ standard and allow line 12 to compile.  If it does compile on your C++ compiler, make a mental note that it is incorrect behavior.
 +
 +</WRAP>
 +
 +----
  
 Let's try copying arrays using a technique similar to the technique used to print arrays (that is, one element at a time): Let's try copying arrays using a technique similar to the technique used to print arrays (that is, one element at a time):
  
-C Code Listing 6+==== C Code Listing 6 ====
  
 <code c> <code c>
Line 274: Line 321:
 </code> </code>
  
-C++ Code Listing 6+---- 
 + 
 +==== C++ Code Listing 6 ====
  
 <code c++> <code c++>
Line 302: Line 351:
 </code> </code>
  
-This technique for copying arrays works fine.  Two arrays are created: age and same_age.  Each element of the age array is assigned a value.  Then, in order to copy of the four elements in age into the same_age array, we must do it element by element.+<WRAP info> 
 +**NOTE**  This technique for copying arrays works fine. 
 + 
 +  Two arrays are created: age and same_age. 
 +  Each element of the age array is assigned a value. 
 +  Then, in order to copy of the four elements in age into the same_age array, we must do it element by element. 
 + 
 +</WRAP>
  
 Image: Copy element 1 Image: Copy element 1
 +
 Copy first element Copy first element
  
Line 311: Line 368:
  
 Image: Copy element 2 Image: Copy element 2
 +
 Copy second element Copy second element
  
Line 321: Line 379:
 One significant advantage of an array over separate variables is the name.  In our examples, using four separate variables requires 4 unique names. The 4 short variables in our array have the same name, age.  The 4 short's in the array are identical except for an index number used to access them.  This distinction allows us to shorten our code in a way that would be impossible with 4 variables, each with unique names: One significant advantage of an array over separate variables is the name.  In our examples, using four separate variables requires 4 unique names. The 4 short variables in our array have the same name, age.  The 4 short's in the array are identical except for an index number used to access them.  This distinction allows us to shorten our code in a way that would be impossible with 4 variables, each with unique names:
  
-C Code Listing 7+==== C Code Listing 7 ====
  
 <code c> <code c>
Line 345: Line 403:
 </code> </code>
  
-C++ Code Listing 7+---- 
 + 
 +==== C++ Code Listing 7 ====
  
 <code c++> <code c++>
Line 375: Line 435:
 If we want to perform any action on an array, we must repeatedly perform that action on each element in the array. If we want to perform any action on an array, we must repeatedly perform that action on each element in the array.
  
 +----
  
 ===== Array Internals ===== ===== Array Internals =====
c/basic_arrays.1619599096.txt.gz · Last modified: 2021/04/28 08:38 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki