Doubt about arrays and pointer

Asked

Viewed 73 times

0

#include <stdio.h>
#include <locale.h>

int main () {

    setlocale (LC_ALL,"Portuguese"); // Formatação.

    int notas1[50],notas2[50],notas3[50],soma[50]; // Declaração.

    for (int i = 1; i < 50; i++){ // Dados.
        printf("\nInforme a primeira nota do aluno %i: ",i);
        scanf("%i",&notas1[i]);
        printf("\nInforme a segunda nota do aluno %i: ",i);
        scanf("%i",&notas2[i]);
        printf("\nInforme a terceira nota do aluno %i: ",i);
        scanf("%i",&notas3[i]); }

    for (int h = 1; h < 50; h++){ // Soma e divisão dos vetores 1,2 e 3 para cada nota.
        soma[h] = (notas1[h] + notas2 [h] + notas3 [h])/3; }

    for (int j = 1; j < 50; j++){ // Imprimindo.
        printf ("\nA nota do aluno %i é: %i",j,soma[j]); }

    return 0;
}

My doubts are : How would I ask for the number of vectors to be created for the user and then save in the variable to continue the program ( without doing with pointer ) ? How would the code look extremely simplified or better like this code only done with pointer ( I tried but I didn’t notice ).

  • 1

    If the answer solved your problem, you can click on V next to the answer to mark your question as answered.

  • I’ve already scored vlw.

1 answer

1


As in C it is possible to declare variables anywhere in the code, you can request the value and then declare its vectors. Example:

int vec_len;
scanf("%d", &vec_len);

int notas1[vec_len],notas2[vec_len],notas3[vec_len],soma[vec_len];

But to simplify the code, it would be better to put all the notes in an array

int notas[vec_len][4]; // a quarta posição de notas serve para a soma das notas

Now for the use of pointers, only changes the part of having to allocate to dislocate.

int vec_len;
scanf("%d", &vec_len);

int *notas1 = malloc(vec_len*sizeof(int));
int *notas2 = malloc(vec_len*sizeof(int));
int *notas3 = malloc(vec_len*sizeof(int));
int *soma = malloc(vec_len*sizeof(int));

The sizeof(int) to allocate the exact size compatible with integer values.

And simplification.

int **notas = malloc(vec_len*sizeof(int*)); // matriz ou ponteiro duplo
int x;
for(x=0;x<vec_len;x++)
    notas[x] = malloc(4*sizeof(int));

In **notas owns the sizeof(int*) why are you getting the size of int* to allocate, and notas[x] represents int* and holds values int.

If you are going to use vectors, you do not need to make changes in the insertion of values. If you are going to use matrix it is notas[x][y].

To shift the pointers just do.

//Caso seja uma matriz
//...
free(notas[x]);
//...
free(notas);

//Caso seja vetor
free(notas1);
free(notas2);
free(notas3);
free(soma);
  • In the part " int *notas1 = malloc ", it is not mandatory to put the int between parentheses ? thank you very much for taking the time to reply !!!

  • Actually there is no need, some authors say it is not a good practice, others say it is good to put. put the (int *) is more something towards code semantics. having or not, it makes no difference in the case of malloc.

  • Rs I know, but I have a hell of a knock ! kkkk

Browser other questions tagged

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