Introduction to Computing – B142L
Arrays and strings
Academic year
2011/12
Objectives:
Concept of an array
Description of an array
Using an array
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 .
Accept monthly revenue input for a period of 3 months
Display the monthly average and total revenue earned in the quarter
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 .
Accept monthly revenue input for a period of 12 months
Display the monthly average and total revenue earned in the quarter
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 .
The array name is months
There are 12 elements in the array
The range of valid elements is 1 to 12
For example, the first element is 1 and the last element is 12
The array length is 12
The length has one value , which is 12 in this example
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 .
The array name is months
There are 12 elements in the array
The range of valid elements is 1 to 12
For example, the first element is 1 and the last element is 12
The array length is 12
The length has one value , which is 12 in this example
An array allows variables to be easily controlled using iteration .
Individual elements of an array are directly accessed using an index value
The first element of an array always starts at index zero
The index value of the last element of an array is one less than the array length
The array length is the not the same as an element index
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 .
The array name is months
There are 12 elements in the array
The range of valid elements is 1 to 12
For example, the first element is 1 and the last element is 12
The array length is 12
The length has one value , which is 12 in this example
An array allows variables to be easily controlled using iteration .
Individual elements of an array are directly accessed using an index value
The first element of an array always starts at index zero
The index value of the last element of an array is one less than the array length
The array length is the not the same as an element index
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 .
#include
#include
int main( void )
{
char word1[] = "Welcome";
char word2[ 30 ] = { '\0' };
char word3[ 15 ] = { 't', 'o', 'b', '1', '4', '2', 'L' };
printf( "%d \n", strlen( word1 ) );
printf( "%d \n", strlen( word2 ) );
printf( "%d \n", strlen( word3 ) );
printf( "%d \n", strlen( "computers" ) );
}
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 .
The return value is zero if the 2 strings are identical, otherwise it returns a non-zero value that has the same sign as the difference between the first differing pair of characters.
Compares a specific number of characters in the string.
#include
#include
int main( void )
{
char word1[] = "Welcome";
char word2[ 30 ] = { '\0' };
char word3[ 15 ] = { 't', 'o', 'b', '1', '4', '2', 'L' };
printf( "%d \n", strncmp( word1, word2, 6 ) );
printf( "%d \n", strncmp( word1, word3, 6 ) );
printf( "%d \n", strncmp( word1, "Welcome", 6 ) );
printf( "%d \n", strncmp( word1, "welcome", 6 ) );
printf( "%d \n", strncmp( word1, "Welcome back", 8 ) );
printf( "%d \n", strncmp( word1, "Welcome back", 4 ) );
}
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 .
#include
#include
int main( void )
{
char word1[] = "Welcome";
char word2[ 30 ] = { '\0' };
char word3[ 15 ] = { 't', 'o', ' ', 'B', '1', '4', '2', 'L' };
sprintf( word2, "%s %s", word1, word3 );
printf( "%s \n", word2 );
sprintf( word2, "%d + %d = %d", 2, 8, 10 );
printf( "%s \n", word2 );
}
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 .
#include
#include
int main( void )
{
char word1[] = "Welcome";
char word2[ 30 ] = { '\0' };
char word3[ 15 ] = { 't', 'o', ' ', 'B', '1', '4', '2', 'L' };
strcpy( word2, word1 );
printf( "%s \n", word2 );
strcpy( word2, word3 );
printf( "%s \n", word2 );
strcpy( word2, "computing lab" );
printf( "%s \n", word2 );
}
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 .
#include
#include
int main( void )
{
char word1[] = "Welcome";
char word2[ 30 ] = { '\0' };
char word3[ 15 ] = { 't', 'o', ' ', 'B', '1', '4', '2', 'L' };
strcat( word2, word1 );
printf( "%s \n", word2 );
strcat( word2, word3 );
printf( "%s \n", word2 );
strcat( word2, "computing lab" );
printf( "%s \n", word2 );
}
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?
int main( void )
{
int age[ 40 ] = { 0 } ;
int youngest = -1 ;
int counter;
for ( counter = 0 ; counter < 40 ; counter++ )
{
scanf( "%d" , &age[ counter ] ) ;
if ( ( age[ counter ] < youngest ) || ( youngest < 0 ) )
youngest = age[ counter ] ;
}
printf ( "The youngest age is %d" , youngest
) ;
}
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?
int main( void )
{
double prices[ 50 ] ;
double total = 0 ;
int index;
for ( index = 0 ; index < XX; index++ )
{
scanf( "%lf" , &prices[ index ] ) ;
total += prices[ index ] ;
}
printf ( "Average ticket price is %0.2lf" , total /
50 ) ;
}
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?
int main( void )
{
int numbers[ ] = { 3 , 5 , 6 , 8 , 11 , 12 , 16 , 17 , 18 , 23 , 24 , 29 , 30 , 31 , 37 } ;
int finish = 0 ;
int guess, index;
do
{
scanf( "%d" , &guess ) ;
for ( index = 0 ; index < 15 ; index++ )
if ( numbers[ index ] == XX )
finish = 1 ;
} while ( ! finish ) ;
}