1. ## Cycle for looping

Hi, I have the following assignment:
I have to do a program that asks the name of the alumn, and then input 3 grades.
Once that is done, the program must calculate the average of each alumn and the average of each test.

I have done some code, it works, but the average calculation doesn't work.
Could you fix that for me please?
Kindly post the whole code fixed, and if you want, an explanation, so I can understand better.
Thanks :)

Code:
```#include <stdio.h> int main ()
{
char matrice[7][8];
int i, n;
int k;
int m;
n = 5;
m = 3;
int mat1[n][m];
int vet1[n];
int somma = 0;
int media;

for (i = 1; i <= n; i++) {
printf("\ninput name(%d): ", i);
scanf("%s", matrice);

printf("\n\n");
}

for (i = 1; i <= n; i++) {
printf("\nName %d: %s ", i, matrice);

for (k = 1; k <= m; k++) {

scanf("%d", &mat1[k]);
}
}

for (i = 1; i <= n; i++) {
printf("\nName %d: %s ", i, matrice);

printf("\n");
for (k = 1; k <= m; k++)
printf(" %d", mat1[k]);
}

//below does not work
for (i = 0; i < n; i++) {
for (i = 0; i < m; i++) {
somma = somma + matrice[k];
}
}
media = somma / (n * m);
printf("\nAverage:  = %s", media);

printf("\n\n");
getc;
}```
Somma = Sum
Media = Average
Matrice = Matrix

2. Originally Posted by d03nvc9
I have done some code, it works, but the average calculation doesn't work.
How does it not work? For example, what is your test input, expected output, and actual output? How did you arrive at the expected output from the test input (i.e., verify that you own calculations are correct)?

Code:
```\$ gcc -Wall bar.c
bar.c: In function ‘main’:
bar.c:17:11: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[8]’ [-Wformat=]
scanf("%s", matrice);
^
bar.c:23:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[8]’ [-Wformat=]
printf("\nName %d: %s ", i, matrice);
^
bar.c:28:13: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[(sizetype)(m)]’ [-Wformat=]
scanf("%d", &mat1[k]);
^
bar.c:33:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘char (*)[8]’ [-Wformat=]
printf("\nName %d: %s ", i, matrice);
^
bar.c:37:14: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf(" %d", mat1[k]);
^
bar.c:43:13: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
somma = somma + matrice[k];
^
bar.c:47:10: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=]
printf("\nAverage:  = %s", media);
^
bar.c:50:3: warning: statement with no effect [-Wunused-value]
getc;
^
bar.c:11:7: warning: unused variable ‘vet1’ [-Wunused-variable]
int vet1[n];
^```
> for (i = 1; i <= n; i++)
Arrays start at 0, not 1.
For an n element array, it's for (i = 0; i < n; i++)

4. Originally Posted by laserlight
How does it not work? For example, what is your test input, expected output, and actual output? How did you arrive at the expected output from the test input (i.e., verify that you own calculations are correct)?
5 names in input
John, Patrick, Ben, Marck, Andrew

And 3 grades for each person
6, 5, 8
9, 8, 6
6, 9, 9
7, 6, 7
5, 4, 7

And expect in output the average of each test, and for each student

5. Originally Posted by d03nvc9
5 names in input
John, Patrick, Ben, Marck, Andrew

And 3 grades for each person
6, 5, 8
9, 8, 6
6, 9, 9
7, 6, 7
5, 4, 7

And expect in output the average of each test, and for each student
expected output, and actual output?
Tim S.

6. Code:
```  //below does not work
for (i = 0; i < n; i++) {
for (i = 0; i < m; i++) {
somma = somma + matrice[k];
}
}```
Where does the value of k change in this loop code?
What is the starting value of k ?

Tim S.