Doubt about vector in C

Asked

Viewed 78 times

0

Good evening, I have a doubt in the code I made below, my teacher requested that the values name, ra, n1,N2 be read and be displayed to the average of 40 students, I’m trying to put when I try to run my program is not calculating the media and gets doesn’t work and only shows a 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]);   
        }
}    
 }
  • Try to use like this: gets(nome[i])

2 answers

0

Come on I found several errors in your code and put comments explaining, but basically you were running too many for no need, you were using %d to float and that uses %f, and was trying to get vector name needs to be matrix, follows the code commented 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 main(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;
 }

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 be as follows:

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 5 vectors now create 1 vector of Aluno with 40 elements in it, in this way:

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

So combining this with the rest of the logic you already have 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;
}

This way each time you want to access a student you have to specify the position and field of the structure, ex:

alunos[2].nome

That accesses the field nome student at position 2. This will be valid both for writing (printf) as for reading(scanf), with the difference of & to the scanf whenever the field is no longer a pointer.

Browser other questions tagged

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