Hello, i have some problems with my c code, could you help me?
it's returns randomly numbers for averages.

#include<stdio.h>
#include<stdlib.h>

struct Student MStudent(struct Student std);
void CalcAvrg(struct Student std);

struct Student{
   int mark[5][5];

};

struct Student MStudent(struct Student std){
int i,j;

for(i=0;i<5;i++){
    for(j=0;j<5;j++){
    printf("%d. Student's mark for %d. exam :\n", i+1,j+1);
    scanf("%d", &std.mark[i][j]);   
}   
}

return std;
}

void CalcAvrg(struct Student std){
    int i;
    for(i=0;i<5;i++){
        printf("%d. Exam's average is: %d\n", i+1,
        (std.mark[0][i] +std.mark[1][i] + std.mark[2][i]+ std.mark[3][i]+ std.mark[4][i])/5);
    }
}

int main(){
      struct Student dene;
      MStudent(dene);
      CalcAvrg(dene);

    return 0;
}

i wrote it in this way too, but it didnt compile in Dev c++;

#include<stdio.h>
#include<stdlib.h>

struct Student
{
    int mark[5][5] = {{0}};
};

void MStudent( Student& myStruct);
void CalcAvrg( Student& myStruct);

int main()
{
    struct Student dene;
    MStudent(dene);
    CalcAvrg(dene);
    return 0;
}
void MStudent( Student& myStruct)
{
    int i,j;
    for(i=0; i<5; i++)
    {
        for(j=0; j<5; j++)
        {
            printf("%d. Student's mark for %d. exam :\n", i+1,j+1);
            scanf("%d", &myStruct.mark[i][j]);
        }
    }
}

void CalcAvrg( Student& myStruct)
{
    int i;
    for(i=0; i<5; i++)
    {
        printf("%d. Exam's average is: %d\n", i+1,

        (myStruct.mark[0][i] + myStruct.mark[1][i] +
         myStruct.mark[2][i] + myStruct.mark[3][i] +
         myStruct.mark[4][i])/5);
    }
}

Recommended Answers

All 3 Replies

I haven't looked at C++ in soooooooo many years, so I don't really remember it. However, you mentioned the second snippet you posted didn't compile, and I don't remember int mark[5][5] = {{0}}; being valid syntax.

Are you programming in C (file extension .c) or in C++ (file extension .cpp)?

Hi,

A couple of things to look at:

1 - In your function "MStudent" - what are you doing with the return value from this? Hint - check out how you are calling it.
2 - In the function "CalcAvrg" - take a closer look at how you are accessing the items in the array. One dimension is for each student and the other is for each grade for a particular student.

-Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.