An array is a data format that can store multiple values of the same type.
Paste the following code:
#include <iostream>
int main ()
{
using namespace Std;
int yams[3];
Yams[0] = 7;
YAMS[1] = 8;
YAMS[2] = 6;
int yamcosts[3] = {20,30,50};
Yamcosts[3] = {20,30,50}; Not allowed;
Yams = yamcosts; Not allowed;
Float Fa[5] = {0.5f,2.3f}; Initializes only a subset of the elements, the compiler resets the other elements to 0
Float Fb[5] = {0.0}; Set all elements to 0
cout<<yams[0]<< "" <<yams[1]<< "" <<yams[2]<<endl;
cout<<yamcosts[0]<< "" <<yamcosts[1]<< "" <<yamcosts[2]<<endl;
Cout<<sizeof yams<<endl;
Cout<<sizeof yams[0]<<endl;
cout<<fa[3]<<endl;
Short test[] = {2,5,97,4};
int num_elements = sizeof test/sizeof (short);//This is a good solution when you don't care about the number of array elements.
cout<<num_elements<<endl;
return 0;
}
Extended:
1. Arrays are called compound types because they are created using other types. You can't just declare something as an array, it must be a specific type. Class such as: Flaot loans[20], where the type of loans is a float array instead of an array.
2, the importance of effective subscript. The compiler does not check if the subscript is valid. For example, if you assign a value to an element that does not exist months[101], the compiler does not give an error. However, this assignment can corrupt the data and may cause the program to terminate unexpectedly. Therefore, you must ensure that the program uses a valid subscript value.
C + + arrays