1 / 11

CS1010E Programming Methodology

Tutorial 8 Question 1. CS1010E Programming Methodology. Discussion. *variable (in declaration) means the variable is a pointer variable *variable (in statement) refers to the value of the variable, NOT the address NOTE:

titus
Download Presentation

CS1010E Programming Methodology

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. Tutorial 8 Question 1 CS1010EProgramming Methodology

  2. Discussion • *variable (in declaration) means the variable is a pointer variable • *variable (in statement) refers to the value of the variable, NOT the address • NOTE: There is a missing information where the initial value of variable “i” is assigned as 0.

  3. Discussion (cont’d) while (i < 5) { if (list[i] != i) { j = list[i]; swap(&list[i], &list[j]); printArray(list, 5); } else i++; }

  4. Discussion (cont’d) void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; return; }

  5. Discussion (cont’d) voidprintArray(int list[], int size) { for (i = 0; i < size; i++) { printf(“%d“, *list); list++; } printf(“\n”); return; }

  6. Discussion (cont’d)

  7. Discussion (cont’d) Answer (so far) : 1 2 4 3 0 2 1 4 3 0 4 1 2 3 0 0 1 2 3 4 Pattern printing…

  8. Discussion (cont’d) voidprintPattern(int list[], int size) { inti; for (i = 1; i <= size; i++) printArray(list+(size-i), i); return; }

  9. Discussion (cont’d) printArray(list+(size-i), i); list+(size-i)  refers to an address (array is a pointer where the address of each member is adjacent to the member next to it)

  10. Discussion (cont’d) list[5] = {0, 1, 2, 3, 4}

  11. Discussion (cont’d) Answer (final) : 1 2 4 3 0 2 1 4 3 0 4 1 2 3 0 0 1 2 3 4 Pattern printing… 4 3 4 2 3 4 1 2 3 4 0 1 2 3 4

More Related