以行的方式:
1
#include
<
stdio.h
>
2
#include
<
stdlib.h
>
3
#define
ROW 6
4
#define
column 5
5
6
int
main(
int
argc,
char
*
argv[])
7
{
8
int
class
[
30
]
=
{
0
,
2
,
0
,
2
,
0
,
9
1
,
4
,
1
,
4
,
1
,
10
5
,
0
,
5
,
0
,
5
,
11
0
,
0
,
0
,
0
,
0
,
12
3
,
0
,
3
,
0
,
3
,
13
0
,
0
,
0
,
0
,
0
,};
14
int
sum;
15
int
i,j;
16
sum
=
0
;
17
for
(i
=
0
; i
<
6
; i
++
)
18
{
19
for
(j
=
0
; j
<
5
; j
++
)
20
{
21
if
(
class
[i
*
column
+
j]
!=
0
)
22
{sum
++
;}
23
}
24
}
25
printf(
"
课程总数: %d\n
"
,sum);
26
system(
"
PAUSE
"
);
27
return
0
;
28
}
29
是5*6的表格数据,用一维数组存储。遍历,还是二维数组的遍历方式。第一个循环是行的循环,第二个是列的循环。
下面是列为主的代码:
1
#include
<
stdio.h
>
2
#include
<
stdlib.h
>
3
#define
ROW 6
4
#define
column 5
5
6
int
main(
int
argc,
char
*
argv[])
7
{
8
int
class
[
30
]
=
{
0
,
2
,
0
,
2
,
0
,
9
1
,
4
,
1
,
4
,
1
,
10
5
,
0
,
5
,
0
,
5
,
11
0
,
0
,
0
,
0
,
0
,
12
3
,
0
,
3
,
0
,
3
,
13
0
,
0
,
0
,
0
,
0
,};
14
int
sum;
15
int
i,j;
16
sum
=
0
;
17
for
(i
=
0
; i
<
6
; i
++
)
18
{
19
for
(j
=
0
; j
<
5
; j
++
)
20
{
21
if
(
class
[j
*
ROW
+
i]
!=
0
)
22
{sum
++
;}
23
}
24
}
25
printf(
"
课程总数: %d\n
"
,sum);
26
system(
"
PAUSE
"
);
27
return
0
;
28
}
29