void main int i int ar 157 3

• Slides: 11

אתחול מערך void main() { int i; int ar 1[5]={7, 3, 4, 5, 2}; int ar 2[]={9, 3, 3, 4, 6}; /*the size is 5*/ int ar 3[5]={3, 2}; /*the other nums are 0*/ int ar 4[3]; /*garbage*/ int ar 5[100]; /*garbage*/ ar 4[0]=12; ar 4[1]=2; ar 4[2]=9; for (i=0; i<100; i++) ar 5[i]=i+5; }

void main() { 1 int nums[6]; 0 int a=8, b=3; nums[0]=5; nums[2]=a; nums[5]=a+b; nums[b]=nums[2]; nums[4]=nums[a-b]; nums[a+b]=9; 5 ? } 2 3 4 5 8 8 1111

Selection sort 5 2 1 7 3 5 1 97 9 2 7

#include <stdio. h> void main() { int i, j, min, temp, nums[10]; for (i=0; i<10; i++){ /*input*/ printf ("enter numbern"); scanf ("%d", &nums[i]); } for (i=0; i<9; i++){ /*main loop*/ min = i; for (j=i+1; j<10; j++) /*finding minimum index*/ if (nums[j]<nums[min]) min = j; temp = nums[i]; /*swaping*/ nums[i] = nums[min]; nums[min] = temp; } for (i=0; i<10; i++) /*printing*/ printf ("%dn", nums[i]); }

Insertion sort 7 3 5 1 9 2

#include <stdio. h> void main() { int i, j, temp, nums[10]; for (i=0; i<10; i++){ /*input*/ printf ("enter numbern"); scanf ("%d", &nums[i]); } for (i=0; i<10; i++){ /*main loop*/ /*finding location of nums[j]*/ for (j=i; j>0 && nums[j]<nums[j-1]; j--){ temp = nums[j]; /*swaping*/ nums[j] = nums[j-1]; nums[j-1] = temp; } } for (i=0; i<10; i++) /*printing*/ printf ("%dn", nums[i]); }

מערך דו מימדי #include <stdio. h> void main() { int i, j; int ar 1[3][2]={{1, 2}, {9, 4}, {14, 6}}; int ar 2[][2]={{5, 6}, {7, 4}, {14, 3}}; int ar 3[4][3]={{2}, {1, 1, 1}, {1, 4, 6}}; int ar 4[3][5]; for (i=0; i<3; i++) for (j=0; j<5; j++) scanf("%d", &ar 4[i][j]); }

הגדרות משתנים : שולה המוקשים #include <stdlib. h> #include <time. h> #include <stdio. h> #define ROWS 10 #define COLS 10 void main() { int i, j, number, mines=0, i 1, j 1, revealed=0; int board[ROWS][COLS]; int player. Board[ROWS][COLS]; srand((unsigned int)time(NULL));

אתחול הלוח : שולה המוקשים for (i=0; i<ROWS; i++){ for (j=0; j<COLS; j++){ number=(int)((double)rand()/(RAND_MAX+1)*10); if (number<1){ board[i][j]=1; mines++; } else board[i][j]=0; player. Board[i][j]=9; } }

while (revealed <ROWS*COLS - mines){ for (i=0; i<ROWS; i++){ for (j=0; j<COLS; j++) printf ("%d", player. Board[i][j]); printf("n"); } printf ("enter row and column numbern"); scanf("%d%d", &i 1, &j 1); if (board[i 1][j 1]==1) break; if (player. Board[i 1][j 1]!=9) continue; revealed++; player. Board[i 1][j 1]=0; for (i=max(0, i 1 -1); i<=min(i 1+1, ROWS-1); i++) for (j=max(0, j 1 -1); j<=min(COLS-1, j 1+1); j++) if (board[i][j]) player. Board[i 1][j 1]++; }

תוצאת המשחק : שולה המוקשים if (revealed<ROWS*COLS - mines) printf("KABOOM!!!n"); else printf("You won!!!n"); }