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

תוכן שנמחק תוכן שנוסף
Atavory (שיחה | תרומות)
אין תקציר עריכה
Atavory (שיחה | תרומות)
מאין תקציר עריכה
שורה 8:
<pre style="text-align:left; direction:ltr;">
char matrix[4][3]=
{ {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}};
</pre>
היא תדפיס:
שורה 28:
{
char matrix[4][3]=
{ {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}};
int i, j;
שורה 34:
{
for(j = 0; j < 3; ++j)
printf("%c ", matrix[i][j]);
printf("\n");
}
return 0;
}
שורה 126:
<pre style="text-align:left; direction:ltr;">
char matrix[4][3]=
{ {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}};
</pre>
שנראה כך:
שורה 156:
int main()
{
int dir; //direction
int x,y; //posiotion
int u,r,d,l; //limits: up, right, down, left
int count; //just counts the cells that printed
 
char arr[4][3]=
{ {'1','2','3'} , {'4','5','6'} , {'7','8','9'} , {'a','b','c'}};
 
//at first, direction set to be right
dir=RIGHT;
 
//we start at this corner
x=0;
y=0;
 
//at first, limits are edges of array
u=1;
r=3-1;
d=4-1;
l=0;
 
for(count=0;count<3*4;count++)
{
printf("%c ", arr[x][y]);
switch(dir){
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;
case RIGHT:
//move to direction
y++;
//if we are on the limit: move limit one step to center & change direction
if(y==r) {
r--;
dir=(dir+1)%4;
}
}
break;
case DOWN:
//move to direction
x++;
//if we are on the limit: move limit one step to center & change direction
if(x==d) {
d--;
dir=(dir+1)%4;
}
}
break;
case LEFT:
//move to direction
y--;
//if we are on the limit: move limit one step to center & change direction
if(y==l) {
l++;
dir=(dir+1)%4;
}
}
break;
}
}
}
return 0;
}
</pre>