160 likes | 307 Views
Introduction to Programming in C. תרגול 6. 15.11.2010. Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel. 1. 1. מטרת התרגול. מערכים דו מימדיים פונקציות. Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel. 2. מערכים דו-מימדיים.
E N D
Introduction to Programming in C תרגול 6 15.11.2010 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 1
מטרת התרגול • מערכים דו מימדיים • פונקציות Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 2
מערכים דו-מימדיים • להלן 2 דרכים שקולות להגדרת מערך דו-מימדי המכיל 9 ערכים שלמים. • int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; • int arr[][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel
מערכים דו-מימדיים - דוגמא 1 • התוכנית בשקופית הבאה קולטת ערכים מהמשתמש, מאחסנת אותם במערך דו-מימדי ולבסוף מדפיסה את המערך שנוצר. Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel
#define SIZE 3 void main() { int arr[SIZE][SIZE]; int i, j; for ( i=0; i<SIZE; i++ ) for ( j=0; j<SIZE; j++ ) scanf("%d", &arr[i][j]); for ( i=0; i<SIZE; i++ ) { for ( j=0; j<SIZE; j++ ) printf("%d ", arr[i][j]); printf("\n"); } } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel
מערכים דו-מימדיים – דוגמא 2 • התוכנית בשקופית הבאה מייצרת מערך דו-מימדי וממלאת אותו באופן הבא: • בכל שורה זוגית מציבה בעמודות הזוגיות את ערך השורה הנוכחית פלוס אחד ובשאר העמודות באותה שורה מציבה את הערך 0. • בכל שורה אי-זוגית מציבה בעמודות האי-זוגיות את ערך השורה הנוכחית פלוס אחד ובשאר העמודות באותה שורה את הערך 0. 0 1 2 0 1 2 דוגמא למערך בגודל 3x3
int arr[SIZE][SIZE], i, j, curValue = 1; for ( i=0; i<SIZE; i++ ) { for ( j=0; j<SIZE; j++ ) { if ((i % 2) == 0) //even rown { if ( (j % 2) == 0 ) //even columns arr[i][j] = curValue; else arr[i][j] = 0; } else //odd rows { if ( (j % 2) == 0 ) //even columns arr[i][j] = 0; else arr[i][j] = curValue; } } curValue++; } #define SIZE 10
פונקציות • קבוצת משפטים המבצעים יחדיו מטלה מסויימת. • לפני כתיבת הפונקציה main() נצהיר על הפונקציות שלנו. נעשה זאת על ידי כתיבת חתימתן. • כתיבת הפונקציות תיעשה לאחר פונקציית main(). • קריאה לפונקציה נעשית על ידי כתיבת שמה ואחריו סוגריים. אם נרצה להעביר לפונקציה פרמטרים, נכתוב אותם בין הסוגריים בזמן הקריאה לפונקציה. • בסיום פעולתה, יכולה פונקציה להחזיר ערך מטיפוס מסויים, כמו למשל int או float. Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel
פונקציות - תיעוד • להלן תיעוד שנוסיף לכל פונקציה שנכתוב: • /* General description of the function. What it does.. name1 – what is this variable used for… ..... …. nameK – what is this variable used for… return value – What this function returns (meaning not type) */ • ret-type func-name(type1 name1,…typeK nameK)
דוגמא 1: מיון / הדפסה (פונקציות) • התוכנית הבאה קולטת סדרה של 6 מספרים שלמים לתוך מערך וממיינת אותם בעזרת מיון "בועות". • בתרגיל זה קיימות תת משימות כגון: • מיון בועות • ביצוע swap בין שני ערכים במערך • הדפסת המערך הממוין • כל תת בעייה תטופל על ידי פונקציה. Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 10
#include <stdio.h> #define LEN 6 void bubbleSort(int nums[], int length); void swap(int nums[], int i, int j); void printArr(int nums[], int length); void main() { int i; int vals[LEN]; for (i = 0; i < LEN; ++i) { printf("Value %d: ", i+1); scanf("%d", &vals[i]); } bubbleSort(vals, LEN); printArr(vals, LEN); } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 11
void printArr(int nums[], int length) { int i, j; for ( i=0; i<LEN; i++ ) printf("%d ", nums[i]); printf("\n"); } void bubbleSort(int nums[], int length) { int i, j; for (i = length - 1; i > 0; i--) for (j = 0; j < i; j++) if (nums[j] > nums[j + 1]) { swap(nums, j, j+1); } } void swap(int nums[], int i, int j) { int temp = nums[j]; nums[j] = nums[i]; nums[i] = temp; } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel
דוגמא 2: פתרון עבודה 1 (פונקציות) #include <stdio.h> #include <math.h> void calcLinearRoot(int b, int c); void calcTwoRealRoots(int a, int b, int discriminant); void calcOneRealRoot(int a, int b); void calcImaginaryRoots(int a, int b, int discriminant); Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel
void main() { variables declaration / printf / scanf if ( a == 0 ) //linear equation { printf("This is a linear equation\n"); if ( b == 0 ) //no solutions printf("There are no roots\n"); else calcLinearRoot(b, c); } else //a!=0, means we will have some kind of roots { discriminant = b*b - 4*a*c; if ( discriminant > 0 ) //2 real roots calcTwoRealRoots(a, b, discriminant); else if (discriminant == 0) //1 real root calcOneRealRoot(a, b); else //discriminant < 0, only imaginary roots exists calcImaginaryRoots(a, b, discriminant); } }
void calcLinearRoot(int b, int c) { float Root1; Root1 = (-1*c)/b; if ( Root1 == ((int)Root1) ) printf("Root1=%d\n", ((int)Root1)); else printf("Root1=%0.2f\n", Root1); } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel
void calcTwoRealRoots(int a, int b, int discriminant) { float Root1, Root2; printf("There are 2 real roots\n"); Root1 = ((-1*b) + sqrt(discriminant)) / (2*a); Root2 = ((-1*b) - sqrt(discriminant)) / (2*a); if ( Root1 == ((int)Root1) ) printf("Root1=%d\n", ((int)Root1)); else printf("Root1=%0.2f\n", Root1 ); if ( Root2 == ((int)Root2) ) printf("Root2=%d\n", ((int)Root2)); else printf("Root2=%0.2f\n", Root2 ); } Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel