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
Next revision
Previous revision
c:basic_arrays [2020/07/20 19:33] – old revision restored (2016/08/05 15:57) 62.138.2.243c:basic_arrays [2021/04/28 08:49] (current) peter
Line 1: Line 1:
 ====== C - Basic Arrays ====== ====== C - Basic Arrays ======
  
-**NOTE**:  This tutorial assumes you've comfortable with [[c:pointers|pointers]].  If you aren't please start there+<WRAP info> 
-Array basics+**NOTE**:  This tutorial assumes you are comfortable with [[c:pointers|pointers]]. 
 +</WRAP>
  
-Let's start by looking at a single variable used to store a person's age.+----
  
-C Code Listing 1+===== A single variable is used to store the age of a person ===== 
 + 
 +==== C Code Listing 1 ====
  
 <code c> <code c>
Line 19: Line 22:
   9: }   9: }
 </code> </code>
 +
 +----
      
-C++ Code Listing 1+==== C++ Code Listing 1 ====
  
 <code cpp> <code cpp>
Line 34: 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 40: 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 62: Line 75:
 </code> </code>
  
-C++ Code Listing 2+----
  
-<code c++>+==== C++ Code Listing 2 ==== 
 + 
 +<code cpp>
   1: #include <iostream>   1: #include <iostream>
   2:    2: 
Line 78: 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 84: 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.+---- 
 + 
 +<WRAP info> 
 +**NOTE:**  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.
  
-The program above shows that the first element of an array is accessed with the number 0 rather than 1.  Later in the tutorial, We'll discuss why 0 is used to indicate the first element in the array.+</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 111: Line 140:
 </code> </code>
  
-C++ Code Listing 3+---- 
 + 
 +==== C++ Code Listing 3 ====
  
 <code c++> <code c++>
Line 129: 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. 
 + 
 +But instead of printing out the four short variables, what 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.
  
-What the "nonsense" output actually is and why the 4 array values were not printed will be addressed later in the tutorial.  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 156: Line 195:
 </code> </code>
  
-C++ Code Listing 4+---- 
 + 
 +==== C++ Code Listing 4 ====
  
 <code c++> <code c++>
Line 176: 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 185: 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 209: Line 256:
 </code> </code>
  
-C++ Code Listing 5+---- 
 + 
 +==== C++ Code Listing 5 ====
  
 <code c++> <code c++>
Line 233: 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 267: Line 321:
 </code> </code>
  
-C++ Code Listing 6+---- 
 + 
 +==== C++ Code Listing 6 ====
  
 <code c++> <code c++>
Line 295: 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 304: Line 368:
  
 Image: Copy element 2 Image: Copy element 2
 +
 Copy second element Copy second element
  
Line 314: 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 338: Line 403:
 </code> </code>
  
-C++ Code Listing 7+---- 
 + 
 +==== C++ Code Listing 7 ====
  
 <code c++> <code c++>
Line 368: 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.1595273594.txt.gz · Last modified: 2020/07/20 19:33 by 62.138.2.243

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki