Home > Back-end >  I need to extract the lower and higher number of sequence
I need to extract the lower and higher number of sequence

Time:10-14

Im using a sequence of x numbers and search the lower and higher number of it and calculate the diff, but when i use a variable to save the highest number it dont save, I dont have that problem with the lower variable.

`  for(int i=0;i<mesos;i  ){
    cin >>m;
    if(m<e) a = m;
    else if(m<e) e = m;

all is declared

CodePudding user response:

To save the highest number you have to test if the number is greater than another number. In your case you're calculating against the same value, the same thing. I didn't test my resolution but it would be something like what I wrote bellow. I used an if in the first time the loop runs so the highest and the lowest value are equal your input.

But test also gonewiththewhind answer, and see which is best for you.

Tip: use good variable names

int lower,higher;
for(int i=0;i<mesos;i  ){ 
    cin<<m;
    if(i==0){ 
        lower = m; 
        higher = m;
    }
    if(m>higher) higher = m;
    else if(m<lower) lower = m;
}

Best regards

CodePudding user response:

Just for elegance as this is a C question.

uint64_t min, max;
for(decltype(mesos) i = 0; i < mesos;   i) {
  std::cin >> m;
  min = std::min(min, m);
  max = std::max(max, m);
}

CodePudding user response:

U can simply do this :

` x[which(x) == min(x)] - x[which(x) == max(x)]






Such it is declared.

  • Related