数组的定义、数组的取值、数组的遍历
实现:用二维数组画出地图
// 2021.2.24数组.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//数组:一组数据的集合
//一维数组的定义:数组类型+数组名称[元素个数]
//int arrNum[5] = {1,5,7,9,2};//数组元素之间用,隔开
//数组元素的访问:数组名称[索引] arrNum[2]=7
//cout << arrNum[2] << endl; 结果为7
//数组的遍历
// for (int i = 0; i < 5; i++)
// {
// cout << arrNum[i] << endl; //
// }
//二维数组的定义:数组类型+数组名称[第一个维度的个数][第二个维度的个数]
//int arrNum[2][3] = {
{1,3.4},
{6,8.9}
};
//arrNum[1][1]=8
//int arrNum[2][3] = {5,6};//5,6,0, 0,0,0
//二维数组元素的访问:数组名称[第一个维度的索引][第二个维度的索引]
//cout << arrNum[1][1] << endl;
int arrMap[15][20] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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