arrayone.cpp: small arrrays of integers #include <iostream> int main() { using namespace std; int yams[3]; yams[0] = 7; yams[1] = 8; yams[2] = 6; int yamcosts[3] = {20, 30, 5}; cout << "Total yams = "; cout << yams[0] + yams[1] + yams[2] << endl; cout << "The package with " << yams[1] << " yams costs "; cout << yamcosts[1] << " cents per yam.\n"; int total = yams[0] * yamcosts[0] + yams[1] * yamcosts[1]; total = total + yams[2] * yamcosts[2]; cout << "The total yam expense is " << total << " cents.\n"; cout << "\nSize of yams array = " << sizeof yams << " bytes.\n"; cout << "Size of one element: " << sizeof yamcosts[0] << " bytes.\n"; return 0; } 1. 编译输出: Total yams = 21 The package with 8 yams costs 30 cents per yam. The total yam expense is 410 cents. Size of yams array = 12 bytes. Size of one element: 4 bytes. 2. 代码详解:
|
|