Use vector on functions outside main

Asked

Viewed 56 times

0

I learned to use functions a few days ago and now I wanted to use an array on them. My teacher taught, but my code just doesn’t work. One of the numbers ends up being repeated.Follow the code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <locale.h>


void crescente(int vetor[],int n);//protótipo função
void decrescente(int vetor[],int n);//protótipo função

int main()
{

    setlocale(NULL,"");
    int vet[10],i,cod=3;


    printf("Digite dez números inteiros (aperte a tecla ENTER ao final de cada número)!\n");
    for(i=0;i<=9;i++)
        scanf("%d%*c",&vet[i]);

    system("cls");

   while(cod!=0)
   {


   printf("Escolha um código :\n1-Organizar em ordem crescente.\n2-Organizar em ordem decrescente.\n0-Finalizar o programa.\n\n");
   cod=getch()-48;

      if(cod==0)
           break;

      else if(cod==1)
           crescente(&vet,10);

      else if(cod==2)
           decrescente(vet,10);




   }
    return 0;
}


void crescente(int vetor[],int n)
{

int aux=0,control,i;

  do{
         control=0;

        for(i=0;i<=n-1;i++)
        {
            if(vetor[i]>vetor[i+1])
            {
                aux=vetor[i];
                vetor[i]=vetor[i+1];
                vetor[i+1]=aux;

                control=1;//controle do bubble sort que vi na internet,melhor q um FOR; kkkkkkk
            }
        }



    }while(control!=0);

    for(i=0;i<=n-1;i++)
        {
            printf("%d. ",*vetor[i]);

        }
     printf("\n\n\n");
}


void decrescente(int vetor[],int n)
{

int aux,control,i;

  do{
        control=0;

        for(i=0;i<=n-1;i++)
        {
            if(vetor[i]<vetor[i+1])
            {
                aux=vetor[i];
                vetor[i]=vetor[i+1];
                vetor[i+1]=aux;

                control=1;//controle do bubble sort que vi na internet,melhor q um FOR; kkkkkkk
            }
        }





    }while(control!=0);


    for(i=0;i<=n-1;i++)
        {
            printf("%d. ",vetor[i]);

        }
     printf("\n\n\n");
}

1 answer

2

You have:

crescente(&vet,10);

And:

decrescente(vet,10);

Notice that one is with the & and the other not. The right thing would be for either of you to have that. After that, in function crescente you should have:

printf("%d. ", vetor[i]);

Instead of:

printf("%d. ",*vetor[i]);

I mean, take that *. Note that the function decrescente is right at this point.

  • Thanks for the warning,I messed up the code too much trying to fix it and forgot a few points, but when fixing the code it still doesn’t work,).

Browser other questions tagged

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