1 / 28

Programming

Programming. Rehearse: 2D arrays Rehearse: Pointers & Structures Intro to Dynamic memory allocation. Example 1 – Multiplication table. Write a program that computes the Multiplication table. 1 2 3 … 10 2 4 6 … 20 3 6 9 … 30 … 10 100. Solution 1 – Multiplication table.

alina
Download Presentation

Programming

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. Programming Rehearse: 2D arrays Rehearse: Pointers & Structures Intro to Dynamic memory allocation

  2. Example 1 – Multiplication table Write a program that computes the Multiplication table. 1 2 3 … 10 2 4 6 … 20 3 6 9 … 30 … 10 100

  3. Solution 1 – Multiplication table #define SIZE 10 void init_table(int table[SIZE][SIZE]) } inti, j; for (i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) table[i][j] = (i + 1) * (j + 1); } void print_table(int table[SIZE][SIZE]) } inti, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) printf("%4d", table[i][j]); printf("\n"); } } int main() } int table[SIZE][SIZE]; init_table(table); print_table(table); return 0; {

  4. Example 2 – Matrix multiplication נכתוב תכנית שמחשבת כפל של 2 מטריצות a,b בגודל 3x3. את התוצאה נשמור במטריצה c. נוסחא לכפל מטריצות:

  5. Solution 2 – Matrix multiplication #define MATRIX_SIZE 3 int main(void) { int a[][MATRIX_SIZE] = { {1,2,3}, {4,5,6}, {7,8,9}}; int b[][MATRIX_SIZE] = { {1,0,0}, {0,2,0}, {0,0,3}}; int c[MATRIX_SIZE][MATRIX_SIZE]; inti = 0, j = 0, k = 0; /* Compute the product c = a * b */ for (i = 0; i < MATRIX_SIZE; ++i) for (j = 0; j < MATRIX_SIZE; ++j) { c[i][j] = 0; for (k = 0; k < MATRIX_SIZE; ++k) c[i][j] += a[i][k] * b[k][j]; } /* Print c */ ...return 0; }

  6. Example 3 – print a polynom Each element in the polynom is called monom typedefstructmonom{ double coefficient; int degree; {Monom;

  7. Example 3 – print a polynom Write a function that gets a valid polynom (pointer to array of Monoms) and the size of the polynom (number of monoms) and prints the polynom in the format :

  8. void printPolynom(Monom * polynom, int size){ inti; for (i=0; i<size; i++){ if(polynom[i].coefficient != 0){ if(i>0) printf(" + "); if(polynom[i].coefficient != 1 || polynom[i].degree == 0) printf("%g", polynom[i].coefficient); if(polynom[i].degree != 0) printf("x"); if(polynom[i].degree > 1) printf("^%d", polynom[i].degree); {} printf("\n");{

  9. Example 4 - char appearances typedefstruct appear{ charch; intn_appearances; } appear_t;

  10. int appearances(char* str, appear_t appear[]) Write a function that gets a string and updates the output array appear (each cell will contain a different char and its number of appearances). The function will return the number of different chars. Assume that appear is big enough.

  11. Solution 4 - char appearances intappearances (char* str, appear_t appear[]){ inti, found, n=0; for(; *str != '\0‘; str++) { found = FALSE; for(i=0; i<n; i++){ if(appear[i].ch == *str){ appear[i].n_appearances++; found = TRUE; { { if(!found){ appear[n].ch = *str; appear[n++].n_appearances = 1; { { returnn; {

  12. Malloc void* malloc(unsignedintnBytes); • Used to dynamically allocate nBytes in memory • Returns a pointer to the allocated area on success, NULL on failure • Always check whether memory was successfully allocated

  13. The free Function void free(void *ptr); • free(p) deallocates memory pointed by p. • Freeing a non-allocated area is an error. • No partial deallocation. • Always free allocated memory.

  14. Warming Up int main() { inti=0, n=0, *p=NULL; printf("How many numbers will you enter?\n"); scanf("%d", &n); /* Allocate an int array of the proper size */ p = (int*)malloc(n * sizeof(int)); if (p == NULL) { printf("Memory allocation failed!\n"); return 1; } ... /* Free the allocated space */ free(p); return 0; } casting

  15. Returning Allocated Memory Example: duplicate a string to a new location (with dynamic allocation), return the new string: // strdup - duplicate a string char* strdup(const char* str){char* dup = (char*)malloc((strlen(str)+1) * sizeof(char)); if (dup != NULL) {strcpy(dup, str); }return dup;}

  16. Exercise @ Class • Implement the function char* my_strcat(const char *s1, const char *s2); Output: a pointer to a dynamically allocated concatenation of s1 and s2. Example: my_strcat(“hello_”, “world!”); Output: “hello_world!” You can use the functions in string.h • Test your function

  17. What’s Wrong Here? char* my_strcat(const char *s1, const char *s2) { char result[500]; /* assume this is enough */ strcpy(result, s1); strcpy(result + strlen(s1), s2); return result; }

  18. Pitfalls • Accessing un-initialized pointerint* p; *p = 3; • Using de-allocated memoryint* p = (int*)malloc(SIZE * sizeof(int)); ... /* do something with p*/free(p); *p = 0;

  19. שאלה ממבחן - תש"ע, סמסטר א',שאלה 2 בשאלה זו תתבקשו לכתוב פונקציות ותוכנת מחשב לניהול משחק הלוטו. סעיף א': "מערך משורשר" - מערך המכיל ערכים של מספר תתי מערכים באותו אורך באופן רציף בזה אחר זה. לדוגמא, "מערך משורשר" המכיל 3 תתי מערכים, כל אחד בגודל 6:

  20. שאלה ממבחן - תש"ע, סמסטר א',שאלה 2 כתבו פונקציה המקבלת: • מערך של מספרים שלמים שהוא "מערך משורשר" • גודל כל תת- מערך n. • 2 מספרים שלמים, i ו-j. הפונקציה תחזיר את arr[i][j]. intgetVal(int arr[], int n, int i, int j) { returnarr[i * n + j]; }

  21. שאלה ממבחן - תש"ע, סמסטר א',שאלה 2 סעיף ב: מגרילים 6 מספרים בין 1 ל-49. טופס מנצח מכיל את כל 6 המספרים. ממשו פונקציה הבודקת מי הם הזוכים, המקבלת: winNums - מערך בגודל 6 המכיל את המספרים הזוכים. Tickets -מערך משורשר עם תתי מערכים באורך 6 ובהם טפסים של משתתפים. numTickets -מספר הטפסים במערך. res -מערך בגודל מספר המשתתפים. בסיום הריצה, התא ה-i ב-resיכיל 1 אם הטופס ה-i זכה בפרס ו-0 אחרת. ערך מוחזר: מספר הטפסים הזוכים.

  22. שאלה ממבחן - תש"ע, סמסטר א',שאלה 2 ניצור מערך עזר בגודל 49 שיבדוק אם מספר הוא מספר זוכה באמצעות פעולת השוואה אחת בלבד! כדי לקבל את מלוא הנקודות בסעיף זה יש לעבור רק פעם אחת על המערך winNumsורק פעם אחת על המערך tickets.

  23. שאלה ממבחן - תש"ע, סמסטר א',שאלה 2 Auxiliary array #define TICKET_SIZE 6 #define NUM_RANGE 49 intcheckWin(intwinNums[], int tickets[], intnumTickets, int res[]) { inti, j, val, numWinners = 0; char numbers[NUM_RANGE]={0}; // init auxiliary array for (i = 0; i < TICKET_SIZE; i++) numbers[winNums[i]] = 1;

  24. שאלה ממבחן - תש"ע, סמסטר א',שאלה 2 // go over all tickets for (i = 0; i < numTickets; i++) { res[i] = 1; // go over 6 numbers in a ticket for (j = 0; j < TICKET_SIZE; j++) { val = getVal(tickets, TICKET_SIZE, i, j); if (numbers[val] == 0) { // found a non-winning number – ticket lost res[i] = 0; break; } { // if we are here – all 6 numbers won numWinners += res[i]; } returnnumWinners; }

  25. שאלה ממבחן - תש"ע, סמסטר א',שאלה 2 סעיף ג' ( 15 נקודות) : הפרס הראשון בלוטו הוא 10,000,000 ש"ח. אם כמה משתתפים זכו הם יתחלקו בפרס באופן שווה (ייתכן שאין זוכה). כתבו תוכנית הקולטת מהמשתמש את הנתונים הבאים לפי הסדר המתואר: 1. ששת המספרים הזוכים 2. מספר המשתתפים 3. טפסי הניחוש בזה אחר זה פלט התוכנית הוא טבלה שבכל שורה בה מופיע מספר סידורי של משתתף וסכום הזכייה שלו.

  26. שאלה ממבחן - תש"ע, סמסטר א',שאלה 2 int main() { inti, numParticipants, numWinners = 0; intwinningNumbers[TICKET_SIZE]; int *tickets, *winners; double jackpot = 10000000; printf("Enter winning numbers: "); for (i = 0; i < TICKET_SIZE; i++) scanf("%d", &winningNumbers[i]); printf("Enter number of participants: "); scanf("%d", &numParticipants); // dynamic allocation tickets = (int*) malloc(numParticipants *TICKET_SIZE * sizeof(int)); winners = (int*) malloc(numParticipants *sizeof(int));

  27. שאלה ממבחן - תש"ע, סמסטר א',שאלה 2 if (tickets == NULL || winners == NULL) { printf("Unable to allocate memory!"); return 1; } printf("Enter lottery tickets: "); … numWinners = checkWin(winningNumbers, tickets,numParticipants, winners); ... // free allocated memory free(tickets); free(winners); return 0; {

  28. Solution to Class exercise char* my_strcat(const char *s1, const char *s2) { intlen;char *result = NULL; len = strlen(s1) + strlen(s2) + 1; result = (char*)malloc(len * sizeof(char)); if (result == NULL) { printf(“Memory allocation failed!\n"); return NULL; } strcpy(result, s1); strcat(result, s2); return result; }

More Related