130 likes | 211 Views
Week 9 Discussion. Q1. void printArray ( int mylist [], int arraySize ) { int i ; for (i = 0; i < arraySize; i++) printf ("%d ", mylist [ i ]); printf ("<br>"); return ; }. Q1. void passElement ( int num) { printf ("The value of num is %d<br>", num); num = 1234;
E N D
Q1 void printArray(intmylist[], intarraySize) { inti; for (i = 0; i < arraySize; i++) printf("%d ", mylist[i]); printf("\n"); return; }
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; }
Q1 void changeElements(intmylist[]) { mylist[2] = 77; mylist[4] = 88; return; }
Q1 void changeReference(intmylist[], int mylist2[]) { mylist = mylist2; return; }
Q1 void copyArray(intmylist[], int mylist2[], intarraySize) { inti; for (i = 0; i < arraySize; i++) mylist[i] = mylist2[i]; return; }
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
Q2 void swap(int *x, int *y) { inttemp; temp = *x; *x = *y; *y = temp; return; }
Q2 void printArray(int list[], int size) { inti; for (i = 0; i < size; i++) { printf("%d ", *list); list++; } printf("\n"); return; }
Q2 void printPattern(int list[], int size) { inti; for (i = 1; i <= size; i++) printArray(list+(size-i),i); return; }
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; }
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
Q3 Random Seeding: #include <time.h> #define SEED (unsigned int)time(NULL) srand(SEED);