Return minimum and maximum values of a vector

Asked

Viewed 136 times

0

Encode the function minimax(v,n,a,b), that receives a vector v containing n real numbers and returns in a and b, respectively, the values minimum and maximum between those stored in v.

The code I made was this, but I’m not understanding why a and the b are receiving garbage.

#include <stdio.h>

#include <time.h>

#include <stdlib.h>


void minimax (int *v,int *a,int *b){

  int i;

  for(i=0;i<10;i++){

    if (*a<v[i])

      *a=v[i];


    if (*b>v[i])

      *b=v[i];

  }

}


int main(void) {

  int v[10];

  int i,a=v[0],b=v[0];

  srand(time(NULL));


  for (i=0;i<10;i++){

    v[i]=rand()%100;

    printf("%d  ",v[i]);

  }


  minimax (&v[10],&a,&b);

  printf("\nOs valores minimo e maximo sao: %d e %d",a,b);


  return 0;

}
  • You have to do the assignments a=v[0] and b=v[0] after read the array v. At the point where you put you are assigning memory junk.

  • What do you mean? I didn’t understand what it would look like

  • You did not initialize the variables a and b, so the crazy result. Before the for of its function place a=v[0] e b=v[0]

1 answer

0

Your code has two errors. Note that when you call your function you pass 3 addresses as arguments, so far so good, but the subtle detail is that you are passing the address of the vector:

minimax (&v[10],&a,&b);

Since the address is the top position then when the loop goes through the spaces in memory will not be the values stored in the vector, but rather the memory addresses that are located after the vector. An example

// Ao invés de fazer isso
a = v[2]
b = [6] 

// O código faz isso
a = v[12]
b = v[16]

These addresses aren’t part of your array, so you don’t change what’s in that memory space, so there’s trash there. To solve you need to pass the address of the first element of the vector, thus:

minimax (v,&a,&b); // Pode fazer assim
minimax (&v[0],&a,&b); // Ou assim

A vector without the brackets by definition points to its first position, i.e., &v[0] == v

The second error of your code is initializing the variables a and b equaling at the zero position of the vector. Note that the vector at the time of initialization of the variables has junk memory at position [0] and therefore you do not know which value will be assigned to the variables. So when you make that comparison:

if (*a<v[i])

In the above comparison the variable "a" has a memory junk, so the value inside can be anything, examples:

if(989++9 < 100) // Variável a
if(-9868768 < 48) // Variável a
if(-986786 > 0) // Variável b
if(0xF756746 > 9) // Variável b

Because of the situation I exemplified is that shows the memory junk, most of the time the memory junk will be out of values between 0 and 100 and so the if will hardly be activated. The solution to the problem is quite simple, just do this:

int i, a = 0,b = 100;

As "a" holds the highest number then it starts with zero, and "b" equals 100. I recommend you make the changes and then do the table test to better understand why a be equal to zero and b equal to 100. The table test will be more efficient than I try to answer, I will probably complicate a little kkk.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.