兰州理工大学研究生课程VC++课件4
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
double engine_size[10]; // Engine size in cubic inches double horsepower[10]; // Engine power output
Using Arrays
• As a basis for an exercise in using arrays, imagine that you have kept a record of both the amount of gasoline you have bought for the car and the odometer reading on each occasion. • You can write a program to analyze this data to see how the gas consumption looks on each occasion that you bought gas:
HANDLING MULTIPLE DATA VALUES OF THE SAME TYPE
• You already know how to declare and initialize variables of various types that each holds a single item of information. • The most obvious extension to the idea of a variable is to be able to reference several data elements of a particular type with a sFra Baidu bibliotekngle variable name. • This would enable you to handle applications of a much broader scope.
Declaring Arrays
• You declare an array in essentially the same way as you declared the variables that you have seen up to now, the only difference being that the number of elements in the array is specified between square brackets immediately following the array name. • For example, you could declare the integer array height, shown in the previous figure, with the following declaration statement:
– }
• // Output results from 2nd entry to last entry
– for (int i = 1; i < count; i++) – {
• cout << endl
– – – – << setw(2) << i << "." // Output sequence number << "Gas purchased = " << gas[i] << " gallons" // Output gas << " resulted in " // Output miles per gallon << (miles[i] - miles[i - 1]) / gas[i] << " miles per gallon.";
• • • • • • • cout << endl << "Enter gas quantity: "; cin >> gas[count]; // Read gas quantity cout << "Enter odometer reading: "; cin >> miles[count]; // Read odometer value ++count; cout << "Do you want to enter another(y or n)? "; cin >> indicator;
Arrays
• The basis for the solution to all of these problems is provided by the array. • An array is simply a number of memory locations called array elements or simply elements, each of which can store an item of data of the same given data type, and which are all referenced through the same variable name.
• Of course, you would also need some means of picking out a particular employee from the whole bunch, together with the data from the generic variables associated with them. • This kind of requirement arises with any collection of like entities that you want to handle in your program, whether they’re baseball players or battleships. • Naturally, C++ provides you with a way to deal with this.
• To refer to a particular element, you write the array name, followed by the index value of the particular element between square brackets.
• The amount of memory required to store each element is determined by its type, and all the elements of an array are stored in a contiguous block of memory.
• A much more convenient way to handle such a problem would be to reference an employee by some kind of generic name — employeeName to take an imaginative example — and to have other generic names for the kinds of data related to each employee, such as pay, tax, and so on.
long height[6];
• Because each long value occupies 4 bytes in memory, the whole array requires 24 bytes.
• You can declare arrays to be of any type.
• For example, to declare arrays intended to store the capacity and power output of a series of engines, you could write the following:
– – – – – – – const int MAX(20); // Maximum number of values double gas[MAX]; // Gas quantity in gallons long miles[MAX]; // Odometer readings int count(0); // Loop counter char indicator('y'); // Input indicator while (('y' == indicator || 'Y' == indicator) && count < MAX) {
•
}
– if (count <= 1) // count = 1 after 1 entry completed – { // ... we need at least 2
• cout << endl << "Sorry - at least two readings are necessary."; • return 0;
• Suppose that you needed to write a payroll program. • Using a separately named variable for each individual’s pay, their tax liability, and so on, would be an uphill task to say the least.
4. Arrays,Strings,and Pointers
201405
WHAT YOU WILL LEARN IN THIS CHAPTER:
• • • • • • • • How to use arrays How to declare and initialize arrays of different types How to use the range-based for loop with an array How to declare and use multidimensional arrays How to use pointers How to declare and initialize pointers of different types The relationship between arrays and pointers How to declare references and some initial ideas on their uses
• You select a particular element in an array using an index value, which is simply an integer representing the sequence number of the element in the array. • The first element has the sequence number 0, the second 1, and so on. • You can also envisage the index value of an array element as being an offset from the first element in an array. • The first element has an offset of 0 and therefore an index of 0, and an index value of 3 will refer to the fourth element of an array.
• • • • • • • • • •
// EX04-01.cpp // Calculating gas mileage #include <iostream> #include <iomanip> using std::cin; using std::cout; using std::endl; using std::setw; int main() {
Using Arrays
• As a basis for an exercise in using arrays, imagine that you have kept a record of both the amount of gasoline you have bought for the car and the odometer reading on each occasion. • You can write a program to analyze this data to see how the gas consumption looks on each occasion that you bought gas:
HANDLING MULTIPLE DATA VALUES OF THE SAME TYPE
• You already know how to declare and initialize variables of various types that each holds a single item of information. • The most obvious extension to the idea of a variable is to be able to reference several data elements of a particular type with a sFra Baidu bibliotekngle variable name. • This would enable you to handle applications of a much broader scope.
Declaring Arrays
• You declare an array in essentially the same way as you declared the variables that you have seen up to now, the only difference being that the number of elements in the array is specified between square brackets immediately following the array name. • For example, you could declare the integer array height, shown in the previous figure, with the following declaration statement:
– }
• // Output results from 2nd entry to last entry
– for (int i = 1; i < count; i++) – {
• cout << endl
– – – – << setw(2) << i << "." // Output sequence number << "Gas purchased = " << gas[i] << " gallons" // Output gas << " resulted in " // Output miles per gallon << (miles[i] - miles[i - 1]) / gas[i] << " miles per gallon.";
• • • • • • • cout << endl << "Enter gas quantity: "; cin >> gas[count]; // Read gas quantity cout << "Enter odometer reading: "; cin >> miles[count]; // Read odometer value ++count; cout << "Do you want to enter another(y or n)? "; cin >> indicator;
Arrays
• The basis for the solution to all of these problems is provided by the array. • An array is simply a number of memory locations called array elements or simply elements, each of which can store an item of data of the same given data type, and which are all referenced through the same variable name.
• Of course, you would also need some means of picking out a particular employee from the whole bunch, together with the data from the generic variables associated with them. • This kind of requirement arises with any collection of like entities that you want to handle in your program, whether they’re baseball players or battleships. • Naturally, C++ provides you with a way to deal with this.
• To refer to a particular element, you write the array name, followed by the index value of the particular element between square brackets.
• The amount of memory required to store each element is determined by its type, and all the elements of an array are stored in a contiguous block of memory.
• A much more convenient way to handle such a problem would be to reference an employee by some kind of generic name — employeeName to take an imaginative example — and to have other generic names for the kinds of data related to each employee, such as pay, tax, and so on.
long height[6];
• Because each long value occupies 4 bytes in memory, the whole array requires 24 bytes.
• You can declare arrays to be of any type.
• For example, to declare arrays intended to store the capacity and power output of a series of engines, you could write the following:
– – – – – – – const int MAX(20); // Maximum number of values double gas[MAX]; // Gas quantity in gallons long miles[MAX]; // Odometer readings int count(0); // Loop counter char indicator('y'); // Input indicator while (('y' == indicator || 'Y' == indicator) && count < MAX) {
•
}
– if (count <= 1) // count = 1 after 1 entry completed – { // ... we need at least 2
• cout << endl << "Sorry - at least two readings are necessary."; • return 0;
• Suppose that you needed to write a payroll program. • Using a separately named variable for each individual’s pay, their tax liability, and so on, would be an uphill task to say the least.
4. Arrays,Strings,and Pointers
201405
WHAT YOU WILL LEARN IN THIS CHAPTER:
• • • • • • • • How to use arrays How to declare and initialize arrays of different types How to use the range-based for loop with an array How to declare and use multidimensional arrays How to use pointers How to declare and initialize pointers of different types The relationship between arrays and pointers How to declare references and some initial ideas on their uses
• You select a particular element in an array using an index value, which is simply an integer representing the sequence number of the element in the array. • The first element has the sequence number 0, the second 1, and so on. • You can also envisage the index value of an array element as being an offset from the first element in an array. • The first element has an offset of 0 and therefore an index of 0, and an index value of 3 will refer to the fourth element of an array.
• • • • • • • • • •
// EX04-01.cpp // Calculating gas mileage #include <iostream> #include <iomanip> using std::cin; using std::cout; using std::endl; using std::setw; int main() {