1 / 20

Introduction to Programming in C

Introduction to Programming in C. תרגול 4. 13.03.2011. 1. מטרת התרגול. לולאות מערכים מחרוזות. Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel. תרגיל 1 : n!. כתבו תוכנית אשר קולטת שלם n ומחשבת את העצרת של n : 1 * 2 * … * n ) השתמשו בלולאת (for

dani
Download Presentation

Introduction to Programming in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming in C תרגול 4 13.03.2011 1

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

  3. תרגיל 1: n! • כתבו תוכנית אשר קולטת שלם n ומחשבת את העצרת של n: 1 * 2 * … * n)השתמשו בלולאת (for void main() { int n, i, fac = ?; printf/scanf for (i = 1; i <= n ; i++) fac *= i; printf } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

  4. תרגיל 2: מעקב • עקבו אחר מהלך התוכנית ונסו להבין מה היא מבצעת: void main() { int x, y, something = 0; printf(“Enter two numbers”); scanf(“%d%d”, &x, &y); while (x-- > 0) something++; for (;y > 0; y--, something--); printf(“%d”, something); }

  5. תרגיל 3: • כתבו תוכנית אשר קולטת שני שלמים x ו-y ומחשבת את x בחזקת y (השתמשו בלולאה כרצונכם) void main() { int x, y, pow = 1; printf/scanf while (y-- > 0) { pow *= x; } printf } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

  6. תרגיל 4: לולאה כפולה • כתבו תוכנית שקולטת שלם n ומדפיסה ריבועnxn של כוכביות int n, i; printf/scanf printf(“\n”); for (i = 0; i < n ; i++) { int j; for (j = 0; j < n ; j++) printf(“*”); printf(“\n”); } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

  7. תרגיל 5: טור לייבניץ • כתבו תכנית המחשבת את ערכו המקורב של PI (3.141592653) באמצעות טור לייבניץ ברמת הדיוק של 0.00001 δ = . טור לייבניץ הינו: Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

  8. Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

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

  10. int num, reversed_num = 0, sign = 1; // Get number printf/scanf // Treat negative number as positive and remember sign if (num < 0) } sign = -1; num = -num; } // Reverse number while (num > 0) { reversed_num = reversed_num * 10 + num % 10; num /= 10; } printf(“Reversed = %d\n”, reversed_num *= sign); Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

  11. מערכים • הגדרת מערך עם שלושה תאים: int nums[3]; • פנייה לתא במערך ע"י האינדקס של התא: nums[0]=1; nums[1]=3; nums[2]=nums[1]+nums[0]; C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel

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

  13. #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); • } C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel

  14. דוגמא 2: אורך מחרוזת • התוכנית הבאה קולטת מחרוזת ומדפיסה את אורכה • #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); • } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel

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

  16. #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”); • } C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel

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

  18. 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; • } C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel

  19. הערה לגבי תרגילי בית • פונקציית main int main(){ . // enter your code here… . return 0; } C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel

  20. תרגול הבא • מערכים דו מיימדיים • מיונים C Programming Intoduction - Fall 2011 - Erez Sharvit, Amir Menczel

More Related