C ++ basic tutorial-array

Source: Internet
Author: User

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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.