Download Array - bYTEBoss

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ARRAYS
Definition
• is a sequence of contiguous memory
locations that each contains data of the same
type.
• Each value is referred to as an element of
the array
• only one name is assigned to an entire array
• individual elements are accessed by
specifying the array name and its
subscript(index)
One Dimensional Arrays
• Syntax
data type
array-name [ number of elements ]
• Example
int numbers [ 7 ];
declares an array named numbers that contains
seven int type elements
Conceptual View
int Scores [6];
float Prices [4];
char Grades [5];
Scores[0]
Prices[0]
Grades[0]
Scores[1]
Prices[1]
Grades[1]
Scores[2]
Prices[2]
Grades[2]
Scores[3]
Prices[3]
Grades[3]
Scores[4]
Scores[5]
Grades[4]
Array name Array Length Each Element Size Each Element Type Array Size
____________________________________________________________________
Scores
Prices
Grades
6
4
5
2 bytes
4 bytes
1 byte
int
float
char
12 bytes
16 bytes
5 bytes
NOTES
the length of an array is the number of data elements in the array, indicating the
number of cells
the size of an array is the array length times the size of an element, indicating number
of bytes
Accessing Array Element
• Accessing array elements means to be able
to store values in and retrieve values from
each array element
• each element is assigned a unique number
called subscript or index
• index 0 is assigned to the first element
Notes
• C doesn’t warn you when an array subscript
exceeds the length of the array
• index assigned to first element in an array is
number 0; thus the last index is 1 less than
the length of the array
• any integer number can be used as the array
index
• an individual array element can be used
anywhere that a simple variable is used.
Examples
Scores[5]; /* accessing the sixth element of the array Scores */
Grade[0]; /* accessing the first element of the array Grade */
Prices[1]; /* accessing the sixth element of the array Prices */
int offset=3;
Scores[offset+2]; /* accessing the sixth element of the array
Scores */
Grade[offset-2];
Prices[++offset];
/* accessing the second element of the
array Grade */
/* accessing the forth element of the array
Prices */
More examples
• The assignment statement
Scores[1]=10;
• increment operator
++ Scores[2];
• decrement operator
--Scores[2];
• assignment operator
keep=Scores[5];
• printf statement
printf(“%d”, Scores[1]);
Array Initializations
• int table[4]={10, 20, 30, 40};
• char alphabet[3]={‘A’, ‘B’, ‘C’};
• int table[ ]={10, 20, 30, 40};
– the array length may be omitted when initializing
values are provided in the declaration statement.
• int table[100] = {0}
– initializes the whole array, 100 elements, to zero
Conceptual View
• int Scores[6]={99,87,91,55,67,75}
• float Prices[4]={11.09, 9.88, 18.67, 7.08}
int Scores [6];
float Prices [4];
99
Scores[0]
11.09
Prices[0]
87
Scores[1]
Prices[1]
91
Scores[2]
9.88
18.67
55
Scores[3]
7.08
Prices[3]
67
Scores[4]
75
Scores[5]
Prices[2]
Program Example
/* Definition: This program shows the array declaration, initialization, and accessing
the array elements. */
# include <stdio.h>
int main ( )
{
/* declaring and initializing an array */
int Scores[ 6 ] = { 99, 87, 91, 55, 67, 75 } ;
int i ;
/* reading data from the array */
for ( i = 0 ; i < 6 ; ++i )
printf ( " Scores [%d] ------> %d\n", i, Scores [ i ] ) ;
return 0 ;
}
Program Output
Scores[0] -----> 99
Scores[1] -----> 87
Scores[2] -----> 91
Scores[3] -----> 55
Scores[4] -----> 67
Scores[5] -----> 75
Using loop
int table[100];
/*declare an int type array that contains 100
elements */
int i;
/* i is the loop counter */
for(i=0; i<100; ++i)
table[i]=-1; /*assign -1 to the ith element of the array */
Array
/* initializing an array to characters A-Z Version 1*/
char table[26];
int i;
char ch;
for(i=0, ch=‘A’; ch<=‘Z’; ++i, ch++)
table[i]=ch;
/* initializing an array to characters A-Z Version 2*/
char table[26];
int i=0;
char ch=‘A’;
for(; ch<=‘Z’; )
table[i++]=ch++;
Arrays and Pointers
Address
int X=20;
2000
2002
address of X
2004
&X
20
&X = = 2006
X = = 20
Value of the variable
Address of the
variable
2006
2008
2010
X
name of the variable
Array Elements Address
Computation
&Scores[index] == &Scores[0]+(index *2)
Examples
&Scores[3]==&Scores[0]+(3*2)
Accessing Array Elements Using Pointer
int *P_Scores;
int Scores[6]={99,87,91,55,67,75};
P_Scores = &Scores[0]; /* assign array address to a pointer
variable */
Scores[0] = = *P_Scores == 99
Scores[3] = = *(P_Scores + 3) = = 55
Conceptual View
int Scores[6]={99,87,91,55,67,75};
P_Score
Address of
Scores[0]
The direct
access method
The indirect
access method
Scores[0]
99
*P_Scores
Scores[1]
87
*(P_Scores+1)
Scores[2]
91
*(P_Scores+2)
Scores[3]
55
*(P_Scores+3)
Scores[4]
67
*(P_Scores+4)
Scores[5]
75
*(P_Scores+5)
Array Access Using Subscript and
Pointer Notations
int Scores[6] = {99,87,91,55,67,75};
int *P_Scores=&Scores[0];
Array
Element
0
1
2
3
4
5
Subscript
Notation
Scores[0]
Scores[1]
Scores[2]
Scores[3]
Scores[4]
Scores[5]
Pointer
Notation
*(P_Scores+0)
*(P_Scores+1)
*(P_Scores+2)
*(P_Scores+3)
*(P_Scores+4)
*(P_Scores+5)
Contents of
the Element
99
87
91
55
67
75
Program Example
/* Definition: This program shows the array declaration, initialization, and indirect
(pointer) method of accessing the arrays elements. */
# include <stdio.h>
int main ( )
{
/* declaring and initializing the array */
int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75} ;
int i ;
/* declare an int variable */
int * P_Scores ;
/* declare an int type pointer */
P_Scores = & Scores[ 0 ];
/* assign the array starting address */
for ( i = 0 ; i < 6 ; ++i )
printf ( " Scores [%d] ------> %d\n", i, * (P_Scores + i ) );
return 0 ;
}
Program Output
Scores[0] -----> 99
Scores[1] -----> 87
Scores[2] -----> 91
Scores[3] -----> 55
Scores[4] -----> 67
Scores[5] -----> 75
Name of the array
• &Scores = = Scores
equivalent
P_Scores = &Scores[0];
• Scores = &Scores[1];
/*invalid assignment */
Notes
• An array name is a pointer constant and the address of
a pointer constant cannot be obtain. Any attempt to
use & with an array .
• The address stored in the array name, a pointer
constant, cannot be changed
• The address stored in the array name is the address of
the array’s first element, thus
Score = = &Scores[0]
Accessing Array Element
Array
Element
0
1
2
3
4
5
Subscript
Notation
Scores[0]
Scores[1]
Scores[2]
Scores[3]
Scores[4]
Scores[5]
Pointer
Contents of
Notation(Array name) the Element
*(Scores+0)
99
*(Scores+1)
87
*(Scores+2)
91
*(Scores+3)
55
*(Scores+4)
67
*(Scores+5)
75
Program Example
/* Definition: This program shows the array declaration, initialization, and indirect
(using the array name as pointer) method of accessing the arrays elements. */
# include <stdio.h>
int main ( )
{
/* declaring and initializing the array */
int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75 } ;
int i ;
/* declare an int variable */
for ( i = 0 ; i < 6 ; ++i )
printf ( " Scores [%d] ------> %d\n", i, * (Scores + i ) ) ;
return 0 ;
}
Program Example
/* Definition: This program shows the array declaration, initialization, and indirect
(using the pointer in the script notation) method of accessing the arrays elements.*/
# include <stdio.h>
int main ( )
{
/* declaring and initializing the array */
int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75 } ;
int i ;
/* declare an int variable */
int * P_Scores = Scores ;
/* declare and initialize an int type pointer */
for ( i = 0 ; i < 6 ; ++i )
printf ( " Scores [%d] ------> %d\n", i, P_Scores [ i ] );
return 0 ;
}
POINTER ARITHMETIC
Pointer Arithmetic
• A pointer is a valid operand only for the
addition and substraction operators
• In performing arithmetic on pointers we
should be careful to produce addresses that
point to something meaningful
Array’s Conceptual View Memory
int Scores[6];
int *P_Scores;
P_Scores = Scores
P_Scores
2000
The array name also
point to the starting
address of the array
2000
Scores[0]
2002
2004
2006
2008
Scores[1]
Scores[2]
Scores[3]
Scores[4]
2010
Scores[5]
Common Error
• Attempting to obtain the address of a
pointer constant.
P_Scores = &Scores is invalid, should be
P_Scores = Scores;
Pointer Manipulation Examples
/* Pointer current Value is 2000 */
Pointer Type Expression New Address Expression New Address
char (1 byte)
int (2 byte)
float (4 bytes)
double (8 bytes)
int (2 bytes)
char (1 byte)
Pointer ++;
++ Pointer;
Pointer ++;
++Pointer;
Pointer +=5;
Pointer +=5;
2001
2002
2004
2008
2010
2005
-- Pointer;
-- Pointer;
-- Pointer;
-- Pointer;
Pointer -=5;
Pointer -=5;
1999
1998
1996
1992
1990
1995
Examples
int Scores[6];
/*declare an array */
int R, P = Scores; /* declare and initialize a pointer variable */
Alternative
Statement
Statement
Explanation
R = *++P
R = *--P
R = *P++
R = *P-R = ++*P
R = * (++P)
Increment the pointer P, and then assign the value
pointed to by P to R
R = * (--P)
Decrement the pointer P, and then assign the
value pointed to by P to R
R = * (P++)
Assign the value ponited to by P to R and then
increment the pointer P
R = * (P--)
Assign the value pointed by P to R, and then
decrement the pointer
R = ++ (*P)
Increment the value pointed to by P, and then
assign the new value to R; the pointer P remains
unchanged
Examples
int Scores[6];
/*declare an array */
int R, P = Scores; /* declare and initialize a pointer variable */
Alternative
Statement
Statement
Explanation
R = --*P
R = (*P)++
R = (*P)
R = -- (*P)
Decrement the value pointed to by P and then
assign the new value to R; the pointer P remains
unchanged.
Retrieve the value pointed to by the pointer P,
and then increment the value pointed to by the
pointer P; the pointer P remains unchanged
Retrieve the value pointed to by the pointer P,
and then decrement the value pointed to by the
pointer P; the pointer P remains unchanged
Program Example
This program shows the array declaration, initialization, and indirect
(using a pointer ) method of accessing the arrays elements.
# include <stdio.h>
/* including the
int main( )
{
/* declaring and initializing the array */
int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75
int i ;
/* declare an int
int * P_Scores ;
/* declare an int
stdio library */
} ;
variable */
type pointer */
P_Scores = Scores ;
/* assign the array starting address */
for ( i = 0 ; i < 6 ; ++i )
printf ( " Scores [%d] ------> %d\n", i, * P_Scores++ ) ;
return 0 ;
}
Pointer Comparison
int Scores[6], *P_Scores;
P_Scores = Scores;
P_Scores == &Scores /* comparing the address of the 1st element */
P_Scores + 5 == &Scores[5] /* comparing the address of the fifth element */
P_Scores < &Scores[1] /* comparing the addresses of the first and second
elements */
Program Example
This program shows the array declaration, initialization,
indirect (using the pointer) method of accessing the
arrays ele-ments, and pointers comparisons.
# include <stdio.h>
/* including the stdio library */
int main( )
{
/* declaring and initializing the array */
int Scores [ 6 ] = { 99, 87, 91, 55, 67, 75 } ;
int i = 6 ;
/* declare and initialize an int variable */
int * P_Scores ;
/* declare an int type pointer */
P_Scores = &Scores[5];
/* assign the address of the array
last element */
while ( P_Scores >= Scores )
printf ( " Scores [%d] --> %d\n", --i, * P_Scores -- ) ;
return 0 ;
}
Passing arrays as function argument
Function declaration
float average(int, int, int);
Function call
printf (“%.2f\n”, average(table[0], table[1],table[2]));
Function header
float average(int n1, int n2, int n3)
Passing an Array to a Function
…
int table [ ] = {10, 20, 30}
float average (int nums [ ]);
Starting
address of the array ----- creating the array
...
Referencing the same array table[0]
…
printf(“%.2f\n”, average (table));
table[1]
return 0;
table[2]
…
float average(int nums[ ])
{
in main
return …;
}
nums[0]
nums[1]
nums[2]
in average
Passing arrays as function argument
Function declaration
float average(int[ ]);
Function call
printf (“%.2f\n”, average(table));
Function header
float average(int nums[ ])
Program Example....continue
This program shows the array declaration, initialization,and different
ways of passing an entire array to a function.
# include <stdio.h>
# define LEN 4
int main ( )
{
int table [ 4] ;
void input ( int [ ] )
int largest ( int [ ]
void display ( int *
/* call functions */
input ( table ) ;
display ( &table[0] ) ;
printf ( "The largest is
/* display the largest */
return 0 ;
}
/* including the stdio library */
/* create a constant LEN */
/* declaring an int type array */
; /* declaring the function input */
) ;/* declaring the function largest */
) ;/* declaring the function display */
%d \n",
/* read input data */
/* display the array */
largest ( table ) ) ;
...continue
/* input: This function receives an array address. It prompts the user
to enter 4 integer numbers and saves them in the array elements. */
void input ( int * ptr )
{
int i ;
/* declare the loop counter variable */
printf ( ”Enter 4 numbers\n" ) ; /* informative message */
for ( i = 0 ; i < LEN ; i++, ptr ++ )
{
printf ( "#%d: ", i + 1 ) ;/* prompt for number */
scanf ( "%d", ptr ) ;
/* read and save one number */
}
return ;
/* end of function */
}
...continue
/* Largest:
This function receives an array address, finds the
largest of the array elements, and returns it to the caller. */
int largest ( int table [ ] )
{
int index;
/* the loop counter */
/*declare Lnum and initialize it to the value of the array 1st
element */
int Lnum = table [ 0] ;
for ( index = 1 ; index < LEN ; ++index )/* iterate 4 times
*/
if ( Lnum < table [index] )
/* if a new larger number
*/
Lnum = table [index] ;
/* assign the new value */
return ( Lnum ) ;
/* return the largest */
}
...end of program example
/* display:
This function receives an array address, and
displays the array elements. */
void display ( int array [ ] ) /* array notation is used in
the parameter */
{
int i ;
/* the loop counter */
for ( i = 0 ; i < 4 ; ++i )
printf ( "[%d] ", array [ i ] ) ;
putchar ( '\n' ) ;
return ;
}
Program output
Enter four numbers
#1: 10
#2: 5
#3: 6
#4: 40
[10][5][6][40]
The largest is 40