Home > Software design >  Getting Error for the following code, Problem:- print maximum and minimum element from array
Getting Error for the following code, Problem:- print maximum and minimum element from array

Time:10-17

I am getting the following error in c code error:

'getmin' was not declared in this scope

 cout<<"Minimum value is"<<getmin(num, size)<<endl;

Code//

Int getmin(int num[], int n){
    int min= INT32_MAX;

    for(int i=0;i<n;i  ){
        if(num[i] < min){
             min=num[I];
        }
    }
    return min;
}

int getmax(int num[], int n){
    int max= INT32_MIN;

    for(int i=0;i<n;i  ){
        if(num[i]>max){
            max=num[I];
        }
    }
    return max;
}

CodePudding user response:

The problem you made is that you passed two integers inside the getmin(array[], integer) whereas you are supposed to pass an array and an integer. Since you passed integer and integer, the program search for a function with signature getmin(integer, integer) when you call the function in main(). But the function is inexistent; thus get the error'getmin' was not declared in this scope

Solved:

int getmin(int num[], int n)
{ 
    int min= INT32_MAX;
    for(int i=0;i<n;i  ){
        if(num[i] < min){
            min=num[i];
        }
    }
    return min;
}

int main()
{
    int a[6]={2,7,4,0,9,10};
    cout<<getmin(a, 6);
    return 0;
}

CodePudding user response:

Not sure that you've prototyped your function getmin(). If not, your every function should be declared before it is used.

int getmin(int num[], int  n);

That is before int main()

Here's my modified code,

#include <iostream>
using namespace std;
int getmin(int num[], int  n);
int getmax(int num[], int n);

int main()
{
    int num[10] = {10,20,30,40,50,60,40,5};
    int n = 7;
    cout << "Minimum value is:" << getmin(num, n) << endl;
    cout << "Minimum value is: " << getmax(num, n) << endl;
}

int getmin(int num[], int n)
{
    int min = INT32_MAX;
    for (int i = 0; i <= n; i  )
    {
        if (num[i] < min) 
        {
            
            min = num[i];
        }
    }
    return min;
}

int getmax(int num[], int n) {
    int max = INT32_MIN;

    for (int i = 0; i < n; i  ) {
        if (num[i] >= max) {
            max = num[i];
        }
    }
    return max;
}
  • Related