Using Arrays to get gpa

Aug 8, 2012 at 7:38pm
Hi, Im new to arrays and Im having a little problem with them. I wrote a program that finds your grade, it asks how many hw/cw/qz etc and then if you have 2 hws it calculates that etc. Im trying to use arrays to ask the user for each homework grade (dont mind the cw and qz, im using loops to list the grades)

when i compile i keep getting

error: expected expression before â]â token at line 23 and 24, can anyone tell me what am i missing



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
#include <stdio.h>


void PrintGreetings()
{
 printf("Final grade is ");
}

int calc(int grade, int h, int c, int q, int f);


int main()
{

 int hn[12], cn, qn, fn;
 int h, c, q, f;
 int s;
 int grade;


 printf("how many homeworks: ");
 scanf("%d", &hn[]);
 for(s=1; s<=hn[]; s++)
 {
        printf("enter homework grade:");
        scanf("%d", &h);
 }

 printf("how many classworks: ");
 scanf("%d", &cn);
 for(s=1; s<=cn; s++)
 {
        printf("enter classwork grade:");
        scanf("%d", &c);
 }

 printf("how many quizzes: ");
 scanf("%d", &qn);
 for(s=1; s<=qn; s++)
 {
        printf("enter quiz grade:");
        scanf("%d", &q);
 }

 printf("enter 1 if the final is completed: ");
 scanf("%d", &fn);
 if(fn == 1)
 {
        printf("enter grade:");
        scanf("%d", &f);
 }

 else if(fn != 1)
 {
 printf("you have no final exam grade\n");
 }

 grade = calc(0, h, c, q, f);
 printf("final grade: %d\n", grade);



 return 0;
}



int calc(int grade, int h, int c, int q, int f)
{
 grade= .16*c + .24*h + .3*q + .3*f;

 PrintGreetings();

 return grade;
}



Last edited on Aug 8, 2012 at 7:44pm
Aug 8, 2012 at 7:45pm
These lines are invalid:
1
2
 scanf("%d", &hn[]);
 for(s=1; s<=hn[]; s++)


When dealing with arrays, you need to say which index in the array you are using, so you'd want something like this:
1
2
3
4
5
6
7
printf("how many homeworks: ");
 scanf("%d", &h);
 for(s=1; s<=h; s++)
 {
        printf("enter homework grade:");
        scanf("%d", &hn[s]);
 }


Note that I am now using s as the index and writing to each element of the array individually.
Aug 8, 2012 at 7:54pm
Thanks so much.
Aug 8, 2012 at 8:18pm
I think when you used this line scanf("%d", &hn[]);, you got mixed up with c language, where you need the '%d' to write a variable. What I think you should do is write scanf(&hn[]). Please correct me if I am wrong.
Topic archived. No new replies allowed.