An array is used to store data in order in the memory. By using the array name and serial number, you can easily find and use data. This tutorial introduces arrays in C ++;
1. Start Geany
1) Click "Application-programming-Geany" in the menu to start Geany and create a c ++ source program;
2) Click the "file-save as" command in the menu and save the file to its own folder with "array" as the file name;
2. Enter the program code.
1) Let's define an array for storing student scores, which is input in the main function;
Int score [5] = {70, 60, 90, 85, 100 };
For (int I = 0; I <5; I ++)
{
Cout <score [I] <"";
}
2) The first sentence is to define an array. square brackets are used to indicate the array size. During definition, values can be assigned for initialization,
Use the for statement to display the array content. The array name score and the local variable I are used to represent each array element, from 0 to 4;
3) let's look at another character string, which has an ending mark at the end, so it can only store 4 characters;
Cout <endl;
Char ch [5] = "abcd"; // omit curly braces
For (int I = 0; I <5; I ++)
{
Cout <ch [I] <"";
}
4) A string is stored in a single sequence in memory, just like an array, but there is a flag at the end of the string;
A | B | c | d |
5) Therefore, the cout <ch; statement can be directly used to display string arrays. When the program is displayed, it automatically ends from ch;
# Include <iostream>
Using namespace std;
Int main (int argc, char ** argv)
{
Int score [5] = {70, 60, 90, 85, 100 };
For (int I = 0; I <5; I ++)
{
Cout <score [I] <"";
}
Cout <endl;
Char ch [5] = "abcd"; // omit curly braces
For (int I = 0; I <5; I ++)
{
Cout <ch [I] <"";
}
Cout <endl;
Cout <ch; // direct output
Return 0;
}