Tải bản đầy đủ (.ppt) (20 trang)

C++ lecture 14

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (350.58 KB, 20 trang )

C++ Programming
Lecture 14

Arrays – Part I
The Hashemite University
Computer Engineering
Department
(Adapted from the textbook slides)


Outline








Introduction.
Arrays declarations and
initialization.
Const variables.
Character arrays.
Static arrays.
Examples.
The Hashemite University

2



Arrays I


Array







To refer to an element, specify




One type of data structures.
Consecutive group of memory locations
Same name and type
Static data structure in which its size remain
the same during the program execution.
Array name and position number

Format: arrayname[ position number ]



First element at position 0
n element array c:
c[ 0 ], c[ 1 ]…c[ n - 1 ]

The Hashemite University

3


Arrays II


Array elements are like normal variables
c[ 0 ] = 3;
cout << c[ 0 ];



Performing operations in subscript. If x = 3,
c[ 5 – 2 ] == c[ 3 ] == c[ x ]






ith element in an array has the index (or
subscript) of i – 1.
Array element i has a an index of i.
The subscript must be an integer value, where
it could be:






Subscript is always int variable or
Constant.
value
Variable.
Expression.
A result of a function call.
The Hashemite University

4


 

Arrays III
Name of array (Note that all elements
of this array have the same name, c)

Position number of
the element within
array c

c[0]

-45

c[1]

6


c[2]

0

c[3]

72

c[4]

1543

c[5]

-89

c[6]

0

c[7]

62

c[8]

-3

c[9]


1

c[10]

6453

c[11]

78

The Hashemite University

5


Declaring Arrays


Declaring arrays - specify:





Name
Type of array
Number of elements
Examples
int c[ 10 ];

float hi[ 3284 ];



Declaring multiple arrays of same type



Similar format as other variables
Example
int b[ 100 ], x[ 27 ];

The Hashemite University

6


Arrays Initialization


Two methods to initialize an array:





Using a loop.
Using an initializers list.

Initializers

int n[ 5 ] = { 1, 2, 3, 4, 5 };
 If not enough initializers, rightmost elements become 0
 If too many initializers, a syntax error is generated
int n[ 5 ] = { 0 }
 Sets all the elements to 0 since the first element is
initialized to 0 and the rest are implicitly initialized to 0.
int array[4]={5}
 Sets the first element to 5 and the rest to zero.



If size omitted, the initializers determine it
int n[] = { 1, 2, 3, 4, 5 };
 5 initializers, therefore n is a 5 element array
The Hashemite University

7


1
2
3
4
5
6
7
8
9
10
11

12
13
14
15
16
17
18
19
20
21
22

// Fig. 4.4: fig04_04.cpp
// Initializing an array with a declaration
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;

Notice how they array is
declared and elements
referenced.

int main()
{
int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
cout << "Element" << setw( 13 ) << "Value" << endl;
for ( int i = 0; i < 10; i++ )
cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl;

return 0;
}

Element
0
1
2
3
4
5
6
7
8
9

Value
32
27
64
18
95
14
90
70
60
37

The Hashemite University

8



const Variables










Also called named constants or read-only variables.
Reserve space in memory.
Must be initialized when declared to avoid getting a syntax
error.
A constant variable cannot be modified throughout the
program after it is being declared.
Arrays sizes are usually declared with type const since they
are static (fixed).
If you want to declare the size of an array using a variable
this variable must be declared const, otherwise you will get a
syntax error, e.g.:
int x = 10;
int c_class[x]; // syntax error
//But
const int x = 10;
int c_class[x]; //Correct


The Hashemite University

9


1

// Fig. 4.7: fig04_07.cpp

2

// A const object must be initialized

3
4

int main()

5

{

6

const int x;

// Error: x must be initialized

7
8


x = 7;

9
10

// Error:

Notice that const variables must be
cannot
modify
a const
initialized
because
they variable
cannot be modified
later.

return 0;

11 }

Fig04_07.cpp:
Error E2304 Fig04_07.cpp 6: Constant variable 'x' must be
initialized in function main()
Error E2024 Fig04_07.cpp 8: Cannot modify a const object in
function main()
*** 2 errors in Compile ***

The Hashemite University


10


Character Arrays I




Strings is the same as array of characters.
All strings end with NULL or '\0'
Character array initialization:


With string literal, Examples:
char string1[] = "hello";



With initializers, Examples:
char string1[] = { 'h', 'e', 'l', 'l', 'o', '\0’ };



Input from keyboard
char string2[ 10 ];
cin >> string2;





Takes user input
Side effect: if too much text entered, data written beyond
array (i.e. read the first n-1 characters and the last
character is set to \0)
The Hashemite University

11


Character arrays examples
int main()
{
char
arr[10]={'c','c'};
for(int i=0;i<10;i++)
{cout<return 0;
}

Output
cc

int main()
{
char arr[10]={“this
is a test”};
for(int i=0;i<10;i++)
{cout<return 0;

}

int main()
{
char arr[10];
cin>>arr //enter this is a
test
for(int i=0;i<10;i++)
{cout<return 0;
}

Syntax Error

Output
this |`||`||`|

This is the terminating string \0 and
garbage values
The Hashemite University

12


Character Arrays II


Subscripting is the same as for a normal array
String1[ 0 ] is 'h'
string1[ 2 ] is 'l'










Remember that cin stop reading from keyboard at
the first white space.
If you enter a string with smaller size than the
array size using cin, cin will automatically add a
NULL at the end of the string within the array.
cout stop printing an array of character when it
reaches the ‘\0’.
Character arrays that do not terminate with NULL
(i.e. not strings) cannot be printed using cout , it
will enter an infinite loop  logical error.
The Hashemite University

13


Inputted strings are
separated by
whitespace characters.
"there" stayed in the
buffer.


Notice how string
elements are referenced
like arrays.

Enter a
string1
string2
string1
H e l l
string1

string: Hello there
is: Hello
is: string literal
with spaces between characters is:
o
is: there
The Hashemite University

14


Static Arrays






Similar to static storage variable

you can define an array to be static.
So this array will not be destroyed
when you become outside the
scope in which it exists.
If you do not initialize a static array,
by default the compiler will initialize
all its elements to 0.
The Hashemite University

15


The Hashemite University

16


The Hashemite University

17


Output

The Hashemite University

18


Notes

Going outside the range of an array is a
logical error in C++.
int main()
{
char array[10];
cin>>array[20]; // logical error
}




There are additional examples of using
arrays in the textbook, study them.
The Hashemite University

19


Additional Notes


This lecture covers the following
material from the textbook:


Fourth Edition


Chapter 4: Sections 4.1 – 4.4


The Hashemite University

20



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×