why i get this eror

hi all
i wrote this functions and got all the main written already
and i get the end of syntex eror all the time
can somone help me pls
thanks
p.s
if u can look on the function and see if there somthing wrong in it

#include <stdio.h>
#define MAX_IN_LEC 20
#define LECTURES_NUM 4

float get_max_value (float a[])
{
float max=0;
int i;
for (i=0;i<LECTURES_NUM*MAX_IN_LEC;i++)
{if (max<a[i])
{max=a[i];}
}

return max;
}

int update_factor (float a[])
{
int i;
float factor,max;
max=get_max_value(a);
factor=100-max;
for(i=0;i<LECTURES_NUM*MAX_IN_LEC;i++)
{
if ((0<=a[i]&&a[i]<=100)||(a[i]==(-1)))
{
if(a[i]!=(-1))
{
a[i]=a[i]+factor;
}
else return 1;
}
}
return 0;
}
float calc_avg_values (float a[])
{
int count=0;
float sum=0,avg=0;
while ((a[count]!=(-1))&&(count<MAX_IN_LEC))
{count++;
sum=sum+a[count];}
avg=sum/(count+1);
return avg;
}
int avg_lec_grades (float a[],float b[])
{
int i=0;
for (i=0;i<LECTURES_NUM;i++)
{
b[i]=calc_avg_values(&a[i]);
}
return 0;
{

int main()
{
/*init variables*/
int i, j, retval;
float grades[LECTURES_NUM][MAX_IN_LEC];
float avg_grades[LECTURES_NUM];
for (i=0; i < LECTURES_NUM; i++)
for (j=0; j < MAX_IN_LEC; j++)
grades[i][j] = -1;
/*enter the grades matrix*/
for (i=0; i < LECTURES_NUM; i++){
printf("Please enter the grades for lecture number %d: \n", i);
scanf("%f", &grades[i][0]);
if (grades[i][0] == -1)
return 1;
for (j=1; j < MAX_IN_LEC; j++)
if (getchar() == '\n')
break;
else
scanf("%f", &grades[i][j]);
}
/*update factor*/
retval = update_factor(grades);
if (retval){
printf("Invalid input!");
return retval;
}
/*call the averaging function*/
retval = avg_lec_grades(grades, avg_grades);
if (retval){
printf("Error occured in the avg_lec_grades(...) function");
return retval;
}
/*print the averaged scores per lecture*/
for (i=0; i < LECTURES_NUM; i++)
printf("The averaged score for lecture#%d is: %.2f \n", i, avg_grades[i]);
return 0;
}
In the future, please use the code tags to insert code. The reason you have the syntax error is that your code is a little messy, and so it is hard to check that all the brackets line up. This is even harder for someone who doesn't know your code and is looking at plain text with all formatting removed. I did however find the error. The bracket right before int main() is "{" when it should be "}" to close off the previous function. You then have a pointer error, but if you have trouble finding that problem after applying my syntax fix, please repost the code with the proper tags so I can better see what you're trying to do. Thanks.
Last edited on
first tnx
it dide solved the problem
i pot it in the way u asked
i just didnt knew that i should do it this way
if u can help me with pointer problem cause this is what im stuck on
thanks allot


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#define MAX_IN_LEC 20
#define LECTURES_NUM 4

float get_max_value (float a[])
{
float max=0;
int i;
for (i=0;i<LECTURES_NUM*MAX_IN_LEC;i++)
{if (max<a[i])
    {max=a[i];}
}

return max;
}

int update_factor (float a[])
{
    int i;
    float factor,max;
    max=get_max_value(a);
    factor=100-max;
    for(i=0;i<LECTURES_NUM*MAX_IN_LEC;i++)
    {
    if ((0<=a[i]&&a[i]<=100)||(a[i]==(-1)))
    {
    if(a[i]!=(-1))
    {
        a[i]=a[i]+factor;
    }
    else return 1;
    }
    }
    return 0;
}
float calc_avg_values (float a[])
{
    int count=0;
    float sum=0,avg=0;
    while ((a[count]!=(-1))&&(count<MAX_IN_LEC))
    {count++;
     sum=sum+a[count];}
     avg=sum/(count+1);
     return avg;
}
    int avg_lec_grades (float a[],float b[])
    {
        int i=0;
        for (i=0;i<LECTURES_NUM;i++)
        {
            b[i]=calc_avg_values(&a[i]);
        }
            return 0;
    }

int main()
{
/*init variables*/
int i, j, retval;
float grades[LECTURES_NUM][MAX_IN_LEC];
float avg_grades[LECTURES_NUM];
for (i=0; i < LECTURES_NUM; i++)
for (j=0; j < MAX_IN_LEC; j++)
grades[i][j] = -1;
/*enter the grades matrix*/
for (i=0; i < LECTURES_NUM; i++){
printf("Please enter the grades for lecture number %d: \n", i);
scanf("%f", &grades[i][0]);
if (grades[i][0] == -1)
return 1;
for (j=1; j < MAX_IN_LEC; j++)
if (getchar() == '\n')
break;
else
scanf("%f", &grades[i][j]);
}
/*update factor*/
retval = update_factor(grades);
if (retval){
printf("Invalid input!");
return retval;
}
/*call the averaging function*/
retval = avg_lec_grades(grades, avg_grades);
if (retval){
printf("Error occured in the avg_lec_grades(...) function");
return retval;
}
/*print the averaged scores per lecture*/
for (i=0; i < LECTURES_NUM; i++)
printf("The averaged score for lecture#%d is: %.2f \n", i, avg_grades[i]);
return 0;
}
the variable grades is a 2d array (float a[][] == float *a[] == float **a), but the functions you are passing it to are looking for a 1d array (float a[] == float *a). You need to either edit the functions to take 2d arrays, or change grades to be a 1d array, depending on the implementation you're going for. Let me know if you need elaboration.

-Roj
i thought it will be like that
cant i threat the 2 d array as a long 1d array?
do i need to change the function or just edit the topics to get a 2d array?
chen
cant i threat the 2 d array as a long 1d array?


Sort of. In memory all the grades are stored as one long array of ints, but the way you set up the grades variable doesn't allow you to easily access that because the name of a 1d array (arr in arr[]) is a pointer to the first element, but the name of a 2d array (arrP in arrP[][]) is a pointer to a pointer to the first element. I see that your code does assume LECTURES_NUM*MAX_IN_LEC elements, as though the passed value is a 1d array of the 2d array, so you might want to simply make a 1d array from grades, but unless you understand pointers better than it seems, you might end up shooting yourself in the foot later on. But, for what it's worth,

1
2
gradesArr[LECTURES_NUM*MAX_IN_LEC];
gradesArr = *grades;


might work. My suggestion though is to read the tutorial on pointers http://www.cplusplus.com/doc/tutorial/pointers/ and make sure you can really draw out a map of what is happening to your stored grades in memory before proceeding.

-Roj
Last edited on
Here's more reading for you:

http://www.cplusplus.com/forum/articles/17108/

That article covers the different types of multidimentional arrays, how to use them, how to pass them between functions, how they relate with 1D arrays, and why MD arrays should be avoided.
Topic archived. No new replies allowed.