1 / 13

Week 9 Discussion

Week 9 Discussion. Q1. void printArray ( int mylist [], int arraySize ) { int i ; for (i = 0; i &lt; arraySize; i++) printf (&quot;%d &quot;, mylist [ i ]); printf (&quot;<br>&quot;); return ; }. Q1. void passElement ( int num) { printf (&quot;The value of num is %d<br>&quot;, num); num = 1234;

baka
Download Presentation

Week 9 Discussion

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. Week 9 Discussion

  2. Q1 void printArray(intmylist[], intarraySize) { inti; for (i = 0; i < arraySize; i++) printf("%d ", mylist[i]); printf("\n"); return; }

  3. Q1 void passElement(int num) { printf("The value of num is %d\n", num); num = 1234; printf("The value of num is %d\n", num); return; }

  4. Q1 void changeElements(intmylist[]) { mylist[2] = 77; mylist[4] = 88; return; }

  5. Q1 void changeReference(intmylist[], int mylist2[]) { mylist = mylist2; return; }

  6. Q1 void copyArray(intmylist[], int mylist2[], intarraySize) { inti; for (i = 0; i < arraySize; i++) mylist[i] = mylist2[i]; return; }

  7. Q1 Output: Original array: 11 22 33 44 55 The value of num is 11 The value of num is 1234 After passing one element: 11 22 33 44 55 After changing individual elements: 11 22 77 44 88 After changing references: 11 22 77 44 88 After copying each array element: 99 99 99 99 99

  8. Q2 void swap(int *x, int *y) { inttemp; temp = *x; *x = *y; *y = temp; return; }

  9. Q2 void printArray(int list[], int size) { inti; for (i = 0; i < size; i++) { printf("%d ", *list); list++; } printf("\n"); return; }

  10. Q2 void printPattern(int list[], int size) { inti; for (i = 1; i <= size; i++) printArray(list+(size-i),i); return; }

  11. Q2 int main(void) { int list[5] = {3,2,4,1,0}, i=0, j; i = 0; while (i < 5) { if (list[i] != i) { j = list[i]; swap(&list[i], &list[j]); printArray(list,5); } else i++; } printf("Pattern printing...\n"); printPattern(list,5); return 0; }

  12. Q2 Output: 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

  13. Q3 Random Seeding: #include <time.h> #define SEED (unsigned int)time(NULL) srand(SEED);

More Related