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;
}
schorsch312
  • 5,553
  • 5
  • 28
  • 57
Kerox
  • 590
  • 1
  • 8
  • 14
  • 1
    What makes you think we can guess what some unknown website is doing with your code? – Jabberwocky Sep 20 '18 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`? – Cory Kramer Sep 20 '18 at 11:37
  • 1
    Please, be nice to others and format your code properly before submitting it. – Acorn Sep 20 '18 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. – Frederik De Ruyck Sep 20 '18 at 11:44
  • 2
    Have a look at [`std::minmax_element`](https://en.cppreference.com/w/cpp/algorithm/minmax_element) – Caleth Sep 20 '18 at 11:46
  • @FrederikDeRuyck no,the website doesn't offer feedback. – Kerox Sep 20 '18 at 12:11
  • @CoryKramer the problem says that the array has N elements,so if I enter 5 as N then I need to enter 5 more numbers that I declared as x variable. – Kerox Sep 20 '18 at 12:13
  • @Caleth your suggestion is good if the OP would have to insert the user inputs to a container and it has ranges(first and last), and then need to find min-max. Here its not needed. – JeJo Sep 20 '18 at 12:14

3 Answers3

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?

JeJo
  • 30,635
  • 6
  • 49
  • 88
  • I'm new to programming and it was supposed that I solve the problem using loops only...Thank you,it works! – Kerox Sep 20 '18 at 12:16
  • yeah but I haven't used other libraries. – Kerox Sep 20 '18 at 12:22
  • [](https://en.cppreference.com/w/cpp/header/limits) and [](https://en.cppreference.com/w/cpp/header/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++](https://stackoverflow.com/questions/32649032/what-does-whilex-mean-in-c/32649084) – JeJo Sep 20 '18 at 12:26
  • 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 Sep 20 '18 at 12:46
  • @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. – JeJo Sep 20 '18 at 12:56
  • thank you very much,it worked. Now I'm curious what else I could replace min and max with,I tried with 0 but it didn't work,do I need another variable? – Kerox Sep 20 '18 at 13:12
  • @Kerox No you cannot provide `0`, same for the reason I explained above. If you want to have an alternate solution(which I won't recommend to use in your case, still good to know) see [std::minmax_element](https://en.cppreference.com/w/cpp/algorithm/minmax_element) and [here you will find a sample solution with that](https://ideone.com/X2syUN) – JeJo Sep 20 '18 at 13:20
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;
}
Kumar
  • 1,882
  • 2
  • 27
  • 44
  • 2
    You can assign `min` and `max` to the first value of the input.This way you can avoid a header file to be included. – nice_dev Sep 20 '18 at 16:18
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;
}