שפת C/מערכים/תרגילים: הבדלים בין גרסאות בדף

תוכן שנמחק תוכן שנוסף
Atavory (שיחה | תרומות)
שורה 288:
 
===הדפסת ערכי מטריצה (מערך דו-מימדי) בצורה מעגלית===
 
{{שקול לדלג|סיבה = פתרון התרגיל נעשה באמצעות שימוש ב[[שפת C/הקדם מעבד|קדם מעבד]].}}
 
כתבו תוכנית שנתונה לה מטריצה בגודל מסוים (4 על 3), ועליה להדפיס אותה בצורה מעגלית. לדוגמה, אם נתון המערך הבא:
<source lang = "c">
שורה 314 ⟵ 315:
#include <stdio.h>
 
#defineconst int UP = 0;
#defineconst int RIGHT = 1;
#defineconst int DOWN = 2;
#defineconst int LEFT = 3;
 
int main()
שורה 345 ⟵ 346:
{
printf("%c ", arr[x][y]);
switchif(dir == UP){
case UP:{
//move to direction
x--;
//if we are on the limit: move limit one step to center & change direction
if(x==u) {
{
u++;
dir=(dir+1)%4;
}
break;}
else caseif (dir == RIGHT:)
{
//move to direction
y++;
שורה 363 ⟵ 366:
dir=(dir+1)%4;
}
break;}
else caseif(dir == DOWN:)
{
//move to direction
x++;
שורה 372 ⟵ 376:
dir=(dir+1)%4;
}
break;}
else if(dir case== LEFT:)
{
//move to direction
y--;
שורה 381 ⟵ 386:
dir=(dir+1)%4;
}
break;}
}
}
return 0;