1 / 11

Functions

Functions. Previously on Clang. אדמיניסטרציה: שיעורים קריטיים, העתקות משתנים: "פתק" המשמש לשמירת מידע טיפוסים: char, int, long, float, double אופרטורים: חשבוניים, השוואה, לוגיים משפטי תנאי: if, else. Previously on Clang. לולאות: for, while , לולאות מקוננות

cara
Download Presentation

Functions

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. Functions

  2. Previously on Clang • אדמיניסטרציה: שיעורים קריטיים, העתקות • משתנים: "פתק" המשמש לשמירת מידע • טיפוסים:char, int, long, float, double • אופרטורים: חשבוניים, השוואה, לוגיים • משפטי תנאי: if, else

  3. Previously on Clang • לולאות: for, while, לולאות מקוננות • מערכים: אתחול, שימוש, חריגה ממערך, דו מימדי • מיונים: selection, insertion • מחרוזות: משמעות char כתו וכמספר, אתחול מחרוזת, התו המפסיק \0, שימוש והדפסה • sscanf ו sprintf ופונקציות לטיפול במחרוזות

  4. פונקציה פשוטה #include <stdio.h> int squareNumber(int a) { int b = a*a; return b; } void main() { int x,y,z; x=squareNumber(3); y=squareNumber(x); z=squareNumber(x-5)+3; printf("%d, %d, %d\n",x,y,z); printf("%d\n",squareNumber(x-2)); }

  5. יותר משורת return אחת #include <stdio.h> int max(int x, int y){ if (x<y) return y; elsereturn x; } void main(){ int a,b; printf ("Enter two integers\n"); scanf("%d%d",&a,&b); printf("The maximal number is %d\n",max(a,b)); }

  6. פונקציה void void readArrayFromUser(int ar [], int size){ int i; for (i=0; i<size; i++){ printf ("enter number\n"); scanf ("%d",&ar[i]); } } void printArray(int ar [], int size){ int i; for (i=0; i<size; i++) printf ("%d\n",ar[i]); }

  7. #include <stdio.h> #define SIZE 100 void main() { int i, j, min, temp, nums[SIZE]; for (i=0; i<10; i++){ printf ("enter number\n"); scanf ("%d",&nums[i]); } for (i=0; i<SIZE; i++){ min = i; for (j=i+1; j<SIZE; j++) if (nums[j]<nums[min]) min = j; temp = nums[i]; nums[i] = nums[min]; nums[min] = temp; } for (i=0; i<SIZE; i++) printf ("%d\n",nums[i]); }

  8. #include <stdio.h> #define SIZE 100 void selectionSort(int ar[], int size){ int i, minInd; for (i=0; i<SIZE; i++){ minInd= findMinIndex(ar, size); swapElements(ar, i, minInd); } } void main(){ int ar[SIZE]; readArrayFromUser(ar, SIZE); selectionSort(ar, SIZE); printArray(ar, SIZE) }

  9. Prototype #include <stdio.h> void printMax(int, int); /*prototype*/ int main(){ int a, b; printf("Enter two numbers:\n"); scanf("%d %d", &a, &b); printMax( a, b ); return 0; } void printMax(int x, int y){ if (x>y) printf("%d",x); else printf("%d\n",y); }

  10. פונקצית swap #include <stdio.h> void swap(int x, int y){ int temp=x; x=y; y=temp; } void main(){ int a=5,b=7; printf("a: %d, b: %d\n",a,b); swap(a,b); printf("a: %d, b: %d\n",a,b); }

  11. #include <stdio.h> int a = 37, b = 0, c = 3 , d = 4; int f (int a, int b){ static int d=1; printf("\n"); { int a = 11, d = 5; printf("a= %d b= %d c= %d d= %d\n",a,b,c,d); { int b =55; c += b; } printf("b= %d c= %d d= %d\n" ,b, c, d++); } printf("a= %d b= %d c= %d d= %d\n" ,a,b, c, d++); return d; } void main(){ int c = 3, d = 1; c = f(a+b+c , d+2); f(c, d); }

More Related