I’m not getting the vector from the subprogram into the program,

Asked

Viewed 32 times

-1

Do a subprogram that has as input and output parameters: the address of an integer vector and the number of elements of that vector. The subprogram must eliminate from the vector all values that are outside the range between 10 and 20, placing the values inside the vector and adjusting the number of vector elements

#include <stdio.h>
#include <stdlib.h>

    int NrVector(int j);

int main(){
    int j,vector[j],z=0,h;

    printf("Introduza o nr de numeros que pretende introduzir:");
    scanf("%d", &j) ;
     h=j;

    vector[z] = NrVector(j);    
    printf("%d", NrVector(j));
            for(z=0;z<j;z++){if(10<=vector[z] && vector[z]<=20){
            printf("{%d}", vector[z]);}
            else{ h-=1;}    }

            printf("o nr de elementos: %d", h);

    printf("\n");
    system("pause");
    exit(1);
}
    int NrVector(int j){
    int vector[j],i=0,z=0,valor;

                for(i=0;i<j;i++){

                printf("Introduza o nr:");
                scanf("%d", &valor);
                vector[i]=valor;}

                }       

1 answer

0

Several errors. Try:

#include <stdio.h>

void NrVector(int *, int);

int main() {
    int j, y, z, h;
    printf("Introduza o nr de numeros que pretende introduzir:");
    scanf("%d", &j);
    int vector[j];
    h=j;
    NrVector(vector, j);
    for(z=0; z<h; z++) {
        if(10<=vector[z] && vector[z]<=20) {
            printf("{%d}", vector[z]);
        }
        else {
            for (y=z+1; y<h; y++)
                vector[y-1] = vector[y];
            h-=1;
        }
    }
    printf("o nr de elementos: %d\n\n", h);
    for(z=0; z<h; z++)
        printf("\t[%d]: %d", z, vector[z]);
    printf("\n");
    return 0;
}

void NrVector(int * vet, int j) {
    int i=0, z=0;
    for(i=0; i<j; i++) {
        printf("Introduza o nr:");
        scanf("%d", &vet[i]);
    }       
}

Browser other questions tagged

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