for function

want to summarize all the test[0][x] and test[1][x]
i have to use "for" or "while" but i thought that for would be easier.
unfortunately i got stuck. the test[0][5] and test[1][5] are for the result.

i really have no idea how to summarize them.

i hope someone can help me

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
 #include <stdio.h>
#define SUB 2
#define NUM 6

	int main(void)
{
	int test[SUB][NUM];
	int i;
	
	test[0][0] = 80;
	test[0][1] = 60;
	test[0][2] = 22;
	test[0][3] = 50;
	test[0][4] = 75;
	test[0][5] = 0 ;
	test[1][0] = 90;
	test[1][1] = 55;
	test[1][2] = 68;
	test[1][3] = 72;
	test[1][4] = 58;
	test[1][5] = 0;

	for(i=0; i<4; i++){
	printf("%d 番目の人の国語の点数は%d です。\n", i+1, test[0][i]);
	printf("%d 番目の人の算数の点数は%d です。\n", i+1, test[1][i]);
}
// the problem is here
	for(i=0; i<=4 ; i++);{
	test[0][5] = test[0][5] + test[0][i]; }

	for(i=0; i<=4 ; i++);{
	test[1][5] = test[1][5] + test[1][i]; }

	printf("全員の国語の合計点数は%dです。\n", test[0][5]);
	printf("全員の算数の合計点数は%dです。\n" , test[1][5]);

	return 0;
}.
こんにちは,

The problem I see is in line 31.

It would look like this:
1
2
3
for (i = 0; i < NUM - 1; i++);

test[0][5] += test[0][i];



The ";" at the end of line 1 means that what follows is not part of the for loop. I compiled this as a C++ program, so if the "+=" does not work for you go back to what you started with.

The "#define"s work, but you should consider const int SUB = 2;. It does the same thing, but here you are sure that the variable is an "int".

Just so you know you could have done this:
1
2
3
4
5
int test[SUB][NUM]
{
    { 80, 60, 22, 50, 75, 0 },
    { 90, 55, 68, 72, 58 }
};

And eliminated lines 10 - 21.

Andy
Hello :D
ohh thank you very much for the fast reply.

こんにちは,

You are welcome. It is the little things that cause the most problems.

Andy
Topic archived. No new replies allowed.