inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

홍정모의 따라하며 배우는 C++

6.4 배열과 선택 정렬 selection sort

[배열을 레퍼런스로 보내는 것]

해결된 질문

232

호두

작성한 질문수 85

0

안녕하세요?

printArray함수에서 배열을 레퍼런스로 받으려고하면

void printArray(const int (&myArr)[10], const unsigned& length);

이렇게 선언 해줄 때 

const int (&myArr)[10]

여기 10은 그냥 항상 하드코딩 해줘야하는거죠?

감사합니다.

<코드>

// 6_4.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

using namespace std;

void printArray(const int (&myArr)[10], const unsigned& length);
void selectionSort(int myArr[], const unsigned& length);
void swap(int& xint& y);

int main()
{
    int myArr[] = { 221317262111537 };
    unsigned length = sizeof(myArr) / sizeof(myArr[0]);

    cout << length << endl;
    
    printArray(myArr, length);

    cout << "after selection sorting " << endl;

    selectionSort(myArr, length);

    cout << endl;

    printArray(myArr, length);


    return 0;
}

void printArray(const int (&myArr)[10], const unsigned& length)
{
    for (unsigned i = 0; i < length; ++i)
    {
        cout << myArr[i] << " ";
    }
    cout << endl;
}
void selectionSort(int myArr[], const unsigned& length)
{
    for (unsigned i = 0; i < length - 1; ++i)
    {
        int min_idx = i;
        for (unsigned j = i + 1; j < length; ++j)
        {
            if (myArr[min_idx] > myArr[j])
            {
                min_idx = j;
            }
        }
        swap(myArr[min_idx], myArr[i]);


    }
}
void swap(int& xint& y)
{
    int temp = x;
    x = y;
    y = temp;
}

// 6_4.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>

using namespace std;

void printArray(const int (&myArr)[10], const unsigned& length);
void selectionSort(int myArr[], const unsigned& length);
void swap(int& xint& y);

int main()
{
    int myArr[] = { 221317262111537 };
    unsigned length = sizeof(myArr) / sizeof(myArr[0]);

    cout << length << endl;
    
    printArray(myArr, length);

    cout << "after selection sorting " << endl;

    selectionSort(myArr, length);

    cout << endl;

    printArray(myArr, length);


    return 0;
}

void printArray(const int (&myArr)[10], const unsigned& length)
{
    for (unsigned i = 0; i < length; ++i)
    {
        cout << myArr[i] << " ";
    }
    cout << endl;
}
void selectionSort(int myArr[], const unsigned& length)
{
    for (unsigned i = 0; i < length - 1; ++i)
    {
        int min_idx = i;
        for (unsigned j = i + 1; j < length; ++j)
        {
            if (myArr[min_idx] > myArr[j])
            {
                min_idx = j;
            }
        }
        swap(myArr[min_idx], myArr[i]);


    }
}
void swap(int& xint& y)
{
    int temp = x;
    x = y;
    y = temp;
}


감사합니다.

C++

답변 1

1

홍정모

포인터 연산을 하려면 사이즈를 알아야 해서 하드코딩 해야만 합니다.
이건 C언어 문법에서 자세히 다루는 내용입니다. 
C++에서는 직접 배열을 사용하기 보다는 컨테이너를 쓰지요.

변수가 메모리에 저장되는 것을 알려주는 강의가 어떤강의였죠

1

466

1

메모리 주소 10진수로 출력

1

653

1

클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.

1

499

1

여러가지 리턴 타입에 관한 강의가 어떤 걸까요?

1

534

1

메모리 주소에 관한 질분

0

679

1

인터페이스 클래스에서 reportError의 매개변수에 대해 궁금한 것이 있습니다.

0

549

1

형변환 오버로딩에서 const 관련 질문이 있습니다.

0

443

1

Digit 뒤에 reference를 사용하는 이유

0

510

1

4.2 전역 변수, 정적 변수, 내부 연결, 외부 연결

0

323

1

dat파일이...

0

539

1

TODO:대입 연산자 오버로딩에 대한 소스코드입니다.

0

644

1

복사 생성자 관련 질문이 있습니다.

0

454

1

수업 중 궁금한점이 있습니다.

1

390

1

라이브러리자체가 이해가 되지 않습니다.

0

561

1

마지막 예제 질문

0

302

1

증감연산자 위치에 따른 수행 순서 질문입니다.

0

375

1

단항 연산자 오버로딩에서 return 부분에 질문이 있습니다.

1

411

1

friend함수 관련 질문이 있습니다.

0

312

1

operator+ 정의부분에서 궁금한 것이 있습니다.

0

447

1

3분 17초 질문

0

350

1

함수에 값을 대입한다는 개념이 이해가 되지 않습니다.

0

448

1

int getvalue() const에서 const는 왜 뒤에 붙는건가요?

0

445

2

const Something &st에서 const를 빼면 안되나요?

0

300

1

friend함수는 다른 클래스의 멤버함수로 쓸 수 없나요??

1

493

1