Function that checks if a vector is in ascending order

2

I'm doing an exercise that says:

  

Critique the code for the following function, which promises to decide whether the vector   v [0..n-1] is in ascending order.

int verifica (int v[], int n) {     //n é o tamanho do vetor
if (n > 1) 
   for (int i = 1; i < n; i++) 
      if (v[i-1] > v[i]) 
         return 0;     
      return 1; 
 }

I have tested with several cases and it seems to be right. Is there something wrong with the function?

Here's how much% I've done to test:

int main(){

    int vetor[5] = {-1, 2, 3, 4, 5}; //pode ser mudado para ler os valores

    if(verifica(vetor, 5)){
        printf("Esta em ordem crescente");
    } else{
        printf("Nao esta em ordem crescente");
    }

    return 0;
}
    
asked by anonymous 06.10.2018 / 03:36

2 answers

1

Yes, the code is right, but it is not readable, it is very easy to think that it does one thing and does another. Note that the code is the same, but it is simpler and more readable in the compact version and more explicit in the organization of the blocks. Even the spaces make a difference in readability. And good names avoid comments.

Normally, before% w / o% should never be less than 1,% w / o% only makes sense if you have an error in the argument. And even if that comes, check and do not check this way gives the same. If it would be the case it would exist if it were to give invalid argument error.

#include <stdio.h>

int EstaOrdemCrescente(int vetor[], int tamanho) {
    for (int i = 1; i < tamanho; i++) if (vetor[i - 1] > vetor[i]) return 0;
    return 1; 
 }

 int main() {
     printf(EstaOrdemCrescente((int[]){-1, 2, 3, 4, 5}, 5) ? "Esta em ordem crescente\n" : "Nao esta em ordem crescente\n");
     printf(EstaOrdemCrescente((int[]){-1, 2, 0, 4, 5}, 5) ? "Esta em ordem crescente\n" : "Nao esta em ordem crescente\n");
     printf(EstaOrdemCrescente((int[]){1}, 0) ? "Esta em ordem crescente\n" : "Nao esta em ordem crescente\n");
}

See running on ideone . And no Coding Ground . Also I put it in GitHub for future reference .

#include <stdio.h>

int EstaOrdemCrescente(int vetor[], int tamanho) {
    for (int i = 1; i < tamanho; i++) {
        if (vetor[i - 1] > vetor[i]) {
            return 0;
        }
    }
    return 1; 
 }

 int main() {
     printf(EstaOrdemCrescente((int[]){-1, 2, 3, 4, 5}, 5) ? "Esta em ordem crescente\n" : "Nao esta em ordem crescente\n");
     printf(EstaOrdemCrescente((int[]){-1, 2, 0, 4, 5}, 5) ? "Esta em ordem crescente\n" : "Nao esta em ordem crescente\n");
     printf(EstaOrdemCrescente((int[]){1}, 0) ? "Esta em ordem crescente\n" : "Nao esta em ordem crescente\n");
}
    
07.10.2018 / 14:33
-1

Your code is semi-complete, it does exactly what it should do, the problem is the return you put, so it compares the first 2 numbers it ends the function to true. The correct one would look something like:

int verifica (int v[], int n) {
   int k = 1;
   if (n > 1){
      for (int i = 1; i < n; i++){
         if (v[i-1] > v[i]){
            k++;
         }
         else{
            return 1;
         }
      }
   }
   if (k == n){
       return 0;
   }
}

In this way he accumulates the "hits" in k, that is, if the previous value is even bigger than the next one ... And so on until the last term n.

If it reaches the last term n and equals k, it means that it ran through the entire list without returning 1, so the list is all growing. If there were any out of order elements, it would fall into return 1 and would not be a growing list.

    
06.10.2018 / 03:56