Function that checks whether a vector is in ascending order

Asked

Viewed 5,610 times

2

I’m doing an exercise that says:

Critique the code of 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 seems to be right. There is something wrong in the function?

Here to main() that I made 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;
}

3 answers

2


Yes, the code is right, but it is not readable, it is very easy to think that he 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 spaces make a difference in readability. And good names avoid comments.

Under normal conditions or before n should never be less than 1, the if only makes sense if you have error in the argument. And even if that comes, check and not check this way gives in the same. If it would be the case it exists if it is 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");
}

Behold working in the ideone. And in the repl it.. Also put on the 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");
}

0

The correct code is this:

#include <iostream>

using namespace std;

int levetor(int v[],int &n){       //função lê vetor

for(int i=0;i<n;i++)

cin>>v[i];
}

bool verificacrescente(int v[],int n){

if(n>1){

    int cont=1;

    for(int i=1;i<n;i++){

        if(v[i-1]<v[i]){

            cont++;

        }else{

            return 1;
            }
        }
   }
        if(n==cont){

            return 0;
        }
}
int main(){

 int n1;

 cin>>n1;

 int v1[n1];

 levetor(v1,n1);

if (verificacrescente (v1,n1)==false)

    cout<<"Vetor esta ordenado";

if (verificacrescente (v1,n1)==true)

    cout<<"Vetor nao esta ordenado";

 return 0;
}

-1

Your code is semi-complete, it does exactly what it should do, the problem is the Return you put, so that it compares the first 2 numbers it finishes the function as true. The right thing would be 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;
   }
}

This way he accumulates the "hits" in k, that is, if the previous value is even higher than the next one... And so until he reaches the last term n.

If it reaches the last term n and equals k, it means that it has gone through the entire list without returning 1, then the list is all increasing. If there were any element out of order, it would fall into Return 1 and it would not be a growing list.

  • No, it doesn’t stop after the first two... I tested it with something like {1, 2, 3, 8, 4} and it really returns that it’s not ordered. Maybe I was a little confused by being without { no for and if (the function is not mine, it was given in the exercise, so I’m not understanding where the error is)

  • tested my code too? Return to the function where it is and returns what was requested.

Browser other questions tagged

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