Question about vector in C

0

Good evening, I have a question in the code I made below, my teacher requested that the values name, ra, n1, n2 be read and the average of 40 students should be read, I am trying to do it when I try to run my program is not calculating the media and the gets does not work and only shows one character instead of the full name

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

 int main(){
char nome[50];
int i=0,j=0,k=0,l=0,m=0;
 float n1[39],n2[39],ra[39],media[39];
 for (i=0;i<40;i++){
  printf("Insira o nome: ");
    gets("%s",&nome[i]);
   }
for (j=0;j<40;j++){
printf("Insira o RA: ");
    scanf("%d",&ra[j]);
}
for (k=0;k<40;k++){ 
    printf("Informe a primeira nota do %d aluno",k+1);
    scanf("%d",&n1[k]);
}
for (l=0;l<40;l++){
    printf("Informe a segunda nota do %d:  ",l+1);
    scanf("%d",&n2[l]);
}
for (m=0;m<40;m++){
    media[39] = (n1[k] * n2[l])/2;
    printf("o aluno %s ra numero:%d teve a media %d 
",nome[i],ra[j],media[m]);   
        }
}    
 }
    
asked by anonymous 22.08.2017 / 05:06

2 answers

0

I've found several errors in your code and I've put comments explaining, but basically you were running many for without needing it, I was using% d for float and this uses% f, and was trying to get vector name the code commented out and summarized, and another do not forget to put the return 0 at the end or else put void inside the main parameter so int (void).

#include<stdio.h>  
#include<stdlib.h>                                                                                                                            
#include<string.h>
 int main(){

//Quando se é string crie matriz como nome e ra terá de ter 2 o primeiro para o tamanho e o sugundo para posição
//Ra não precisa ser float
char nome[50][50],ra[40][40];

//Não necessita 5 for crie só 2
int i=0,m=0;
//Não de tamanho exato para vetores utilize sempre mais
 float n1[40],n2[40],media[40];
 for(i=0;i<40;i++){
    printf("Insira o nome: ");
    //Troquei gets por scanf com %s que significa que irá pegar uma string
    scanf("%s",nome[i]);
    printf("Insira o RA: ");
    scanf("%s",ra[i]);
    printf("Informe a primeira nota do %d aluno",i+1);
    //Seu maior erro está aqui, quando vai pegar float use %f e não %d, %d é para inteiro
    scanf("%f",&n1[i]);
    printf("Informe a segunda nota do %d:  ",i+1);
    scanf("%f",&n2[i]);
   }

    for(m=0;m<40;m++){
    media[m] = (n1[m] + n2[m])/2;
    printf("o aluno %s ra numero: %s teve a media %f",nome[m],ra[m],media[m]);   
        }

   return 0;
 }
    
22.08.2017 / 13:15
0

My answer is an alternative solution to the program that has already corrected simultaneously the problems that Anderson Henrique had already pointed out.

And why not use structures? If all the vectors you are using refer to information that each student has, then you can create a structure that encompasses the information of each one (similar to the classes in higher level languages).

For the information you have in your program, the structure would look like this:

struct Aluno {
    char nome[50];
    float n1;
    float n2;
    float ra;
    float media;
};

In this case everything is organized. And in the main instead of creating% with% vectors it will create 5 vector of 1 with Aluno elements in it, like this:

int main(){
    ...
    Aluno alunos[40]; //criar um vetor de 40 alunos

So combining this with the rest of the logic that already had it would look like this:

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

struct Aluno {
    char nome[50];
    float n1;
    float n2;
    float ra;
    float media;
};

typedef struct Aluno Aluno; //criar um outro nome para o tipo da estrutura para simplificar

int main(){

    int i;

    Aluno alunos[40]; //criar um vetor de 40 alunos

    for (i=0;i<40;i++){
        printf("\nInsira o nome: ");
        scanf("%s",alunos[i].nome);
        printf("Insira o RA: ");
        scanf("%f",&alunos[i].ra);
        printf("Informe a primeira nota do %d aluno: ",i+1);
        scanf("%f",&alunos[i].n1);
        printf("Informe a segunda nota do %d aluno: ",i+1);
        scanf("%f",&alunos[i].n2);
        alunos[i].media = (alunos[i].n1 + alunos[i].n2)/2; //media tem de ser com soma e não multiplicação
        printf("o aluno %s ra numero:%.2f teve a media %.2f",alunos[i].nome,alunos[i].ra,alunos[i].media);
    }

    return 0;
}

In this way each time you want to access a student you have to specify the position and field of the structure, eg

alunos[2].nome

This accesses the 40 field of the student in position 2. This will be valid for writing ( nome ) and reading ( printf ), with the difference of scanf for & whenever the field is no longer a pointer.

    
22.08.2017 / 15:55