2

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

asked Aug 31, 2022 at 1:37
3
  • 5
    Arrays in C++ start at 0, not 1. You're iterating to one-past-the-end, which is undefined behavior Commented Aug 31, 2022 at 1:38
  • 5
    Variable length arrays are not standard C++ so this code is not portable. Whitespace is free, you should consider using some. Things like } int max_value = arr[0]; give me a headache. Commented Aug 31, 2022 at 1:41
  • See also std::max_element Commented Aug 31, 2022 at 2:07

3 Answers 3

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]);
}
answered Aug 31, 2022 at 1:58
1
  • 2
    Worth also noting comments 2 and 3 under the main question in your answer. int arr[n]; with int n; isn't valid C++, it's a non-standard extension. Commented Aug 31, 2022 at 2:09
1

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;
answered Aug 31, 2022 at 6:57
1

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];
answered Aug 31, 2022 at 1:40
3
  • 1
    int arr* = new int[n]; statement is not valid, it should be int *arr = new int[n];. Commented 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 like push_back. If you insist on C arrays, use make_unique in its array form. Commented 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. Commented Sep 5, 2022 at 3:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.