C++: a beginner's Guide, Second Edition


  C++ A Beginner’s Guide by Herbert Schildt  CRITICAL SKILL 4.1: Use one-dimensional arrays



Download 11,33 Mb.
Pdf ko'rish
bet60/194
Sana12.03.2022
Hajmi11,33 Mb.
#491693
1   ...   56   57   58   59   60   61   62   63   ...   194
Bog'liq
C A Beginner\'s Guide 2nd Edition (2003)


C++ A Beginner’s Guide by Herbert Schildt 
CRITICAL SKILL 4.1: Use one-dimensional arrays
 
A one-dimensional array is a list of related variables. Such lists are common in programming. For 
example, you might use a one-dimensional array to store the account numbers of the active users on a 
network. Another array might store the current batting averages for a baseball team. When computing 
the average of a list of values, you will often use an array to hold the values. Arrays are fundamental to 
modern programming.
The general form of a one-dimensional array declaration is
type name[size];
Here, type declares the base type of the array. The base type determines the data type of each element 
that makes up the array. The number of elements the array can hold is specified by size. For example, 
the following declares an integer array named sample that is ten elements long:
int sample[10];
An individual element within an array is accessed through an index. An index describes the position of 
an element within an array. In C++, all arrays have zero as the index of their first element. Because 
sample has ten elements, it has index values of 0 through 9. You access an array element by indexing the 
array using the number of the element. To index an array, specify the number of the element you want
surrounded by square brackets. Thus, the first element in sample is sample[0], and the last element is 
sample[9]. For example, the following program loads sample with the numbers 0 through 9:
The output from this example is shown here:
This is sample[0]: 0



C++ A Beginner’s Guide by Herbert Schildt 
This is sample[1]: 1
This is sample[2]: 2
This is sample[3]: 3
This is sample[4]: 4
This is sample[5]: 5
This is sample[6]: 6
This is sample[7]: 7
This is sample[8]: 8
This is sample[9]: 9
In C++, all arrays consist of contiguous memory locations. (That is, all array elements reside next to each 
other in memory.) The lowest address corresponds to the first element, and the highest address 
corresponds to the last element. For example, after this fragment is run:
nums looks like this:
Arrays are common in programming because they let you deal easily with sets of related
variables. Here is an example. The following program creates an array of ten elements and
assigns each element a value. It then computes the average of those values and finds the
minimum and the maximum value.



C++ A Beginner’s Guide by Herbert Schildt 
The output from the program is shown here:
Average is 34
Minimum value: -19



C++ A Beginner’s Guide by Herbert Schildt 
Maximum value: 100
Notice how the program cycles through the elements in the nums array. Storing the values in an array 
makes this process easy. As the program illustrates, the loop control variable of a for loop is used as an 
index. Loops such as this are very common when working with arrays.
There is an array restriction that you must be aware of. In C++, you cannot assign one array to another. 
For example, the following is illegal:
To transfer the contents of one array into another, you must assign each value individually, like this:
for(i=0; i < 10; i++) a[i] = b[i];
No Bounds Checking
C++ performs no bounds checking on arrays. This means that there is nothing that stops you from 
overrunning the end of an array. In other words, you can index an array of size N beyond N without 
generating any compile-time or runtime error messages, even though doing so will often cause 
catastrophic program failure. For example, the compiler will compile and run the following code without 
issuing any error messages even though the array crash is being overrun:
int crash[10], i;
for(i=0; i<100; i++) crash[i]=i;
In this case, the loop will iterate 100 times, even though crash is only ten elements long! This causes 
memory that is not part of crash to be overwritten.
Ask the Expert

Download 11,33 Mb.

Do'stlaringiz bilan baham:
1   ...   56   57   58   59   60   61   62   63   ...   194




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish