Variable does not display correct result in C

Asked

Viewed 114 times

0

I have the following problem to solve: Write a program that reads two matrices of 10 rows and 2 columns. Each row of the matrix stores two notes of a student. The first matrix stores the notes of G1 and the second the grades of G2. After reading the two matrices calculate the final average of each of the students and store in a vector of 10 positions. Important the note average is arithmetic. Write the vector resulting on the screen. Important: the final note of G1 is obtained by averaging the two notes of the matrix G1. A G2 note is obtained from the average of the two notes in the matrix of G2.

I realized what was requested, however at the time of displaying the result of media[i] only the value 0 is shown in all vector indices.

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

main()
{
int m1[10][2], m2[10][2];
int soma[10], soma2[10], media[10], result[10], result2[10];
int l, c, i, j;

printf ("\n Notas G1 \n");
for (l=0; l<10; l++)
{
    for (c=0; c<2; c++)
    {
        printf ("\n Aluno [%d], nota [%d]: ", l+1,c+1);
        scanf ("%d", &m1[l][c]);
        soma[l] += m1[l][c]; //soma notas g1
        result[l] = soma[l] / 2; 
    }
}

printf ("\n Notas G2 \n");

for (i=0; i<10; i++)
{
    for (j=0; j<2; j++)
    {
        printf ("\n Aluno [%d], nota [%d]: ", i+1,j+1);
        scanf ("%d", &m2[i][j]);
        soma2[i] += m2[i][j]; // soma notas g2
        result2[i] = soma2[i] / 2;
    }
}

for (i=0; i<10; i++)
{
    media[i] = (result[i] + result2[i])/2; //media
}

for (i=0; i<10; i++)
{
    printf ("\n Media dos aluno [%d]: %.2f", i,media[i]);
}
}
  • How are the variables typing?

1 answer

2

The media, result and result 2 vectors should be of the float type. Another thing that can cause problems: taking into account that

soma[l] += m1[l][c];

is the same thing as

soma[l] = soma[l] + m1[l][c]

and no value has been assigned to sum[l], sum[l] can receive a random value, so I suggest you assign the value 0 to sum[l] before adding up the notes:

printf ("\n Notas G1 \n");
for (l=0; l<10; l++)
{
    soma[l] = 0;
    for (c=0; c<2; c++)
    {
        printf ("\n Aluno [%d], nota [%d]: ", l+1,c+1);
        scanf ("%f", &m1[l][c]);
        soma[l] += m1[l][c]; //soma notas g1
        result[l] = soma[l] / 2;
    }
}
  • But how to make the vector sum[l] receive 0?

  • after entering the loop for (l=0; l<10; l++) and before entering the loop for (c=0; c<2; c++), you put, sum[l] = 0, I’ll edit my answer for you to see.

  • Got it, but still not displaying the result :/

  • the output is still zero for all vector items?

  • change the types of matrices M1 and m2 and the sum and soma2 vectors to float as well

  • For this problem, modify all variables that will participate in the calculation for float typing.

  • They should really be the type float? Notes are not the best things to put on floating point, normally would be used even fixed point.

Show 2 more comments

Browser other questions tagged

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