본문 바로가기

c++

이것이 c++이다 연습문제 1-6

1-6 다음과 같은 int 배열을 오름차순으로 정렬한 후 화면에 결과를 출력하는 프로그램을 작성하세요. 정렬 방법은 상관없습니다. 단, 화면에 배열 내용을 출력할 때는 반드시 '범위 기반 for문'을 사용해야 합니다.

 

int aList[5] = {40, 10, 20, 50, 30};

 

답:

#include "pch.h"
#include <iostream>
#include <tchar.h>
using namespace std;
#define max 5
void Swap(int &a, int &b)
{
int nTmp = a;
a = b;
b = nTmp;
}


int _tmain(int argc, _TCHAR* argv[])
{
int aList[max] = { 40,10, 20, 50, 30 };
for (int i = 0; i < max - 1; i++)
for (int j = i + 1; j < max; j++)
if (aList[i] < aList[j])
Swap(aList[i], aList[j]);
for (auto n : aList) cout << n << endl;
return 0;

}

코드창
결과창

 

'c++' 카테고리의 다른 글

이것이 c++이다 연습문제 1-5  (0) 2020.06.15
이것이 c++이다 연습문제 1-4  (0) 2020.06.14
이것이 c++이다 연습문제 1-3  (0) 2020.06.14
이것이 c++이다 연습문제 1-2  (0) 2020.06.14
이것이 c++이다 연습문제 1-1  (0) 2020.06.13