I'm doing a program that finds the max value in a array. I done it but I found a strange bug.
#include<iostream>
using namespace std;
int main() {
int n; //input number of elements in
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i]; //input array's elements
} int max_value = arr[0];
for (int i = 1; i <= n; i++) {
if (arr[i] > max_value) {
max_value = arr[i];
}
} cout << max_value;
return 0;
}
When I put 5 as first line for the number of elements and 2, 7, 6, 8, 9 as the elements of the array. It returns 16 instead of 9. Please help
3 Answers 3
In Arrays the first index starts with 0
and ends in n - 1
assuming the array is of length n
so when looping from i = 1
to i <= n
. n is now larger than n - 1
.
the solution would be to start from 0
and end at i < n
hence:
#include<iostream>
using namespace std;
int main() {
int n; //input number of elements in
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i]; //input array's elements
} int max_value = arr[0];
for (int i = 0; i < n; i++) {
if (arr[i] > max_value) {
max_value = arr[i];
}
}
cout << max_value;
return 0;
}
you could also use the std::max
function like so:
for(int i = 0; i < n; i ++) {
max_value = max(max_value, arr[i]);
}
-
2Worth also noting comments 2 and 3 under the main question in your answer.
int arr[n];
withint n;
isn't valid C++, it's a non-standard extension.David C. Rankin– David C. Rankin2022年08月31日 02:09:07 +00:00Commented Aug 31, 2022 at 2:09
The other posts already pointed out problem in your code.
You should be aware of that int arr[n];
is not permitted in standard C++.
[GCC and CLANG compiler support it in C++ as an extension]
An alternative is to allocate memory dynamically:
int *arr = new int[n];
and to find maximum value you can use std::max_element
:
int max_value = *(std::max_element(arr, arr + n));
Instead of dynamic array, its better to use vector
STL (make yourself familiar with Containers Library). You can do:
std::vector <int> arr;
for (int i = 0; i < n; i++) {
int input;
std::cin >> input;
arr.push_back(input);
}
int max_value = *std::max_element(arr.begin(), arr.end());
std::cout << "Max element is :" << max_value << std::endl;
in your second for do this
for (int i = 1; i < n; i++) {
if (arr[i] > max_value) {
max_value = arr[i];
}
delete '=' from i <= n because i is index which start from 0
and instead of this
int arr[n];
do this
int *arr = new int[n];
-
1
int arr* = new int[n];
statement is not valid, it should beint *arr = new int[n];
.H.S.– H.S.2022年08月31日 06:58:41 +00:00Commented Aug 31, 2022 at 6:58 -
And better use
std::vector
in this case. It can be accessed like an array, if you do not likepush_back
. If you insist on C arrays, usemake_unique
in its array form.Sebastian– Sebastian2022年08月31日 07:00:13 +00:00Commented Aug 31, 2022 at 7:00 -
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.2022年09月05日 03:27:25 +00:00Commented Sep 5, 2022 at 3:27
} int max_value = arr[0];
give me a headache.