Ashish Kushwaha
Ashish Kushwaha

Reputation: 15

What should be the proper declaration of array while finding the largest number in array?

C++ This is my code in C++ for finding the largest number in array. When I was running in my IDE then there was no compilation error but it was not giving me output. I think the problem is in the declaration of array at line 8. I replaced the array declaration from line 8 to line 11 then it is working fine in my IDE. So I didn't get it that why the declaration of array was not working at line 8?

#include <bits/stdc++.h>
using namespace std;
int largest_in_array(int a[], int n);

int main() // main function
{
    int n; // User will enter the size of array 
    int arr[n]; // Line 8
    cout << "Enter the size of array: " << endl;
    cin >> n;
              // Line 11
    cout << "\nEnter the elements of array: " << endl;

    for (int i = 0; i < n; i++) // This loop will run for each element of array that user wants to enter
    {
        cout << "Enter the " << (i + 1) << " element:";
        cin >> arr[i];
        cout << endl;
    }
    cout << "Elements are: [";
    for (int i = 0; i < n; i++) // Prints the elements of array
    {
        // cout << "Enter the " << (i + 1) << " element:";
        cout << arr[i] << " ";
        // cout << endl;
    }
    cout << "]";

    int res = largest_in_array(arr, n); //Function call
    cout << "\nLargest element in array is: " << arr[res] << endl;
    return 0;
}

int largest_in_array(int a[], int n) // function that will return the index of largest element in array
{
    int max = 0;
    for (int i = 1; i < n; i++)
    {
        if (a[max] < a[i])
        {
            max = i;
        }
    }
    return max;
} 

Upvotes: 1

Views: 122

Answers (2)

Ted Lyngmo
Ted Lyngmo

Reputation: 117298

  • You declare int arr[n]; before the user has entered a value into n. n has an indeterminate value when you read it and create arr.
  • You don't check that the user enters a positive value into n. Zero and negative sized arrays are not valid.

Other points:

  • bits/stdc++.h is not a standard header which makes your program not portable. Use the proper header files, like iostream etc.
  • arr[n] is a Variable Length Array (VLA) which is not part of standard C++. Make it a std::vector<int> arr(n); instead.
  • The use of std::endl is unnessesary. There is no need to flush the output streams here. Use \n instead.

Example:

#include <iostream>
#include <limits>
#include <vector>

int largest_in_array(const std::vector<int>& a) {
    int max = 0;
    for(int i = 1; i < a.size(); i++) {
        if(a[max] < a[i]) {
            max = i;
        }
    }
    return max;
}

int main() // main function
{
    int n; // User will enter the size of array
    std::cout << "Enter the size of array:\n";

    // check that input succeeds and that the value is valid
    if(!(std::cin >> n) || n < 1) return 1;

    std::vector<int> arr(n);
    std::cout << "\nEnter the elements of array:\n";

    for(int i = 0; i < n; i++)
    {
        std::cout << "Enter the " << (i + 1) << " element:";
        if(!(std::cin >> arr[i])) {
            std::cout << "invalid input, bye bye\n";
            return 1;
        }
    }
    std::cout << "Elements are: [";
    for(int i = 0; i < n; i++)
    {
        std::cout << arr[i] << " ";
    }
    std::cout << "]";

    int res = largest_in_array(arr); // Function call
    std::cout << "\nLargest element in array is: " << arr[res] << '\n';
}

That said, you could however use the standard algorithm std::max_element instead of writing your own. It returns an iterator to the maximum element. You could also make use of range-based for loop when you don't need to know the index in the array, as in your second loop.

Example:

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <iterator>
#include <limits>
#include <vector>

int main() {
    int n; // User will enter the size of array
    std::cout << "Enter the size of array:\n";
    if(!(std::cin >> n) || n < 1) return 1;

    std::vector<int> arr(n);
    std::cout << "\nEnter the elements of array:\n";

    for(int i = 0; i < n; i++) // This loop will run for each element of
                               // array that user wants to enter
    {
        std::cout << "Enter the " << (i + 1) << " element:";
        if(!(std::cin >> arr[i])) {
            std::cout << "invalid input, bye bye\n";
            return 1;
        }
    }
    std::cout << "Elements are: [";
    for(auto value : arr) {          // a range-based for loop
        std::cout << value << ' ';
    }
    std::cout << "]\n";

    auto res = std::max_element(arr.begin(), arr.end());
    std::cout << "Largest element in array is: " << *res << '\n';

    std::size_t index = std::distance(arr.begin(), res);
    std::cout << "which has index " << index << '\n';
}

Upvotes: 2

Mureinik
Mureinik

Reputation: 311393

When you have int n on line 8 it is initialized when you use it to create the array. When n is initialized explicitly, it's value is undefined behavior. You may be creating an array larger than the n you inputted on line 10, resulting in the array having extra random junk, it may be smaller meaning your program read memory it really shouldn't, etc.

Upvotes: 0

Related Questions