Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

C Programming for the Absolute Beginner phần 6 ppt
MIỄN PHÍ
Số trang
36
Kích thước
1.6 MB
Định dạng
PDF
Lượt xem
1903

Tài liệu đang bị lỗi

File tài liệu này hiện đang bị hỏng, chúng tôi đang cố gắng khắc phục.

C Programming for the Absolute Beginner phần 6 ppt

Nội dung xem thử

Mô tả chi tiết

printf("\nInitialized character array:\n");

for ( x = 0; x < 6; x++ )

printf("%c", cName[x]);

} //end main

Figure 6.4 demonstrates why it is necessary to initialize arrays because old data may already

exist in each element. In the case of Figure 6.4, you can see leftover data (not assigned nor

initialized by me) stored in the cArray’s elements.

FIGURE 6.4

Initializing a

character-based

array.

Searching One-Dimensional Arrays

One of the most common practices with arrays is searching their elements for contents. Once

again, you will use looping structures, such as the for loop, to iterate through each element

until the search value is found or the search is over.

The concept of searching an array is demonstrated in the next program, which prompts a

user to enter a numeric search value.

#include <stdio.h>

main()

{

int x;

int iValue;

int iFound = -1;

int iArray[5];

138 C Programming for the Absolute Beginner, Second Edition

for ( x = 0; x < 5; x++ )

iArray[x] = (x + x); //initialize array

printf("\nEnter value to search for: ");

scanf("%d", &iValue);

for ( x = 0; x < 5; x++ ) {

if ( iArray[x] == iValue ) {

iFound = x;

break;

}

} //end for loop

if ( iFound > -1 )

printf("\nI found your search value in element %d\n", iFound);

else

printf("\nSorry, your search value was not found\n");

} //end main

As the preceding program shows, I use two separate loops: one for initializing my integer￾based array to the counting variable plus itself (iArray[x] = (x + x)) and the other, which

searches the array using the user’s search value.

Valid values for each preceding array element are shown in Table 6.1.

TABLE 6.1 V ALID ELEMENT V ALUES FOR IARRAY [ X ] = ( X + X )

Element Number Value after Initialization

0 0

1 2

2 4

3 6

4 8

Chapter 6 • Arrays 139

If a match is found, I assign the element to a variable and exit the loop with the break keyword.

After the search process, I alert the user if the value was found and at which element number.

If no match was found, I also alert the user.

Figure 6.5 demonstrates the output of the searching program.

FIGURE 6.5

Searching the

contents of an

array.

Remember that the break keyword can be used to exit a loop early. When C encounters the

break keyword in a loop, it moves program control to the next statement outside of the loop.

This can be a timesaving advantage when searching through large amounts of information.

TWO-DIMENSIONAL ARRAYS

Two-dimensional arrays are even more interesting structures than their single-dimension

counterparts. The easiest way to understand or think about two-dimensional arrays is to visu￾alize a table with rows and columns (e.g. a checkerboard, chessboard, or spreadsheet). C,

however, implements two-dimensional arrays as single-dimension arrays with pointers to

other single-dimension arrays. For ease of understanding, though, envision two-dimensional

arrays as a grid or table as mentioned previously.

Two-dimensional arrays are created similar to one-dimensional arrays, but with one excep￾tion: two-dimensional arrays must be declared with two separate element numbers (number

of columns and number of rows) as shown next.

int iTwoD[3][3];

The array declaration above creates a total of 9 elements (remember that array indexes start

with number 0). Two-dimensional arrays are accessed with two element numbers, one for the

column and one for the row.

Figure 6.6 demonstrates a two-dimensional array with nine elements.

140 C Programming for the Absolute Beginner, Second Edition

Tải ngay đi em, còn do dự, trời tối mất!