I’m not getting a piece of code into a c-function, can you help me please I’m new to this business

Asked

Viewed 35 times

-3

Problem 3:

Make a function that receives, as a parameter, a vector with ten integers and return them ordered in increasing form.

The code I’m using:

#include <stdio.h>

int main(){
    
    int i, troca,j,vetor[10], ordenado[10];
    
    for (i=0;i<10;i++){
    printf("Informe o numero %d de um vetor de 10 numeros",i+1);
    scanf("%d", &vetor[i]);
    }
    for (i=0;i<10;i++){
        for(j=i+1;j<10;j++){
            if(vetor[i] > vetor[j]){
                troca = vetor[i];
                vetor[i] = vetor[j];
                vetor[j] = troca;
            }
        }
    }   
    for (i=0;i<10;i++){
    printf("Posição %d do vetor %d \n",i+1,vetor[i]);
    }
}

The code with the function:

#include <stdio.h>
int converte (int *vetor[10]){
        int i,j,troca;
        for (i=0;i<10;i++){
        for(j=i+1;j<10;j++){
            if(vetor[i] > vetor[j]){
                troca = vetor[i];
                vetor[i] = vetor[j];
                vetor[j] = troca;
            }
        }
    }   
    for (i=0;i<10;i++){
    printf("Posição %d do vetor %d \n",i+1,vetor[i]);
    
}

int main(){
    
    int i, troca,j,vetor[10], ordenado[10];
    
    for (i=0;i<10;i++){
    printf("Informe o numero %d de um vetor de 10 numeros",i+1);
    scanf("%d", &vetor[i]);
    }
    for (i=0;i<10;i++){
        for(j=i+1;j<10;j++){
            if(vetor[i] > vetor[j]){
                troca = vetor[i];
                vetor[i] = vetor[j];
                vetor[j] = troca;
            }
        }
    }   
    for (i=0;i<10;i++){
    printf("Posição %d do vetor %d \n",i+1,vetor[i]);
    }
}

Error:

C:\Users\Usuario\Desktop\Exercicios alp2\testes1.cpp    In function 'int converte(int**)':
7   20  C:\Users\Usuario\Desktop\Exercicios alp2\testes1.cpp    [Error] invalid conversion from 'int*' to 'int' [-fpermissive]
9   14  C:\Users\Usuario\Desktop\Exercicios alp2\testes1.cpp    [Error] invalid conversion from 'int' to 'int*' [-fpermissive]
18  11  C:\Users\Usuario\Desktop\Exercicios alp2\testes1.cpp    [Error] a function-definition is not allowed here before '{' token
38  1   C:\Users\Usuario\Desktop\Exercicios alp2\testes1.cpp    [Error] expected '}' at end of input
  • But its function converte should receive an array of pointers for int or should it be an int array? Its version with function should invoke the function.

1 answer

0

You were passing the wrong parameters for the function, follow the correct:

#include <stdio.h>
int converte (int *vetor){
        int i,j,troca;
        for (i = 0;i < 10; i++){
        for(j = i+1; j < 10; j++){
            if(vetor[i] > vetor[j]){
                troca = vetor[i];
                vetor[i] = vetor[j];
                vetor[j] = troca;
        }
      }
    }   
for (i = 0; i < 10; i++){
printf("Posição %d do vetor %d \n",i+1,vetor[i]);

}
    }

int main(){

int i, troca,j,vetor[10], ordenado[10];

for (i = 0;i < 10; i++)
{
    printf("Informe o numero %d de um vetor de 10 numeros: ",i+1);
    scanf("%d", &vetor[i]);
}
for (i = 0; i < 10; i++)
{
    for(j=i+1;j<10;j++)
    {
        if(vetor[i] > vetor[j])
        {
            troca = vetor[i];
            vetor[i] = vetor[j];
            vetor[j] = troca;
        }
    }
}   
for (i = 0; i < 10; i++)
{
printf("Posição %d do vetor %d \n",i+1,vetor[i]);
}
}

Browser other questions tagged

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