# Error with fixes

3

Hello everyone, is that I try to solve a problem with minor and major fixes and make their difference but ... the program defines well the major element but not the minor so is the arrangement

2 5 3 7 12 9 8 5 4 10

Define the biggest one that in this case is 12

The minor defines it badly that it prints 10 and it is supposed to be 2

It is the following code

`````` #include<iostream>
#define MAX 10002
using namespace std;

int arr[MAX];
int mayor=arr[0], menor=arr[0];
int diferencia=0;

int main()
{
for(int i=1; i<=10; i++)
{
cin>>arr[i];

if(arr[i]>mayor)
{
mayor=arr[i];
}

if(arr[i]<menor);
{
menor=arr[i];
}
}
cout<<menor<<" "<<mayor;
}
``````

If someone can help me, I'd really appreciate it

asked by carlos 29.09.2018 в 20:04
source

3

Let's go in parts:

``````int arr[MAX];
int mayor=arr[0], menor=arr[0];
``````

At this time, the content of `arr[0]` is unknown. It can be anything, because you have not initialized the array.

Since we do not know that value, it turns out that `mayor` is already greater than any of the elements you put (and would not change in the iterations). O `menor` is less than any of your elements (and would not change in iterations).

For example, if `menor` starts with 0, none of the elements in your example would replace it. `menor` has to be so high that you are sure that you will find a lower value, `mayor` so low that you are sure that you will find a higher value.

``````int diferencia=0;
int main() {
for(int i=1; i<=10; i++)
{
cin>>arr[i];
``````

If you do not need to save the data, you really do not need an array. If you make `int numero; cin >> numero;` it works exactly the same.

Naturally, in the following lines you would have to change `arr[i]` by `numero` .

``````    if(arr[i]>mayor) {
mayor=arr[i];
}
if(arr[i]<menor);
``````

You have a `;` , so the `if` ends on the top line and the block below always runs.

``````    {
menor=arr[i];
}
}
cout<<menor<<" "<<mayor;
``````

The impression is complicated because it is confused with the data of the last line. I think that's part of the problem; it's much better: `cout << endl << "Menor :" << menor << endl << "Mayor: " << mayor << endl;`

``````}
``````

3

When you start to program it is very normal that some details escape but once you have time you notice certain details, your mistake is that you put a `;` where you should not go.

``````  #include<iostream>
#define MAX 10002
using namespace std;

int arr[MAX];
int mayor=arr[0], menor=arr[0];
int diferencia=0;

int main()
{
for(int i=1; i<=10; i++)
{
cin>>arr[i];

if(arr[i]>mayor)
{
mayor=arr[i];
}

if(arr[i]<menor); <---- precisamente aquí
{
menor=arr[i];
}
}
cout<<menor<<" "<<mayor;
}
``````

0

Basically the program has two errors. One has already been indicated and it is that you have cast a `;` after a `if` :

``````if(arr[i]<menor);
//              ^
``````

That semicolon causes the `if` to do absolutely nothing ... silly language details. To see it more clearly we could replace that semicolon with two keys:

``````if(arr[i]<menor){ }
{
menor = arr[i];
}
``````

Now it looks better than that semicolon overrides the conditional.

On the other hand the initialization of `menor` is not correct.

Global arrays are initialized automatically, not those that belong to a function. That is:

``````int arr[MAX]; // Todos sus elementos valen 0
// Pero únicamente porque es variable global!!!

int main()
{
int arr2[MAX]; // Array no inicializado!!!
}
``````

Well, the next thing you do after declaring the fix is to declare and initialize `mayor` and `menor` :

``````int mayor=arr[0], menor=arr[0];
``````

And here the problems begin ... this initialization makes `mayor=menor=0` . If we subsequently give values to `arr[0]` , the value of `mayor` and `menor` will not change automatically. Your starting value will be always 0.

This initialization poses a problem and is that for `menor` find the smallest number, it must be equal to or less than 0. Otherwise the program will not work.

To avoid this mess of minimums and maximums I would use pointers:

``````int* mayor = arr;
int* menor = arr;

for(int i=0; i<MAX; i++)
{
cin>>arr[i];

if(arr[i] > *mayor)
{
mayor = &arr[i];
}

if(arr[i] < *menor)
{
menor = &arr[i];
}
}

cout << *menor << ' ' << *mayor;
``````

This solution uses pointers instead of variables. Thus, `menor` does not store a value of its own, but will point to the smallest element (the same for `mayor` ).

If using pointers is not a possibility, then you should pay special attention to the initialization of `menor` and `mayor` . Pulling the standard the best is to use `std::numeric_limits` :

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

What we do here is initialize `menor` with the largest number that can be stored in a `int` , in this way any number that the user enters will be equal or smaller and, consequently, the algorithm will be able to detect it as smaller. In `mayor` , instead, we store the smallest number possible, so that any number that the user enters is equal or larger.