110 likes | 285 Views
אתחול מערך. void main() { int i; int ar1[5]={7,3,4,5,2}; int ar2[]={9,3,3,4,6}; /*the size is 5*/ int ar3[5]={3,2}; /*the other nums are 0*/ int ar4[3]; /*garbage*/ int ar5[100]; /*garbage*/ ar4[0]=12; ar4[1]=2; ar4[2]=9; for (i=0;i<100;i++) ar5[i]=i+5; }.
E N D
אתחול מערך void main() { int i; int ar1[5]={7,3,4,5,2}; int ar2[]={9,3,3,4,6};/*the size is 5*/ int ar3[5]={3,2};/*the other nums are 0*/ int ar4[3];/*garbage*/ int ar5[100];/*garbage*/ ar4[0]=12; ar4[1]=2; ar4[2]=9; for (i=0;i<100;i++) ar5[i]=i+5; }
0 1 2 3 4 5 void main() { int nums[6]; 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 8 8 11 11 ?
Selection sort 5 1 2 7 3 3 5 5 1 1 9 7 2 2 7 7 9
#include <stdio.h> void main() { int i, j, min, temp, nums[10]; for (i=0; i<10; i++){ /*input*/ printf ("enter number\n"); 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 ("%d\n",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 number\n"); 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 ("%d\n",nums[i]); }
מערך דו מימדי #include <stdio.h> void main() { int i,j; int ar1[3][2]={{1,2},{9,4},{14,6}}; int ar2[][2]={{5,6},{7,4},{14,3}}; int ar3[4][3]={{2},{1,1,1},{1,4,6}}; int ar4[3][5]; for (i=0;i<3;i++) for (j=0;j<5;j++) scanf("%d",&ar4[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,i1,j1, revealed=0; int board[ROWS][COLS]; int playerBoard[ROWS][COLS]; srand((unsignedint)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; playerBoard[i][j]=9; } }
while (revealed <ROWS*COLS - mines){ for (i=0;i<ROWS;i++){ for (j=0;j<COLS;j++) printf ("%d",playerBoard[i][j]); printf("\n"); } printf ("enter row and column number\n"); scanf("%d%d",&i1,&j1); if (board[i1][j1]==1) break; if (playerBoard[i1][j1]!=9) continue; revealed++; playerBoard[i1][j1]=0; for (i=max(0,i1-1); i<=min(i1+1,ROWS-1); i++) for (j=max(0,j1-1);j<=min(COLS-1,j1+1); j++) if (board[i][j]) playerBoard[i1][j1]++; }
שולה המוקשים: תוצאת המשחק if (revealed<ROWS*COLS - mines) printf("KABOOM!!!\n"); else printf("You won!!!\n"); }