1

I need to find min and max of an array with N elements. The fact is that my program is working but when I submit it on a website it gives me only 32 points out of 100 and I don't know what's wrong.

#include <iostream>

using namespace std;

int main() {
    int N,min,max;
    cin >> N;
    min = N;
    max = N;

    int i,x;
    for (i = 1; i <= N; ++i) {
        cin >> x;

        if ( x < min ) {
            min = x;
        }
        if (x > max) {
            max = x;
        }
    }
    cout << min <<" "<< max;
    return 0;
}
8
  • 1
    What makes you think we can guess what some unknown website is doing with your code? Commented Sep 20, 2018 at 11:37
  • 7
    Why do you initialize min and max to N? What if I enter N = 20 elements, but the largest value is only x=6? Commented Sep 20, 2018 at 11:37
  • 1
    Please, be nice to others and format your code properly before submitting it. Commented Sep 20, 2018 at 11:42
  • You get 32/100 score for you solution on some website. Can't you just read the feedback you get? We can not see the original assignment nor can we tell the criteria which is used to calculate your score. Commented Sep 20, 2018 at 11:44
  • 2
    Have a look at std::minmax_element Commented Sep 20, 2018 at 11:46

3 Answers 3

6

Your logic here

min = N;
max = N;

initializing them with N, is wrong. When you have the minimum number for example 0 in your user input, and your N is greater than that 0, you never find your minimum. The same will happen for the maximum.

Initialize min with largest possible value of int and max with least possible value, like the following:

int min = std::numeric_limits<int>::max();
int max = std::numeric_limits<int>::min();

Suggestion - 1

As it looks like you do not want to save the user input to find mim and max you can use std::min and std::max functions as follows:

#include <iostream>
#include <limits>    //  std::numeric_limits<>
#include <algorithm> //  std::min, std::max

int main()
{
    // initialize like this
    int min = std::numeric_limits<int>::max();
    int max = std::numeric_limits<int>::min();
    int N;
    std::cin >> N;
    while (N--)
    {
        int x; std::cin >> x;
        min = std::min(x, min);  // use std::min
        max = std::max(x, max);  // use std::max
    }
    std::cout << min << " " << max;
    return 0;
}

Suggestion - 2

If you want to find the min-max of an already existing array, you might wanna consider use std::minmax_element instead.

#include <algorithm>   //  std::minmax_element
#include <iostream>
#include <vector>

int main()
{
    int N; std::cin >> N;
    std::vector<int> v(N);
    for(auto& element: v) std::cin >> element;
    // do something.....

    // to find min-max of the array
    auto result = std::minmax_element(v.begin(), v.end());
    std::cout << "min element is: " << *result.first << '\n';
    std::cout << "max element is: " << *result.second << '\n';
}

Side Note: Do not practice with std namespüace std;, why? see this post: Why is “using namespace std” considered bad practice?

Sign up to request clarification or add additional context in comments.

7 Comments

I'm new to programming and it was supposed that I solve the problem using loops only...Thank you,it works!
yeah but I haven't used other libraries.
<limits> and <algorithm> are part of std C++ library which are shipped with C++ compilers. It is always good to know what stdands provids, before you make your own implimentations. :) Regarding loop, see this post: What does while(x--) mean in C++
so I can't solve the problem using for and if only like how I tried? It's weird because in CodeBlocks it works...
@Kerox Of course, you can. I think my wording made you confusing. Sorry about that. :( If you correct for your initialization of min and max like I mentioned, you are good to go. Never mind the suggestion, I recommented. I wanted to emphasize that, there are some functions avalable in C++ std library, for such basic algorithmic purpose and we should be knowing them beforehand.
|
2

As suggested by vivek_23, Use first element as min and max:

#include <iostream>

using namespace std;

int main()
{
    int N,min,max;
    cin >> N;
    cin>>min;
    max = min; 

    int i,x;
    for (i = 1; i < N; ++i){
        cin >> x;

        if ( x < min ){
            min = x;
        }
        if (x > max){
            max = x;
        }
    }
    cout << min <<" "<< max;
    return 0;
}

Alternative solution: Add extra headers and use int max and min limits

#include <iostream>
#include <cmath>
#include <climits>

using namespace std;

int main()
{
    int N,min,max;
    cin >> N;
    min = INT_MAX; //take largest value
    max = INT_MIN; //take smallest value

    int i,x;
    for (i = 1; i <= N; ++i){
        cin >> x;

        if ( x < min ){
            min = x;
        }
        if (x > max){
            max = x;
        }
    }
    cout << min <<" "<< max;
    return 0;
}

1 Comment

You can assign min and max to the first value of the input.This way you can avoid a header file to be included.
1
#include <iostream>
using namespace std;
    int max(int arr[], int n ){
        int max ,i;
        max=arr[0];
        for(i =0;i<n;i++){
         if(arr[i]>max){
             max = arr[i];
         }
            }
            return max;
    }
    int min(int arr[] , int n){
      int min , i ;
      min = arr[0];
      for(i=0;i<n;i++){
      if(min > arr[i]){
      
      min = arr[i];
      
      
      }
      
      
      }
      return min;
    }

int main(){
    int arr[] = { 5 , 2 ,3 ,9 , 8 , 11 , 25 ,1};
    int n= 8;
    int maxval = max(arr , n);
    cout<< "The max element is " << maxval << endl;
    int minval = min(arr , n);
    cout<< "The min value is " << minval;
    
    
    
    
  return 0;
}

Comments

Your Answer

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.