Problems with result accuracy

Asked

Viewed 136 times

0

I’m having a problem where I did a program to calculate a student’s average, where the condition is:

MEAN = NOTA1 + NOTA2/2 (the result should be >= 7 for the student to pass)

The program is running right, the problem is that when I use decimal values close to 7 it does not return the response of the condition...

For example; the average was: 6,9. The right one was to appear the message (DISAPPROVED), but it does not appear... if I use values.

The curious thing is that if it is any decimal number below 6, it works normally... for example: media = 5.9 prints FAILED

string NomeDoAluno, disciplina, RA;
int NumeroDeFaltas;
double NP1, NP2, MEDIA_FINAL;


Console.Write("\nDIGITE O SEU NOME: "); 
NomeDoAluno = Console.ReadLine();

Console.Write("DIGITE O SEU RA: ");
RA = Console.ReadLine();

Console.Write("DIGITE A DISCIPLINA: ");
disciplina = Console.ReadLine();

Console.Write("DIGITE O SEU NÚMERO DE FALTAS: ");
NumeroDeFaltas = int.Parse(Console.ReadLine());

Console.Write("DIGITE SUA NP1 E SUA NP2: ");
NP1 = Double.Parse (Console.ReadLine());
NP2 = Double.Parse (Console.ReadLine());

MEDIA_FINAL = (NP1 + NP2) / 2;


Console.WriteLine("\nNome:  {0} \tRA:  {1}", NomeDoAluno, RA);
Console.WriteLine("\nDisciplina: {0}   \t Número de Faltas:  {1}", disciplina,NumeroDeFaltas);
Console.WriteLine("\nNotas\tP1:  {0}  \tP2: {1}   \tMEDIA FINAL:  {2}", NP1, NP2,MEDIA_FINAL);

//CONDIÇÃO PARA APROVAÇÃO DO ALUNO 

if (MEDIA_FINAL >= 7 & NumeroDeFaltas <= 10)
{
    Console.Write("\nPARABÉNS VOCÊ FOI APROVADO !");
}


else if (NumeroDeFaltas > 10 & MEDIA_FINAL <= 6)
{
    Console.Write("\nVOCÊ FOI REPROVADO POR FALTA E NOTA!");
}

else if (NumeroDeFaltas > 10 & MEDIA_FINAL >=7)
{
    Console.Write("\nVOCÊ FOI REPROVADO POR FALTA !");
}

else if (MEDIA_FINAL <= 6 & NumeroDeFaltas <= 10) 
{
    Console.Write("\nVOCÊ FOI REPROVADO POR NOTA!");
}
  • Else if (Numbers > 10 & MEDIA_FINAL < 7) AND ALSO Else if (MEDIA_FINAL < 7 & Numbers <= 10)

  • From what I’ve seen, you have no fail condition where test that is less than 7, always less than 6. So values between 6 and 7 are ignored from tests.

  • The solution is given by my comment above, I only drafted an answer because surely others will do based on this condition.

  • read this post https://pt.meta.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-resposta/1079#1079

3 answers

1

You are mixing the data types to compare(Int and double). Ideally you would only use decimal or double. Examples:

  • 10 (Int)
  • 10.0 (double)
  • 10m (decimal)

    double MEDIA_FINAL = (NP1 + NP2) / 2.0;

Another problem is that the conditions do not meet the interval between 6 and 7. The correct one would be:

if (MEDIA_FINAL >= 7 & NumeroDeFaltas <= 10)
{
Console.Write("\nPARABÉNS VOCÊ FOI APROVADO !");
}

else if (NumeroDeFaltas > 10 & MEDIA_FINAL < 7)
{
Console.Write("\nVOCÊ FOI REPROVADO POR FALTA E NOTA!");
}

1

The range less than 7 is not defined, the system is only failing note <= 6 when it should be < 7

else if (NumeroDeFaltas > 10 & MEDIA_FINAL < 7)

else if (MEDIA_FINAL < 7 & NumeroDeFaltas <= 10)

So cover all combinations

if (MEDIA_FINAL >= 7 & NumeroDeFaltas <= 10)
{
Console.Write("\nPARABÉNS VOCÊ FOI APROVADO !");
}

else if (NumeroDeFaltas > 10 & MEDIA_FINAL < 7)
{
Console.Write("\nVOCÊ FOI REPROVADO POR FALTA E NOTA!");
}

else if (NumeroDeFaltas > 10 & MEDIA_FINAL >=7)
{
Console.Write("\nVOCÊ FOI REPROVADO POR FALTA !");
}

else if (MEDIA_FINAL < 7 & NumeroDeFaltas <= 10) 
{
Console.Write("\nVOCÊ FOI REPROVADO POR NOTA!");
}

1

Surely your problem lies in mixing the types (double, int, etc) in the calculation of the media. You can read here about what is the correct way to use float, double and decimal.

I was in a good mood, and I decided to write a more elaborate code to do the same thing, but with object orientation.

Have fun!

Calculating Media Like a Boss

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        Console.Write("\nDIGITE O SEU NOME: ");
        var nome = Console.ReadLine();

        Console.Write("DIGITE O SEU RA: ");
        var ra = Console.ReadLine();

        var aluno = new Aluno(nome, ra);

        Console.Write("DIGITE A DISCIPLINA: ");
        var nomeDisciplina = Console.ReadLine();
        var disciplina = aluno.AdicionarDisciplina(nomeDisciplina);

        Console.Write("DIGITE O SEU NÚMERO DE FALTAS: ");
        var faltas = int.Parse(Console.ReadLine());
        disciplina.AdicionarFaltas(faltas);

        foreach (var prova in disciplina.Provas)
        {
            Console.Write("DIGITE A NOTA DA PROVA "+ prova.Tipo +": ");
            var nota = decimal.Parse(Console.ReadLine());
            prova.DarNota(nota);
        }

        Console.WriteLine("\nNome:  {0} \tRA:  {1}", aluno.Nome, aluno.RA);
        Console.WriteLine("\nDisciplina: {0}   \t Número de Faltas:  {1}", disciplina.Nome, disciplina.Faltas);
        foreach (var prova in disciplina.Provas) Console.WriteLine("\nNotas\t{0}: {1}", prova.Tipo, prova.Nota);
        Console.WriteLine("\nMEDIA FINAL: {0}", disciplina.CalcularMedia());

        if (disciplina.AprovadoGeral())
            Console.Write("\nPARABÉNS VOCÊ FOI APROVADO !");
        else
        {
            if (!disciplina.AprovadoPorNota())
                Console.Write("\nOOPS! FOI REPROVADO POR NOTA!");
            if (!disciplina.AprovadoPorPresenca())
                Console.Write("\nOOPS! FOI REPROVADO POR FALTA!");
        }
    }
}

public class Aluno
{
    public string Nome { get; private set; }
    public string RA { get; private set; }
    public List<Disciplina> Disciplinas { get; }

    public Aluno(string nome, string ra)
    {
        Nome = nome;
        RA = ra;
        Disciplinas = new List<Disciplina>();
    }

    public Disciplina AdicionarDisciplina(string nomeDisciplina)
    {
        var disciplina = new Disciplina(nomeDisciplina);
        Disciplinas.Add(disciplina);
        return disciplina;
    }
}


public class Disciplina
{
    public string Nome { get; private set; }
    public int Faltas { get; private set; }
    public List<Prova> Provas { get; }

    private const int _faltasMaximasParaAprovar = 10;
    private const decimal _notaMinimaParaAprovar = 7m;

    public Disciplina(string nome)
    {
        Nome = nome;
        Faltas = 0;
        Provas = new List<Prova>
        {
            new Prova(TipoProva.NP1),
            new Prova(TipoProva.NP2),
        };
    }

    public void AdicionarFaltas(int faltas)
    {
        Faltas += faltas;
    }

    public decimal CalcularMedia()
    {
        var total = 0m;
        foreach (var prova in Provas) total += prova.Nota;

        var media = total / Provas.Count;
        return media;
    }

    public bool AprovadoPorNota()
    {
        var media = CalcularMedia();
        return media >= _notaMinimaParaAprovar;
    }

    public bool AprovadoPorPresenca()
    {
        return Faltas <= _faltasMaximasParaAprovar;
    }

    public bool AprovadoGeral()
    {
        var aprovadoPorNota = AprovadoPorNota();
        var aprovadoPorPresenca = AprovadoPorPresenca();

        return aprovadoPorNota && aprovadoPorPresenca;
    }
}

public class Prova
{
    public TipoProva Tipo { get; }
    public decimal Nota { get; private set; }

    public Prova(TipoProva tipo)
    {
        Tipo = tipo;
    }

    public void DarNota(decimal nota)
    {
        Nota = nota;
    }
}

public enum TipoProva
{
    NP1,
    NP2
}

See working here

  • Very complete answer, you did it all again. kkkk

  • This statement in the question "The most curious thing is that if it is any decimal number below 6, it works normally... for example: media = 5.9 prints FAILED" puts its statement (Int and double) as unfounded.

  • 1

    @Leocaracciolo could not be a question of rounding the value, either "up" or "down" ?

  • If it is 6.1 and round without being explicit, you can round it up to 7.0 (as it is the next valid integer). Then proceed yes.

  • @Jcsaint notice in Else if that there is no condition in the range greater than 6 up to less than 7. This is the problem, a matter of logic, when it comes to the question of rounding, I can’t say anything because I don’t know the language and I don’t know how to test the author’s code. But he states that ""if it is ANY decimal number below 6, it works normally", so it should work for any number below 7 if THERE is in the logic condition of testing these numbers below 7

  • I don’t know anything about c# and I don’t even know how to make your netfiddle return something, I only know that the author states that "if it is any decimal number below 6, it works normally..." and soon after states that " ... for example: media = 5.9 prints FAILED"

  • THANK YOU VERY MUCH ... HELPED A LOT ^^

  • Do you recommend some encryption book to encrypt messages in c# or c?

  • @Leocaracciolo opens the link, clicks on "Run" and in the footer will be the app running.

  • yes, it had already done, but after clicking on "Run" nothing happens, just ask to enter a name, type a name and nothing happens! See this print http://kithomepage.com/sos/fiddle.png

  • Did you enter? I know it’s a silly question, but...

  • @Thiagolunardi worse than was not silly no, hahaha, your script is excellent!

Show 7 more comments

Browser other questions tagged

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