int hours [6];
该数组的名字是 hours。方括号内的数字是数组的大小声明符。它表示数组可以容纳的元素或值的数量。hours 数组可以存储 6 个元素,每个元素都是一个整数,如图 2 所示。const int SIZE = 6; int hours [SIZE];可以定义任何数据类型的数组,以下全部都是有效的数组定义:
float temperature [100] ; // 100 个 float 值的数组 char letter [2 6] ; // 26 个 char 值的数组 double size [1200] ; // 1200 个 double 值的数组 string name [10] ; // 10 个 string 对象的数组
short age[6];
在一台典型的 PC 上,一个短整型使用 2 个字节的内存,所以 age 数组将占用 12 个字节,如图 3 所示。数组声明 | 元素的数量 | 元素的大小 | 数组的大小 |
---|---|---|---|
char letter[26]; | 26 | 1个字节 | 26个字节 |
short ring[ 100]; | 100 | 2个字节 | 200个字节 |
int mile[84]; | 84 | 4个字节 | 336个字节 |
float temp [12]; | 12 | 4个字节 | 48个字节 |
double distance[1000]; | 1000 | 8个字节 | 8000个字节 |
hours[0] =20;
图 6 显示了该语句将 20 赋值给 hours[0] 之后的 hours 数组的内容。hours[3] = 30;
图 7 显示了执行这个语句之后数组的内容。int doctorA [5] ; //保留A医生每5天的看病人数 int doctorB [5] ; //保留B医生每5天的看病人数以下全部都是合法的赋值语句:
doctorA[0] =31; // doctorA[0]现已保存值 31 doctorA[1] = 40; //doctorA[1]现己保存值 40 doctorA[2] = doctorA[0]; //doctorA[2]也已保存值 31 doctorB [0] = doctorA[1] ; //doctorB [0]现已保存值 40但是,以下语句都是非法的:
doctorA = 152; //非法!数组不能作为一个整体赋值 doctorB = doctorA; //只能使用下标,每次给一个元素赋值
#include <iostream> using namespace std; int main() { const int NUM_EMPLOYEES = 6; int hours[NUM_EMPLOYEES]; // Input the hours worked by each employee cout << "Enter the hours worked by " << NUM_EMPLOYEES << " employees: "; cin >> hours[0]; cin >> hours[1]; cin >> hours[2]; cin >> hours[3]; cin >> hours[4]; cin >> hours[5]; //Display the contents of the array cout << "The hours you entered are:"; cout << " " << hours[0]; cout << " " << hours[1]; cout << " " << hours[2]; cout << " " << hours[3]; cout << " " << hours[4]; cout << " " << hours[5] << endl; return 0; }程序输出结果:
Enter the hours worked by 6 employees: 20 12 40 30 30 15
The hours you entered are: 20 12 40 30 30 15
#include <iostream> using namespace std; int main() { const int NUM_EMPLOYEES = 6; int hours[NUM_EMPLOYEES];// Holds hours worked for 6 employees int count; // Loop counter // Input the hours worked by each employee cout << "Enter the hours worked by " << NUM_EMPLOYEES << " employees: "; for (count = 0; count < NUM_EMPLOYEES; count++) cin >> hours[count]; // Display the contents of the array cout << "The hours you entered are:" ; for (count = 0; count < NUM_EMPLOYEES; count++) cout << " " << hours [count]; cout << endl; return 0; }程序运行结果为:
Enter the hours worked by 6 employees: 20 12 40 30 30 15
The hours you entered are: 20 12 40 30 30 15
#include <iostream> using namespace std; int main() { const int SIZE = 3; int A [SIZE] = {1, 1, 1}; int B[SIZE]; cout << "Here are the original numbers in 3-element array A: "; for (int count = 0; count < 3; count++) cout << A[count] << " "; cout << "\n\nNow I'm storing 7 numbers in 3-element array B."; for (int count = 0; count < 7; count++) B[count] =5; cout << "\nlf you see this message, the computer did not crash."; cout << "\n\nHere are the 7 numbers in array B : "; for (int count = 0; count < 7; count++) cout << B [count] << " "; cout << "\nHere are the numbers now in array A: "; for (int count = 0; count < 3; count ++) cout << A [count] <<" "; cout << "\n\nArray A's values were overwritten by \n" << "the values that did not fit in Array B. \n"; return 0; }程序输出结果为:
Here are the original numbers in 3-element array A: 1 1 1
Now I'm storing 7 numbers in 3-element array B.
lf you see this message, the computer did not crash.
Here are the 7 numbers in array B : 5 5 5 5 5 5 5
Here are the numbers now in array A: 5 5 5
Array A's values were overwritten by
the values that did not fit in Array B.
Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有