¨Ubungspaket 11 Schleifen
¨Ubungspaket 11 Schleifen
¨Ubungspaket 11 Schleifen
Sie wollen auch ein ePaper? Erhöhen Sie die Reichweite Ihrer Titel.
YUMPU macht aus Druck-PDFs automatisch weboptimierte ePaper, die Google liebt.
1 # include <br />
2<br />
3 int main ( int argc , char ** argv )<br />
4 {<br />
5 int x;<br />
6<br />
7 // all numbers in one for loop<br />
8 for ( x = 0; x < 100; x = x + 1 )<br />
9 {<br />
10<br />
<strong>11</strong> // we print the number<br />
12 printf ( "%2d ", x );<br />
13<br />
14 // is a newline required ?<br />
15 if ( x % 10 == 9 )<br />
16 printf ( "\n" );<br />
17 }<br />
18 return 0; // done<br />
19 }<br />
Alternativ bietet es sich auch an, die inherente 10×10 Struktur der Matrix direkt<br />
in das C-Programm umzusetzen. Dies geht direkt mit zwei verschachtelten for-<br />
<strong>Schleifen</strong>.<br />
1 # include <br />
2<br />
3 int main ( int argc , char ** argv )<br />
4 {<br />
5 int i, j;<br />
6<br />
7 // it ’s 10 lines ...<br />
8 for ( i = 0; i < 10; i = i + 1 )<br />
9 {<br />
10<br />
<strong>11</strong> // ... by 10 columns<br />
12 for ( j = 0; j < 10; j = j + 1 )<br />
13 printf ( "%2d ", i * 10 + j );<br />
14<br />
15 // finish the current line<br />
16 printf ( "\n" );<br />
17 }<br />
18 return 0; // done<br />
19 }<br />
Einführung in die Praktische Informatik, Wintersemester 2013/14 <strong>11</strong>-14