-
C++)to sort an array in Descending Order& Ascending Order코딩Coding/C++_연습 2020. 12. 11. 09:24728x90
#include <iostream> using namespace std; #define MAX 100 int main() { //array declaration int arr[MAX]; int n, i, j; int temp; //read total number of elements to read cout << "Enter total number of elements to read: "; cin >> n; //check bound if (n<0 || n>MAX) { cout << "Input valid range!!!" << endl; return -1; } //read n elements for (i = 0; i < n; i++) { cout << "Enter element [" << i + 1 << "] "; cin >> arr[i]; } //print input elements cout << "Unsorted Array elements:" << endl; for (i = 0; i < n; i++) cout << arr[i] << "\t"; cout << endl; //sorting - Descending ORDER for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (arr[i] < arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } //print sorted array elements cout << "Sorted (Descending Order) Array elements:" << endl; for (i = 0; i < n; i++) cout << arr[i] << "\t"; cout << endl; return 0; }
결과
https://www.includehelp.com/cpp-programs/sort-an-array-in-descending-order.aspxC++ program to sort an array in Descending Order - IncludeHelp
Home » C++ programs » C++ Most popular & searched programs C++ program to sort an array in Descending Order In this program, we will learn how to sort integer array numbers/elements in Descending Order in C++? This program will read total number of eleme
www.includehelp.com
#include <iostream> using namespace std; #define MAX 100 int main() { //array declaration int arr[MAX]; int n, i, j; int temp; //read total number of elements to read cout << "Enter total number of elements to read: "; cin >> n; //check bound if (n<0 || n>MAX) { cout << "Input valid range!!!" << endl; return -1; } //read n elements for (i = 0; i < n; i++) { cout << "Enter element [" << i + 1 << "] "; cin >> arr[i]; } //print input elements cout << "Unsorted Array elements:" << endl; for (i = 0; i < n; i++) cout << arr[i] << "\t"; cout << endl; //sorting - ASCENDING ORDER for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } //print sorted array elements cout << "\nSorted (Ascending Order) Array elements:" << endl; for (i = 0; i < n; i++) cout << arr[i] << "\t"; cout << endl; return 0; }
결과
www.includehelp.com/cpp-programs/sort-an-array-in-ascending-order.aspx
C++ program to sort an array in Ascending Order - IncludeHelp
Home » C++ programs » C++ Most popular & searched programs C++ program to sort an array in Ascending Order In this program, we will learn how to sort integer array numbers/elements in Ascending Order in C++? This program will read total number of element
www.includehelp.com
반응형'코딩Coding > C++_연습' 카테고리의 다른 글
C++연습_숫자를 영어로 출력하기 (0) 2020.12.20 C++연습_내가 입력한 숫자가 홀수(odd)나 짝수(even)인지 구별 하여 출력하기 (0) 2020.12.20 C++연습_문자열(내가 친 단어를(이름5개)) 순서대로 정렬하여 출력하기_최종본 (0) 2020.12.20 C++연습_문자열(내가 친 단어를) 순서대로 정렬하여 출력하기 (0) 2020.12.20 C++연습) Array input and output입력받고 입력받은 값 출력하기(DevC++돌림,VisualStudio에러남 원인모르겠음) (0) 2020.12.01 C++) 난수생성 후 내림차순 정렬(Descending), 오름차순 정렬(Ascending) (0) 2020.11.30 C++연습)마일을 킬로미터로 변환 (0) 2020.11.29 2D Transformation : Scaling in C++ (0) 2020.11.28