-5

Hey people this is my first question on this page. I have been learning C in university and I have to complete an activity where I insert name, surname and 4 grades, of 3 different students. After that I must calculate average grade, maximum and minimum grade, and print those results on screen.

The program compiles correctly without errors, but when the results are printed, the program says that average grade, maximum and minimum grade are all equal to 0.000000.

I really don't know what the problem is and I would apreciate if anyone could check it out and help me in any way.

(Sorry if you don't understand the variable names, I'm from Argentina and they are in Spanish)

#include <stdio.h>
#define N 50

struct alumno
{
    char nombre [N];
    char apellido [N];
    float nota1;
    float nota2;
    float nota3;
    float nota4;
} alumno1, alumno2, alumno3;

struct notas
{
    float prom;
    float nmax;
    float nmin;
} notas1, notas2, notas3;

float promedio (struct alumno a, struct notas b)
{
    b.prom = (a.nota1 + a.nota2 + a.nota3 + a.nota4)/4;

    return b.prom;
};

float notamaxima (struct alumno a, struct notas b)
{
    if ((a.nota1>a.nota2) && (a.nota1>a.nota3) && (a.nota4>a.nota4))
    {
        b.nmax=a.nota1;
    }
    else if ((a.nota2>a.nota1) && (a.nota2>a.nota3) && 
            (a.nota2>a.nota4))
    {
        b.nmax=a.nota2;
    }
    else if ((a.nota3>a.nota1) && (a.nota3>a.nota2) && 
                (a.nota3>a.nota4))
    {
        b.nmax=a.nota3;
    }
    else if ((a.nota4>a.nota1) && (a.nota4>a.nota2) && 
            (a.nota4>a.nota3))
    {
        b.nmax=a.nota4;
    }
    return 0;
};

float notaminima (struct alumno a, struct notas b)
{

    if ((a.nota1<a.nota2) && (a.nota1<a.nota3) && (a.nota1<a.nota4))
    {
        b.nmin=a.nota1;
    }
    else if ((a.nota2<a.nota1) && (a.nota2<a.nota3) && 
            (a.nota2<a.nota4))
    {
        b.nmin=a.nota2;
    }
    else if ((a.nota3<a.nota1) && (a.nota3<a.nota2) && 
            (a.nota3<a.nota4))
    {
        b.nmin=a.nota3;
    }
    else if ((a.nota4<a.nota1) && (a.nota4<a.nota2) && 
            (a.nota4<a.nota3))
    {
        b.nmin=a.nota4;
    }
    return 0;
};

int main ()
{
    struct alumno;
    struct notas;

    printf ("Ingrese nombre del alumno:\n");
    fgets (alumno1.nombre, N, stdin);
    printf ("Ingrese apellido del alumno:\n");
    fgets (alumno1.apellido, N, stdin);
    printf ("Ingrese la primera nota del alumno:\n");
    scanf ("%f", &alumno1.nota1);
    printf ("Ingrese la segunda nota del alumno:\n");
    scanf ("%f", &alumno1.nota2);
    printf ("Ingrese la tercera nota del alumno:\n");
    scanf ("%f", &alumno1.nota3);
    printf ("Ingrese la cuarta nota del alumno:\n");
    scanf ("%f", &alumno1.nota4);

    promedio (alumno1, notas1);
    notamaxima (alumno1, notas1);
    notaminima (alumno1, notas1);

    printf ("El promedio del primer alumno es %f\n", notas1.prom);
    printf ("La nota maxima del primer alumno es %f\n", notas1.nmax);
    printf ("La nota minima del primer alumno es %f\n", notas1.nmin);

    printf ("Ingrese nombre del alumno:\n");
    fgets (alumno1.nombre, N, stdin);
    printf ("Ingrese apellido del alumno:\n");
    fgets (alumno1.apellido, N, stdin);
    printf ("Ingrese la primera nota del alumno:\n");
    scanf ("%f", &alumno2.nota1);
    printf ("Ingrese la segunda nota del alumno:\n");
    scanf ("%f", &alumno2.nota2);
    printf ("Ingrese la tercera nota del alumno:\n");
    scanf ("%f", &alumno2.nota3);
    printf ("Ingrese la cuarta nota del alumno:\n");
    scanf ("%f", &alumno2.nota4);

    promedio (alumno2, notas2);
    notamaxima (alumno2, notas2);
    notaminima (alumno2, notas2);

    printf ("El promedio del segundo alumno es %f\n", notas2.prom);
    printf ("La nota maxima del segundo alumno es %f\n", notas2.nmax);
    printf ("La nota minima del segundo alumno es %f\n", notas2.nmin);

    printf ("Ingrese nombre del alumno:\n");
    fgets (alumno1.nombre, N, stdin);
    printf ("Ingrese apellido del alumno:\n");
    fgets (alumno1.apellido, N, stdin);
    printf ("Ingrese la primera nota del alumno:\n");
    scanf ("%f", &alumno3.nota1);
    printf ("Ingrese la segunda nota del alumno:\n");
    scanf ("%f", &alumno3.nota2);
    printf ("Ingrese la tercera nota del alumno:\n");
    scanf ("%f", &alumno3.nota3);
    printf ("Ingrese la cuarta nota del alumno:\n");
    scanf ("%f", &alumno3.nota4);

    promedio (alumno3, notas3);
    notamaxima (alumno3, notas3);
    notaminima (alumno3, notas3);

    printf ("El promedio del tercer alumno es %f\n", notas3.prom);
    printf ("La nota maxima del tercer alumno es %f\n", notas3.nmax);
    printf ("La nota minima del tercer alumno es %f\n", notas3.nmin);

    return 0;
}
brainplot
  • 131
  • 2
  • 12
  • 2
    Stackoverflow is not a do my homework service, so your question is **off-topic**. Recommendations: read more about C programming. Compile with all warnings and debug info: `gcc -Wall -Wextra -g` with [GCC](http://gcc.gnu.org/). Improve the code to get no warnings. Then [use the `gdb` debugger](https://sourceware.org/gdb/current/onlinedocs/gdb/) to understand the behavior of your program. Improve it. Test your program. Repeat all this till you are satisfied. – Basile Starynkevitch May 06 '18 at 17:13
  • Read also http://floating-point-gui.de/ if you use `float` or `double` – Basile Starynkevitch May 06 '18 at 17:16
  • 2
    See also [this](https://stackoverflow.com/a/49952924/841108) explanation about call-by-value arguments in C – Basile Starynkevitch May 06 '18 at 17:19
  • I didn't mean to use this site for that. I'm sorry if you thought that. I actually did all the coding, so I wasn't really looking for anyone to do it for me LOL :) I just wanted to know what the problem is since I've read, asked my professors and also classmates, and no one seems to find the problem. – Gaspar Bosch May 06 '18 at 17:29
  • Anyway thanks for the recomendations, I'll keep trying to find the issue. – Gaspar Bosch May 06 '18 at 17:30
  • You did not *end* your coding, since finding and correcting bugs is an essential part of the developer's job. – Basile Starynkevitch May 06 '18 at 17:40
  • 1
    Is it possible that some of the notes are equal? How should the `notaminima` and `notamaxima` functions behave in this case? – Razvan Socol May 06 '18 at 17:43

1 Answers1

1

I think the function here you used doesn't make any changes

float promedio (struct alumno a, struct notas b)  

All parameters you send to the function need to be the address type like this

void promedio (struct alumno *a, struct notas *b) 
.
.
.
promedio (&alumno3, &notas3);

The question about call by value & call by reference

Another question is you always return 0 in every each function
Why not use 'void' or return the calculation results , and the result must need to be accepted by a parameter

float notamaxima (struct alumno a, struct notas b)
{
    if ((a.nota1>a.nota2) && (a.nota1>a.nota3) && (a.nota4>a.nota4))
    {
        return a.nota1;
    }...
}

b.nmax = notamaxima (alumno1, notas1);

It's impossible your professors didn't know that.

sry for my eng

吳柏亞
  • 41
  • 6