Hello all,

I am now on lesson 4

This is my code

multi_dimensional.c

```
#include <stdio.h>
extern void average_grades_from_fortran(int subjects,int students,int grades[subjects][students]);
void calculate_the_average_of_subjects(int number_of_subjects,int number_of_students,int grades[number_of_subjects][number_of_students])
{
float average_grade;
for (int i=0;i<number_of_subjects;i++)
{
average_grade = 0.0;
for (int j=0;j<number_of_students;j++)
{
average_grade +=grades[i][j];
}
average_grade /= number_of_students;
printf("From C the average marks for subject %d is: %.2f \n",i,average_grade);
}
average_grades_from_fortran(number_of_subjects,number_of_students,grades);
}
```

multi_dimensional.f90

```
module calculate_the_average_grade
use,intrinsic :: iso_c_binding,only: c_int,c_float
implicit none
private
public :: intro
interface
subroutine what_is_the_average_grade(subjects,students,grades) bind(C,name="calculate_the_average_of_subjects")
import :: c_int
implicit none
integer(kind=c_int),value,intent(in) :: subjects,students
integer(kind=c_int),dimension(subjects,students),intent(in) :: grades
end subroutine what_is_the_average_grade
end interface
contains
subroutine average_grades_from_fortran(subjects,students,grades) bind(C)
integer(kind=c_int),intent(in),value :: subjects,students
integer(kind=c_int),dimension(subjects,students),intent(in) :: grades
real(kind=c_float) :: average_of_the_grades
integer(c_int) :: i,j
print *, " "
do i=1,subjects
average_of_the_grades=0.0
do j=1,students
average_of_the_grades=average_of_the_grades+real(grades(i,j),kind=c_float)
end do
average_of_the_grades=average_of_the_grades/size(grades,dim=2,kind=c_float)
print "(a,1x,i0,1x,a,1x,f12.6)", "In Fortran the average for subject",i,"is:",average_of_the_grades
end do
end subroutine average_grades_from_fortran
subroutine intro()
integer(kind=c_int) :: given_subjects,given_students
integer(kind=c_int),dimension(:,:),allocatable :: given_grades
integer (kind=c_int) :: i,j
write(unit=*,fmt="(a,1x)",advance="no") "Please enter the number of subjects"; read *, given_subjects
write(unit=*,fmt="(a,1x)",advance="no") "Please enter the number of students"; read *, given_students
allocate(given_grades(given_subjects,given_students))
do i=1,given_subjects
do j=1,given_students
write(unit=*,fmt="(a,1x,i0,1x,a,1x,i0,1x,a,1x)",advance="no") "Please enter the grade for subject",i,"for student",j,":"
read *, given_grades(i,j)
end do
end do
call what_is_the_average_grade(given_subjects, given_students,given_grades)
end subroutine intro
end module calculate_the_average_grade
```

main.f90

```
program main
use calculate_the_average_grade
implicit none
call intro()
end program main
```

Compiled with:

```
gfortran -O3 multi_dimensional.c multi_dimensional.f90 main.f90 -o calculate_the_students_grades
```

Output:

```
Please enter the number of subjects 2
Please enter the number of students 2
Please enter the grade for subject 1 for student 1 : 34
Please enter the grade for subject 1 for student 2 : 22
Please enter the grade for subject 2 for student 1 : 12
Please enter the grade for subject 2 for student 2 : 23
From C the average marks for subject 0 is: 23.00
From C the average marks for subject 1 is: 22.50
In Fortran the average for subject 1 is: 28.000000
In Fortran the average for subject 2 is: 17.500000
```

Why is there such a difference in the output from Fortran vs C ? Did I mess up somewhere ?

I also know there is a more modern way to do things with the C descriptor , but I wanna learn this way first.