Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 Introduction to Programming in C תרגול 5 22.03.2011.

Similar presentations


Presentation on theme: "11 Introduction to Programming in C תרגול 5 22.03.2011."— Presentation transcript:

1 11 Introduction to Programming in C תרגול 5 22.03.2011

2 מטרת התרגול מיון בועות מערכים דו-ממדיים ומחרוזות. 2

3 תזכורת מערכים הגדרת מערך עם שלושה תאים: int nums[3]; פנייה לתא במערך ע " י האינדקס של התא : nums[0]=1; nums[1]=3; nums[2]=nums[1]+nums[0]; 3

4 מיון בועות כתבו תוכנית אשר קולטת מערך של עד LEN מספרים וממיינת אותו בעזרת מיון בועות. באופן הבא : 1)Nums  קלוט LEN מספרים 2)i  LEN - 1 3) כל עוד i > 1, בצע : 1)j  0 2) כל עוד j < i, בצע : 1) אם Nums[J] > Nums[J+1] אז : החלף Num[j]  Num[j+1] 2)j  j + 1 3)i  i - 1 4

5 5 int i, j, len = 0, a[LEN], isLastValue = 0; // Get number until -1 is entered printf(“Enter numbers (-1 to finish): “); while ( (len < LEN) && (!isLastValue) ) { scanf(“%d”, &a[len]); if (a[len] == -1) isLastValue = 1; len++; } // Sort array using bubble sort for (i = len - 1; i > 1; i--) for (j = 0; j < i; j++) if (a[j] > a[j + 1]) { int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } // Print sorted array for (i = 0; i < len; i++) printf(“%d, “, a[i]);

6 ASCII Table

7 צריך לממש את התוכנית הבאה: –קלט: מערך דו-ממדי המכיל תווים (chars), ומילה המורכבת מתווים אשר אורכה קצר משני ממדי המערך. הקלט נתון (Hardcoded), אין צורך לקלוט אותו ממשתמש. –פלט: הדפסת כל המופעים של המילה במערך הדו-ממדי, כאשר המילה יכולה להופיע בשורה (horizontally) או בעמודה (vertically) חלוקת עבודה לשלבים: –נבין כיצד למצוא את כל המופעים בשורות. –נבין כיצד למצוא את כל המופעים בעמודות. האם יש דמיון למציאה עבור שורות. תרגיל 1- מערכים דו - ממדיים

8 #include #define ROW 5 #define COL 5 #define WORDSIZE 3 void main() { char matr[ROW][COL]= {{'r','v','o','q','w'}, { 'a','h','s','x','l'}, {'n','k','s','d','m'}, { 'r','a','n','j','r'}, { 'd','k','u','c','a'}}; char word[WORDSIZE+1]="ran";//Leave room for the ‘\0’ character. int i,j,k,l; פתרון תרגיל 1

9 // Search for horizontal words (along the rows): for (i=0; i<ROW; i++) //Scan the rows. for(j=0; j<=COL-WORDSIZE; j++) //Scan the columns. { //Scan the word if it is there. for(k=j, l=0; l<WORDSIZE && matr[i][k]==word[l]; k++, l++); if (l==WORDSIZE) printf("The word was found horizontally!!! Its coordinates are: x1=%d, y1=%d, x2=%d, y2=%d\n", i, k- WORDSIZE,i,k-1); } פתרון תרגיל 1 - שורות

10 //Search for vertical words (along the columns): for(i=0; i<COL; i++) //Scan the columns: for(j=0; j<=ROW-WORDSIZE; j++) //Scan the rows: { for(k=j, l=0;l<WORDSIZE && matr[k][i]==word[l]; k++, l++); if(l==WORDSIZE) printf("The word was found vertically!!! Its coordinates are: x1=%d, y1=%d, x2=%d, y2=%d\n", k-WORDSIZE,i,k-1,i); } פתרון תרגיל 1 - עמודות

11 פתרון תרגיל 1 - פלט RVOQW AHSXL NKSDM RANJR DKUCa The word was found horizontally!!! Its coordinates are: x1=0, y1=0, x2=0, y2=2“ The word was found vertically!!! Its coordinates are: x1=3, y1=0, x2=3, y2=2

12 תרגיל 2 הדפסת ערכי מטריצה (מערך דו-מימדי) בצורה מעגלית כתבו תוכנית שנתונה לה מטריצה בגודל מסוים (4 על 3), ועליה להדפיס אותה בצורה מעגלית. לדוגמה, אם נתון המערך הבא: char matrix[4][3]= { {'1','2','3'}, {'4','5','6'}, {'7','8','9'}, {'a','b','c'}}; היא תדפיס: 1 2 3 6 9 c b a 7 4 5 8 123 456 789 ‘a’‘b’‘c’

13 פתרון תרגיל 2 #include #define UP 0 #define RIGHT 1 #define DOWN 2 #define LEFT 3 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; 13

14 פתרון תרגיל 2 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;

15 פתרון תרגיל 2 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; 15

16 פתרון תרגיל 2 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; } 16

17 להלן אלג' פסודו-קוד עבור מחרוזת W: –עבור על כל אינדקס iמ-0 עד אורך מחרוזת נתונה W – עבור על כל אינדקס j מ-0 עד אורך מחרוזת נתונה W – אם W[i] = W[j] הדפס את W[i]. מה עושה האלג'? מוצא אותיות שמופיעות במילה יותר מפעם אחת. מה יודפס בהינתן המלה “abc” ? תשובה “abc” –כיצד נתקן את האלג' ? מה יודפס בהינתן המילה “koshka” ? תשובה "kk” –כיצד נתקן את האלג' ? תרגיל 3

18 הייצוג של תווים כ- char-ים הוא ע"י תווי ascii. –לדוגמא, ייצוג ascii של ‘a’ הוא 97, ועל כן (int) ‘a’ == 97. כל האותיות באנגלית מופיעות באופן עוקב ב- ascii. –ערך ‘b’ הוא 98, ערך ‘c’ הוא 99 וכן הלאה. נוכל לנצל תכונה זו על מנת לבצע השוואות ואריתמטיקה של תווים. דוגמא: if (c >= ‘a’ && c <= ‘z’) printf(“%c is an alphabetic character\n”, c); דוגמא שקולה: If (c – ‘a’ >= 0 && c – ‘z’ <= 0) printf(“%c is an alphabetic character\n”, c); ASCII codes

19 פלינדרום הוא מילה שנקראת באופן זהה כאשר קוראים אותה מן הסוף להתחלה. דוגמאות לפלינדרומים: –“a” –“aba” –“a man, a plan, a canal – panama” (בהתעלם מסימני פיסוק ורווחים) צריך לממש את התוכנית הבאה: –קלט: מחרוזת מן המשתמש. –פלט: הודעה האם המחרוזת היא פלינדרום או לא. תרגיל 4 - פולינדרום

20 #include void main() { int i,len=-1; char w1[256]; printf(“Enter String: ”); scanf(“%s”, w1); // Calc length while (w1[++len] != ‘\0’); for (i=0 ; i < len/2 && w1[i] == w1[len-i-1] ; i ++); if (i==len/2) printf("The word is a palindrome! \n"); else printf("The word isn't a palindrome! \n"); } פתרון תרגיל 4

21 השוואה לקסיקוגרפית (מילונית) היא כזו שמשווה מילים לפי סדר הופעתם במילון. צריך לממש את התוכנית הבאה: –קלטים: 2 מחרוזות מהמשתמש. –פלט: הודעה שמבהירה איזו משתי המחרוזות גדולה יותר לקסיקוגרפית. תרגיל 5 – השוואה לקסיקוגרפית

22 #include #define MAX_WORD_LEN 256 void main() { int i; char w1[MAX_WORD_LEN], w2[MAX_WORD_LEN]; printf("Please enter first word:"); scanf(“%s”, w1); printf("Please enter second word:"); scanf(“%s”, w2); for (i=0 ; w2[i] && w1[i] == w2[i] ; i++); if (!w1[i] && !w2[i]) printf("equal\n"); else if (w1[i] > w2[i]) printf("first bigger\n"); elseprintf("last bigger\n"); } פתרון תרגיל 5

23 צריך לממש את התוכנית הבאה: –קלט: מחרוזת שמייצגת משפט. –פלט: אותה מחרוזת, כאשר כל מילה מתחילה באות גדולה. זכרו- ניתן לנצל את התכונות האריתמטיות של התווים. ההפרש בין כל אות גדולה ואות קטנה בקוד ascii הוא קבוע. תרגיל 6

24 #include void main() { int i,len; char w1[256]; printf("Please enter a sentence:\n"); scanf(“%s”, w1); for (i=0; w1[i] ; i++) if (! i || w1[i-1]==' ') if (w1[i] >='a' && w1[i]<='z') w1[i] += 'A' - 'a';//This is actually subtracting 32 from w1[i]. printf("%s\n",w1); } פתרון תרגיל 6

25 צריך לממש את התוכנית הבאה: –קלט: מחרוזת שמייצגת משפט המכיל 5 מילים. –פלט: המילה הארוכה ביותר במשפט. זכרו: גם מחרוזת היא מערך. כמו שיכולנו לעבוד על מערך באמצעות לולאות מקוננות, ניתן לבצע פעולה כזו גם על מחרוזות. תרגיל 7

26 #include void main() { int i=0,len,maxWordLoc, maxWordLen=0, curWordLen=0; char w1[256]; printf("Please enter a sentence consisting of 5 words:\n"); scanf(“%s”, w1); do{ if (w1[i] == ' ' || w1[i] =='\0') { if (curWordLen>maxWordLen) { maxWordLen = curWordLen; maxWordLoc = i; } curWordLen = 0; } else curWordLen++; } while(w1[i++]); פתרון תרגיל 7

27 while (maxWordLoc && w1[maxWordLoc-1] !=' ') maxWordLoc--; while (w1[maxWordLoc] && w1[maxWordLoc]!=' ') putchar(w1[maxWordLoc++]); } פתרון תרגיל 7

28 תרגיל 8: מעקב #define BUFF_SIZE 256 void main() { int i = -1, x = 0; char s[BUFF_SIZE]; printf(“Enter a string: ”); gets(s); while (s[++i] != '\0') if (s[i] >= '0' && s[i] <= '9') { x *= 10; x += s[i] - '0'; } i = -1; while (s[++i] != '\0') if (s[i] >= '0' && s[i] <= '9') { s[i] = x % 10 + '0'; x /= 10; } puts(s); }


Download ppt "11 Introduction to Programming in C תרגול 5 22.03.2011."

Similar presentations


Ads by Google