Problem passing array as parameter

0

I'm not able to pass the array parameters correctly to the calculos function. The code compiles correctly, but the function does not get the values I'm trying to pass. Since it is an array of type struct, this should be the problem.

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

typedef struct faculdade
{
  char disc[40];
  int mat;
  int rfreq;
  int arpro;
} facu;


void calculos(int quant[],int aprov[],int repr[],float *por_apr,float *por_re)
{ int i;



    int total=0;
    int total_apro=0;
    int total_re=0;

    for(i=0;i<1;i++){
    total+=quant[i];
    total_apro+=aprov[i];
    total_re+=repr[i];}
    *por_apr=((float)total_apro/total)*100;
    *por_re=((float)total_re/total)*100;



}








int main(){
  facu d[100];
  int i,n=1;
  float p_ap,p_re;

    for (i=0; i<n; i++){
      printf("Digite a %d° disciplina:\n",i+1);
      scanf("\n%[^\n]", d[i].disc );
      printf("digite quantos alunos foram matriculados na disciplina %s:\n",d[i]);
      scanf("%d", &d[i].mat );
      printf("digite quantos alunos foram aprovados na disciplina %s:\n",d[i]);
      scanf("%d", &d[i].arpro );
      printf("digite quantos alunos foram reprovados na disciplina %s:\n",d[i]);
      scanf("%d", &d[i].rfreq );
    }for (i=0; i<n; i++){
    printf("\nmatriculados %d\n",d[i].mat);
    printf("aprovados %d\n",d[i].arpro);}

    calculos(d->mat,d->arpro,d->rfreq,&p_ap,&p_re);







  return 0;

}
    
asked by anonymous 07.10.2017 / 14:47

1 answer

0

Dude, I kind of did not quite understand what you want with this function, if you can explain to me I'm sure I can improve my answer for your better understanding, but going straight to the answer.

First pass your% of% direct in the parameters since you are working with it anyway, type struct , it is very important to note that when passing a vector by parameter you have to pass the size of it , because otherwise the function will not know what it is, and this can result in errors, vectors can be passed in parameters in two ways, following the example above, as void calculos ( facu* dado, int tam, float* por_apr, float* por_re ) or facu* dado , both are the same thing but in one the pointer is implied.

It's important that since you do not know what the size of the vector will be, ask the user, so allocate a vector of the correct size, so you have a gain in performance.

To make a dynamic allocation, just have the facu dado[] library included and then declare a pointer to the type you want to allocate then call the function <stdlib.h> , ex malloc , it works as follows, in the parameters of facu* d = (facu*)malloc(sizeof(facu) * tam); you will pass the size of the data type you are using (highly recommended that you use malloc , since the size can vary from architecture to architecture) and the size you want your vector to have, is very It is important to remember that you should always do a casting for the type of pointer you are using, so sizeof , and also always remember when to finish using vector to release the space allocated in memory with the command (facu*) , ex: free .

You also had a small error in your first free(d); , scanf , I do not know if it was intentional or not, but do not delete this scanf("\n%[^\n]", d[i].disc ); , and always remember to leave a space between I want to be able to do this in a way that I think is correct to do this \n is, scanf .

Without further ado here is the answer (I think).

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

typedef struct faculdade
{
  char disc[40];
  int mat;
  int rfreq;
  int arpro;
} facu;

void calculos ( facu* dado, int tam, float* por_apr, float* por_re )
{
    int i, total=0, total_apro=0, total_re=0;

    for ( i = 0; i < tam; ++i) {
        total += dado[i].mat;
        total_apro += dado[i].arpro;
        total_re += dado[i].rfreq;
    }

    *por_apr = ((float)(total_apro/total)) * 100;
    *por_re = ((float)(total_re/total)) * 100;
}

int main()
{
  int i, tam; /** declare a variavel do tamanho aqui. */
  float p_ap, p_re;

  printf("Quatos alunos ha: "); /* pergunte o tamanho do vetor, depois aloque-o */
  scanf(" %i", &tam);
  facu* d = (facu*)malloc(sizeof(facu) * tam);

    for ( i = 0 ; i < tam; ++i ) {
      printf("Digite a %i° disciplina: ", i+1);
      scanf(" %[^\n]s", d[i].disc );

      printf("digite quantos alunos foram matriculados na disciplina %s: ", d[i].disc);
      scanf(" %i", &d[i].mat );

      printf("digite quantos alunos foram aprovados na disciplina %s: ", d[i].disc);
      scanf(" %i", &d[i].arpro );

      printf("digite quantos alunos foram reprovados na disciplina %s: ", d[i].disc);
      scanf(" %i", &d[i].rfreq );
    }

    for ( i = 0; i < tam; ++i ) {
    printf("\nmatriculados %i\n", d[i].mat);
    printf("aprovados %i\n", d[i].arpro);
    }

    calculos(d, tam, &p_ap, &p_re);

    free(d);        

    return 0;

}

For a better understanding of what has been said here are some links that can help you:

Pass by reference:

link

Dynamic memory allocation:

link

I had more links, but unfortunately I could not post: (.

    
07.10.2017 / 15:43