어레이

2427 단어 cpparrays
배열 요소의 액세스 및 수정이 수행되는 방법을 보여주는 간단한 코드:

//AccessingAndModifyingArrayElements.cpp : 이 파일에는 'main' 함수가 포함되어 있습니다. 프로그램 실행은 그곳에서 시작하고 끝납니다.
//

"pch.h" 포함



포함



네임스페이스 표준 사용;

정수 메인()
{
//std::cout << "안녕하세요!\n";

//1. Accessing array elements
cout << "1. Accessing array elements : " << endl;
int arr[]{ 100, 20, 30, 50, 60 };

cout << "Array element 1: " << arr[0] << endl;
cout << "Array element 2: " << arr[1] << endl;
cout << "Array element 3: " << arr[2] << endl;
cout << "Array element 4: " << arr[3] << endl;
cout << "Array element 5: " << arr[4] << endl;

//2. Arrays are not check for out of bounds :
// Most probably code will crash in such situations.
//cout << "enter any number for arr[5] : " << endl;

//cin >> arr[5];

//3. Initializing array:
cout << "\n";
cout << "===============" << endl;
cout << "\n";
//int arr1[5];
//int arr2[5]{};
int arr3[5]{100, 90};

cout << "Array element 1: " << arr3[0] << endl;
cout << "Array element 2: " << arr3[1] << endl;
cout << "Array element 3: " << arr3[2] << endl;
cout << "Array element 4: " << arr3[3] << endl;
cout << "Array element 5: " << arr3[4] << endl;

//4. Modifying array elements
cout << "Enter 5 nums for arr3 : ";
cin >> arr3[0];
cin >> arr3[1];
cin >> arr3[2];
cin >> arr3[3];
cin >> arr3[4];

cout << "MOdified array : " << endl;
cout << arr3[0] << endl;
cout << arr3[1] << endl;
cout << arr3[2] << endl;
cout << arr3[3] << endl;
cout << arr3[4] << endl;

cout << "the value of array name is : " << arr3 << endl;  // this shows the address value of 1st element of array.

return 0;

}

산출:

좋은 웹페이지 즐겨찾기