Individual average does not return value and the number of student situations does not count in C#

Asked

Viewed 103 times

0

My code it gets 3 student grades and shows your average, counts the number of approved, failed and who are still on exams and show the average of all.

The problems of my code are the accounting of the situations of students who are not in agreement with the notes that are typed and show the individual average of each student who are also not in agreement.

using System;

namespace TesteRepeticao1
{
    class Program8
    {
        static int Main(string[] args)
        {
            var alunos = new int[3];
            int i, reprovados = 0, exame = 0, aprovados = 0;
            double nota1 = 0, nota2 = 0, nota3 = 0, media = 0, mediaclasse = 0, somamedias = 0;
            for (i = 0; i <= alunos.Length; i++)
            {
                Console.Write($"Digite a primeira nota do aluno {i}: ");
                nota1 = Convert.ToDouble(Console.ReadLine());
                Console.Write($"Digite a segunda nota do aluno {i}: ");
                nota2 = Convert.ToDouble(Console.ReadLine());
                Console.Write($"Digite a terceira nota do aluno {i}: ");
                nota3 = Convert.ToDouble(Console.ReadLine());
                media = (nota1 + nota2 + nota3) / 3;
                somamedias += media;
                Console.WriteLine();
            }

            for (i = 0; i < alunos.Length; i++)
            {
                if(media < 5)
                {
                    reprovados++;
                }
                else if (media >= 5 && media < 7)
                {
                    exame++;
                }
                else if (media >= 7 && media < 10)
                {
                    aprovados++;
                }
            }

            for (i = 0; i < alunos.Length; i++)
            {
                Console.WriteLine($"A média do aluno {alunos[i]} é {media}");
            }
            Console.WriteLine();

            mediaclasse = somamedias / 3;
            Console.WriteLine($"O total de alunos reprovados é {reprovados}");
            Console.WriteLine($"O total de alunos em exame é {exame}");
            Console.WriteLine($"O total de alunos aprovados é {aprovados}");
            Console.WriteLine($"A média da classe é {mediaclasse}");
            Console.ReadKey();
            return 0;
        }
    }
}

3 answers

3


There are two errors and a bug with your code.

Mistakes:

  • On the line for (i = 0; i <= alunos.Length; i++) the counter will end the iteration in a unit beyond the capacity of its array, remembering that in C# array starts at index 0 and ends at the index array.Length - 1. To fix just do for (i = 0; i < alunos.Length; i++).

  • The average of the three students are being stored in the same memory space, the variable média. To fix just create a vector media with the same size of students, in this case three average three students, var media = new double[3];.

The bug is as follows, if the user enters a note that is not a number the program crashes or if the user enters a note that is not between 0 and 10 the program runs away from its scope so for each note reading I put the following check:

do{
    Console.Write($"Digite a enésima nota do aluno {i}: ");
    if (!Double.TryParse(Console.ReadLine(), out notaX)) notaX = -1;
} while((notaX < 0) || (notaX > 10));

This check uses the method Double.TryParse() that forms a string of a number in its double-precision floating-point equivalent and returns a Boolean indicating whether the conversion was successful or failed. If the method fails pass -1 for the note if successful passes the converted input to note and then tests if the note is in the range 0 and 10. Being in the appropriate interval passes to next action not being the appropriate track note is reiterated the question on the note.

using System;

namespace TesteRepeticao1
{
    class Program8
    {
        static int Main(string[] args)
        {
            var alunos = new int[3];
            var media = new double[3];
            int i, reprovados = 0, exame = 0, aprovados = 0;
            double nota1 = 0, nota2 = 0, nota3 = 0, mediaclasse = 0, somamedias = 0;
            for (i = 0; i < alunos.Length; i++)
            {
                do{
                   Console.Write($"Digite a primeira nota do aluno {i}: ");
                   if (!Double.TryParse(Console.ReadLine(), out nota1)) nota1 = -1;
                } while((nota1 < 0) || (nota1 > 10));

                do{
                   Console.Write($"Digite a segunda nota do aluno {i}: ");
                   if (!Double.TryParse(Console.ReadLine(), out nota2)) nota2 = -1;
                } while((nota2 < 0) || (nota2 > 10));

                do {
                   Console.Write($"Digite a terceira nota do aluno {i}: ");
                   if (!Double.TryParse(Console.ReadLine(), out nota3)) nota3 = -1;
                } while((nota3 < 0) || (nota3 > 10));
                media[i] = (nota1 + nota2 + nota3) / 3;
                somamedias += media[i];
                Console.WriteLine();
            }

            for (i = 0; i < alunos.Length; i++)
            {
                if(media[i] < 5)
                {
                    reprovados++;
                }
                else if ((media[i] >= 5) && (media[i] < 7))
                {
                    exame++;
                }
                else if ((media[i] >= 7) && (media[i] < 10))
                {
                    aprovados++;
                }
            }

            for (i = 0; i < alunos.Length; i++)
            {
                Console.WriteLine($"A média do aluno {alunos[i]} é {media[i]}");
            }
            Console.WriteLine();

            mediaclasse = somamedias / 3;
            Console.WriteLine($"O total de alunos reprovados é {reprovados}");
            Console.WriteLine($"O total de alunos em exame é {exame}");
            Console.WriteLine($"O total de alunos aprovados é {aprovados}");
            Console.WriteLine($"A média da classe é {mediaclasse}");
            Console.ReadKey();
            return 0;
        }
    }
}

Code in Repl.it

  • Inside the for in line I can replace the do-whiles by others for?

  • @Carlosa. I think so, as long as it’s nothing fancy.

2

You can use the Nota1, nota2 and nota3 variable as an array as well

double[] notas = new double[3];

And make a "for" to go through those notes inside the other "for"

int i2;
for (i2 = 0; i2 < notas.Length; i2++)
{
    notas[i2] = Convert.ToDouble(Console.ReadLine());
}

1

From what I understood from your code the variable media is being rewritten to each student’s grade-dropping loop, so only the average value of the last enrolled student remains on it. So everyone’s average has stayed the same and the situation is the same for all three.

To solve the problem, create a vector also to set the averages.

Browser other questions tagged

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