-
/*
-
****************************************************************************
-
File: FindMax2.cpp
-
Author: birthis
-
Email:
-
Date: 09/01/2011
-
Description: Chuong trinh tim so lon thu 2 trong mang nhap vao
-
****************************************************************************
-
*/
-
#include <stdio.h>
-
#include <conio.h>
-
-
int
getMax2
(
int
a
[
]
,
int
n
)
;
-
-
int
main
(
)
-
{
-
int
a
[
100
]
,
n
;
-
-
printf
(
“
\n
Chuong trinh tim so lon thu 2 trong mang.”
)
;
-
//Nhap du lieu
-
printf
(
“
\n
Nhap vao n: “
)
;
-
fflush
(
stdin
)
;
stdin
-
scanf
(
“%d”
,&
n
)
;
-
-
printf
(
“
\n
Nhap mang:”
)
;
-
for
(
int
i
=
0
;
i
<
n
;
i
++
)
-
{
-
printf
(
“
\n
a[%d]= “
,
i
)
;
-
fflush
(
stdin
)
;
stdin
-
scanf
(
“%d”
,
a
+
i
)
;
-
}
-
-
//In ket qua
-
printf
(
“
\n
Mang vua nhap: “
)
;
-
for
(
int
i
=
0
;
i
<
n
;
i
++
)
-
{
-
printf
(
“%d “
,
a
[
i
]
)
;
-
}
-
-
//Gia tri lon thu 2
-
printf
(
“
\n
Gia tri lon thu 2: %d”
,
getMax2
(
a
,
n
)
)
;
getMax2
-
-
fflush
(
stdin
)
;
stdin
-
getch
(
)
;
-
-
}
-
/*
-
Ten ham : getMax2
-
Muc dich: Tim phan tu lon thu 2 cua mang
-
Dau vao : – Mang so nguyen a
-
– So luong phan tu n cua mang a
-
Dau ra : – Tra ve max2
-
*/
-
int
getMax2
(
int
a
[
]
,
int
n
)
-
{
-
int
max1
,
max2
;
-
if
(
n
<=
0
)
-
{
-
printf
(
“
\n
Lam gi co phan tu nao dau ma tim ^^”
)
;
-
return
0
;
-
}
-
else
if
(
n
==
1
)
-
{
-
printf
(
“
\n
Khong co max2 chi co max1 ^^”
)
;
-
return
0
;
-
}
-
else
-
{
-
max1
=
max2
=
0x80000000
;
-
for
(
int
i
=
0
;
i
<
n
;
i
++
)
-
{
-
if
(
max1
<
a
[
i
]
)
-
{
-
max2
=
max1
;
-
max1
=
a
[
i
]
;
-
}
-
else
if
(
max2
<
a
[
i
]
&&
a
[
i
]
<
max1
)
-
{
-
max2
=
a
[
i
]
;
-
}
-
}
-
return
max2
;
-
}
-
-
}