Home > Software design >  How to output the maximum value inside an array
How to output the maximum value inside an array

Time:03-13

#include <iostream>

using namespace std;

void value(int array[],int size){
    
    int minimum;
    int maximum;
    
    minimum = array[0];
    for(int x = 0; x < size; x  ){
        if(minimum > array[x 1]){
        minimum = array[x 1];
    }
    }
    maximum = array[0];
    for(int x = 0; x < size; x  ){
        if(maximum < array[x 1]){
        maximum = array[x 1];
    }
    }
    cout << "Minimum Value is: " << minimum << endl;
    cout << "Maximum Value is: " << maximum;
    
}

int main(){
    int size;

    cout << "Number of values you want to input: ";
    cin >> size;
    cout << "Input " << size << " values" << endl;
    
    int array[size];
    
    for(int x = 0; x < size; x  ){
    cout << "Input #" << x 1 <<": ";
    cin >> array[x];
    }
    
    value(array,size);
    
    return 0;

How can I output the maximum value inside the array? whenever I print the value the maximum value always return a number that is not present inside the array but the minimum seems fine, its only the maximum value that I am encountering a problem, I tried every possible answer that I know but it doesn't work, I hope ya'll can help Thank you in advance

CodePudding user response:

for(int x = 0; x < size; x  ){

If you have an array with ten values, size will be 10. If you work out, with paper and pencil, what this for loop does, you will see that it iterates for values of x 0 through 9, that's what this says. x starts with 0. When it reaches 10, x < size will be false and the loop ends, so the loop runs with x ranging from 0 to 9.

        if(minimum > array[x 1]){

Since x will range from 0-9, it logically follows that x 1 will range from 1 to 10, and so this if statement will check the values in array[1] through array[10].

In C array indexes start with 0, not 1. The values in your array are array[0] through array[9]. array[10] does not exist, so the above code is undefined behavior.

Furthermore:

int array[size];

This is not valid C either. Your C compiler may allow this as a non-standard C extension, but array sizes must be fixed, constant sizes in C , determined at compile time. You can't use a non-constant variable to set the size of an array, C does not work this way. If you need to have an array of size that's determined at runtime then you need to use std::vector instead of a plain array, and change the rest of your code accordingly.

CodePudding user response:

Mistake 1

Your example has undefined behavior because of the expression array[x 1]. That is, for the last iteration of the for loop, you're going out of bounds of the array and so have undefined behavior.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.

So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB. The program may just crash.

So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.

Mistake 2

In standard C , the size of an array must be a compile time constant. So in your code:

int size;
cin >> size;
int array[size]; //NOT STANDARD C  

The statement int array[size]; is not standard C because size is not a constant expression.

Additionally you don't need 2 separate for loops when you can achieve the goal in 1 for loop as shown below.

Solution 1

You can use std::vector as shown below:

#include <iostream>
#include <vector>
#include <climits>
 
//this function take a vector as input 
void value(const std::vector<int>& arr)
{    
  int max_num = INT_MIN;
  int min_num = INT_MAX;
  
  //iterate through the vector to find the max and min value 
  for(const int& element: arr)
  {
      if(element >  max_num)
      {
          max_num = element;
      }
      if(element < min_num)
      {
          min_num = element;
      }
  }
  std::cout<<"maximum is: "<<max_num<<std::endl;
  std::cout<<"minimum is: "<<min_num; //return the difference of mx and min value
}
 

int main()
{
  int n;
  std::cout<<"elements: ";
  std::cin >> n;

  //create vector of int of size n 
  std::vector<int>  arr(n);
  
  //take elements from user
  for(int i=0; i<n; i  )
  {
    std::cin >> arr[i];
  }
  
  value(arr);
 
  return 0;
}

Demo

Solution 2

You can make the function a function template so that you don't need to pass a separate argument to the function as shown below:

#include <iostream>
#include <climits>
//N is a nontype template parameter
template<std::size_t N> 
void value(const int (&array)[N]){
    
    int max_num = INT_MIN;
    int min_num = INT_MAX;
  
  //iterate through the array to find the max and min value 
  for(const int& element: array)
  {
      if(element >  max_num)
      {
          max_num = element;
      }
      if(element < min_num)
      {
          min_num = element;
      }
  }
  std::cout<<"maximum is: "<<max_num<<std::endl;
  std::cout<<"minimum is: "<<min_num;
}

int main(){
    int array[3] = {};
    
    for(int x = 0; x < sizeof (array) / (sizeof (array[0])); x  ){
        std::cout << "Input #" << x 1 <<": ";
        std::cin >> array[x];
    }
    
    value(array); //no need to pass the second argument
    
    return 0;
}

Demo


Also note that with C 17, you can use std::size instead of sizeof (array) / (sizeof (array[0])) to find the length of the array.


1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

  • Related