Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 5 08.11.2010.

Similar presentations


Presentation on theme: "1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 5 08.11.2010."— Presentation transcript:

1 1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 5 08.11.2010

2 מטרת התרגול מערכים מחרוזות 2 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

3 דוגמא: מערך של מספרים התוכנית הבאה ( עמוד הבא ) קולטת סדרה של 20 מספרים שלמים לתוך מערך ומחשבת את הממוצע שלהם 3 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

4 C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel 4 #define MAX_LEN 20 void main() { int i, sum =0; int nums[MAX_LEN]; // Get numbers printf(“Enter %d numbers: “, MAX_LEN); for (i = 0; i < MAX_LEN; i++) scanf(“%d”, &nums[i]); // Calc average for (i = 0; i < MAX_LEN; i++) sum += nums[i]; printf(“Sum = %.2f”, (float)sum / MAX_LEN); }

5 דוגמא 2: אורך מחרוזת התוכנית הבאה קולטת מחרוזת ומדפיסה את אורכה 5 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel #define BUFF_SIZE 256 void main() { int len = -1; char s[BUFF_SIZE]; printf(“Enter String: ”); scanf(“%s”, s); // Calc length while (s[++len] != ‘\0’); printf(“Length = %d”, len); }

6 תרגיל 1: השוואת מחרוזות כתבו תוכנית אשר קולטת שתי מחרוזות ובודקת אם הן שוות 6 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

7 C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel 7 #define BUFF_SIZE 256 void main() { int i; char s1[BUFF_SIZE], s2[BUFF_SIZE]; printf/scanf // Run as long strings match or (at least) one string ends for (i = 0; s1[i] == s2[i] && s1[i] != ‘\0’; i++); // Current value indicate whether strings equal if (s1[i] != s2[i]) printf(“Not equal”); else printf(“Equal”); }

8 תרגיל 2: הפיכת מחרוזת כתבו תוכנית אשר קולטת מחרוזת והופכת אותה 8 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

9 C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel 9 int left, right; char s[BUFF_SIZE]; printf/scanf // Get initial left & right positions left = 0; right = -1; while (s[++right] != ‘\0’) ; // Reverse string for (right--; left < right; left++, right--) { char temp; // Swap current left & right elements temp = s[left]; s[left] = s[right]; s[right] = temp; }

10 תרגיל 3: מיון בועות כתבו תוכנית אשר קולטת מערך של עד 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 10 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

11 C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel 11 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]);

12 תרגיל 4: מעקב 12 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel #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); }

13 תרגיל 5: lower case כתבו תוכנית אשר קולטת מחרוזת ומחליפה כל אות גדולה באות קטנה. שימו לב שלפי טבלת ה -ASCII ( עמוד הבא ), מתקיים : ‘b’ = ‘B’ + ‘a’ – ‘A’ 13 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel #define BUFF_SIZE 256 void main() { int i = -1; char s[BUFF_SIZE]; printf/scanf // Replace each upper case letter with lower case while (s[++i] != ‘\0’) if (s[i] >= ‘A’ && s[i] <= ‘Z’) s[i] += ‘a’ – ’A’; printf(“%s”, s); }

14 ASCII Table


Download ppt "1 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול 5 08.11.2010."

Similar presentations


Ads by Google