c++에서 최댓값의 위치 출력하기
조회수 664회
include
using namespace std;
int main()
{
int x;
int max = 0;
cout << "Enter 10 numbers" << endl;
for (int i = 0; i < 10; i++)
{
cin >> x;
if (x > max)
{
max = x;
}
}
cout << "The max: " << max << endl;
cout << "The location max: " << << endl;
} 다음과 같은 코드에서 포인터와 배열을 사용하지 않고 최댓값의 위치를 출력하고 싶습니다 ex) 10개의 정수를 넣었을 때 4번째인 100이제일 컸다고 가정하면 3이 출력되도록 하고 싶습니다. index를 사용하는 방법이 있다고들었는데 어떻게 해야할지 잘모르겠습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
1 답변
-
아래 코드 참고하세요.
- 코드
#include <iostream> using namespace std; int main() { int x; int max = 0; int max_index; cout << "Enter 10 numbers" << endl; for (int i = 0; i < 10; i++) { cin >> x; if (x > max) { max = x; max_index = i; } } cout << "The max: " << max << endl; cout << "The location max: " << max_index << endl; return 0; }
- 결과
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력