Created
December 21, 2018 13:34
-
-
Save rkhang7/721e8e6c0e7e5cddaf97ab86ffa3e623 to your computer and use it in GitHub Desktop.
Sử dụng lại bài 29. Viết chương trình đảo ngược vị trí của giá max và min bên trong mảng một chiều. Thứ tự của các phần tử khác là giữ nguyên.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <conio.h> | |
int doivt(int &a,int &b) | |
{ | |
int c; | |
c = a; | |
a = b; | |
b = c; | |
} | |
int main() | |
{ | |
int n; | |
printf("Nhap so phan tu cua mang: ");scanf("%d", &n); | |
int a[n]; | |
for(int i = 0;i<n;i++) | |
{ | |
printf("a[%d]=",i);scanf("%d", &a[i]); | |
} | |
int max = a[0],vtmax; | |
for(int i = 0;i<n;i++) | |
{ | |
if(max <= a[i]) max = a[i]; | |
} | |
for(int i = 0;i<n;i++) | |
{ | |
if(max == a[i])vtmax = i; | |
} | |
int min = a[0],vtmin; | |
for(int i = 0;i<n;i++) | |
{ | |
if(min >=a [i]) min = a[i]; | |
} | |
for(int i = 0;i<n;i++) | |
{ | |
if(min == a[i]) vtmin = i; | |
} | |
doivt(a[vtmin],a[vtmax]); | |
for(int i = 0;i<n;i++) | |
{ | |
printf("%d\t",a[i]); | |
} | |
getch(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment