Home   MosaicPresentation format
<http://cnfolio.com/IntroToComputingTopic06Print>
Introduction to Computing – B142L

Arrays and strings

Academic year 2011/12



Objectives:
  1. Concept of an array
  2. Description of an array
  3. Using an array
  4. Strings as arrays of characters






Concept of an array



Programs often use a list of variables which are of the same data type and share a common list name.

For example, below is a problem which needs 3 variables which have the same float data type and share a common list name, month.

Sample monthly revenue input values:
5100.50  5200.04  4992.37

Variables to store the input values:
float month1, month2, month3;


Sample output display:
The average monthly revenue is 5097.64
The total revenue earned is 15292.91

Variables to store the output values:
float average, total;






Concept of an array



Programs often use a list of variables which are of the same data type and share a common list name.

For example, below is a problem which needs 12 variables which have the same float data type and share a common list name, month.

Variables to store the input values:
float month1, month2, month3, month4, month5, month6;
float month7, month8, month9, month10, month11, month12;


Variables to store the output values:
float average, total;






Concept of an array



Which control structure could be used to collect the inputs and calculate the output values?

 






Arrays are data structures



Variables to store the input values:
float month1, month2, month3, month4, month5, month6;
float month7, month8, month9, month10, month11, month12;


An array is used to manage a set of variables that all have the same data type.

An array allows variables to be easily controlled using iteration.

An array to store the input values:
float months[ 12 ] = { 0 };

months[0] months[1] months[2] months[3] months[4] months[5] months[6] months[7] months[8] months[9] months[10] months[11]
0 0 0 0 0 0 0 0 0 0 0 0






Description of an array



months[0] months[1] months[2] months[3] months[4] months[5] months[6] months[7] months[8] months[9] months[10] months[11]
0 0 0 0 0 0 0 0 0 0 0 0

An array is used to manage a set of variables that all have the same data type.

An array allows variables to be easily controlled using iteration.

An array to store the input values:
float months[ 12 ] = { 0 };






Description of an array



months[0] months[1] months[2] months[3] months[4] months[5] months[6] months[7] months[8] months[9] months[10] months[11]
0 0 0 0 0 0 0 0 0 0 0 0

An array is used to manage a set of variables that all have the same data type.

An array allows variables to be easily controlled using iteration.

An array to store the input values:
float months[ 12 ] = { 0 };






Using arrays



The array length can be automatically determined by the number of values in the initialization list.

float months[] = { 5100.50, 5200.04, 4992.37, 3721.18, 3299.68, 4229.02, 3510.54, 6920.74, 4291.57, 3964.92, 3719.98, 4323.12 };



Every element in the array is initialized to the same value as the single initialization value.

float months[ 12 ] = { 0 };



The array is declared, but not initialized.

float months[ 12 ];






Using arrays



months[0] months[1] months[2] months[3] months[4] months[5] months[6] months[7] months[8] months[9] months[10] months[11]
0 0 0 0 0 0 0 0 0 0 0 0

An array is used to manage a set of variables that all have the same data type.

An array allows variables to be easily controlled using iteration.

 






Using arrays



An array is stored as a contiguous sequence in program memory.

Amount of memory used by an array = ( array length ) x ( memory for array data type )







Using arrays



Conceptually, we think of a two dimensional array as a table:





However, arrays are stored sequentially in program memory:







Using arrays



How could we use a 2-dimensional array to store revenue for 2 years?

 






Strings as arrays of characters



A string is an array of character variables that ends with a NULL character ( '\0' ). Read the detailed reference description.

char message[ 33 ] = { '\0' };

message[0] message[1] message[2] ( 3 to 31 ) message[32]
\0 \0 \0 \0 \0






Using strings



A string is an array of character variables that ends with a NULL character ( '\0' ). Read the detailed reference description.

When using string related functions, remember to add the header file for the standard string library functions.

#include <string.h>






Using strings



A string is an array of character variables that ends with a NULL character ( '\0' ). Read the detailed reference description.

Use the strlen() function to find the length of a text string.

 

When using string related functions, remember to add the header file for the standard string library functions.

#include <string.h>






Using strings



A string is an array of character variables that ends with a NULL character ( '\0' ). Read the detailed reference description.

Use the strncmp() function to compare 2 strings for equality.

 






Using strings



A string is an array of character variables that ends with a NULL character ( '\0' ). Read the detailed reference description.

Use the sprintf() function to store formatted output into a text string.

 






Using strings



A string is an array of character variables that ends with a NULL character ( '\0' ). Read the detailed reference description.

Use the strcpy() function to copy one string to another string.

 






Using strings



A string is an array of character variables that ends with a NULL character ( '\0' ). Read the detailed reference description.

Use the strcat() function to append one string to the end of another string.

 






Using strings



A string is an array of character variables that ends with a NULL character ( '\0' ). Read the detailed reference description.

Text in a string can be parsed into smaller units using the strtok() function.

 






Review questions




Using arrays


The C program below is intended to collect the age of 40 people, stores the values in an array and displays the youngest age. At line 10, the expression on the right side of the or operator provides the condition for what?

  1. int main( void )
  2. {
  3.   int age[ 40 ] = { 0 };
  4.   int youngest = -1;
  5.   int counter;
  6.  
  7.   for ( counter = 0; counter < 40; counter++ )
  8.   {
  9.     scanf( "%d", &age[ counter ] );
  10.     if ( ( age[ counter ] < youngest ) || ( youngest < 0 ) )
  11.       youngest = age[ counter ];
  12.   }
  13.  
  14.   printf( "The youngest age is %d", youngest );
  15. }

Save the first value into the array.
Clear all values in the array.
Find the lowest age in the array.
Find the highest age in the array.






Review questions




Using arrays


The C program below is intended to collect the ticket prices at 50 movie theatres, store them in an array and display the average ticket price. At line 7, what should replace the XX placeholders?

  1. int main( void )
  2. {
  3.   double prices[ 50 ];
  4.   double total = 0;
  5.   int index;
  6.  
  7.   for ( index = 0; index < XX; index++ )
  8.   {
  9.     scanf( "%lf", &prices[ index ] );
  10.     total += prices[ index ];
  11.   }
  12.  
  13.   printf( "Average ticket price is %0.2lf", total / 50 );
  14. }








Review questions




Using arrays


The C program below is intended to continue asking for an input number until an input guess matches one of the numbers stored in the array. At line 12, what should replace the XX placeholders?

  1. int main( void )
  2. {
  3.   int numbers[] = { 3, 5, 6, 8, 11, 12, 16, 17, 18, 23, 24, 29, 30, 31, 37 };
  4.   int finish = 0;
  5.   int guess, index; 
  6.  
  7.   do
  8.   {
  9.     scanf( "%d", &guess );
  10.  
  11.     for ( index = 0; index < 15; index++ )
  12.       if ( numbers[ index ] == XX )
  13.         finish = 1;
  14.   } while ( ! finish );
  15. }