Presentation is loading. Please wait.

Presentation is loading. Please wait.

Will these print the same thing?

Similar presentations


Presentation on theme: "Will these print the same thing?"— Presentation transcript:

1 Will these print the same thing?
#include <stdio.h> void printout(int num[]); int main(void) { int num[9] = {10,20,30,40,50,60,70,80,90}; printout(num); } void printout(int num[]) int i; for (i=0;i<9;i++) printf(“num=%d”, num[i]); #include <stdio.h> int main(void) { int num[9] = {10,20,30,40,50,60,70,80,90}; int i; for (i=0;i<9;i++) printf(“num=%d”, num[i]); } YES!

2 What will this print out?
#include <stdio.h> void change(int array[], int val); int main(void) { int val = 3; int array[1] = {3}; change(array, val); printf(“array=%d, val=%d”,array[0],val); } void change(int array[], int val) int i; array[0] = array[0]*2; val = val * 2; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6

3 In the previous example, array was passed by ____1______,
while val was are passed by ___2_____? 1. reference, value 2. value, reference 4. Oh shoot, don’t know, I’d better ask Garvin’s other half as she is the one who knows everything! 3. Yo mamma, yo daddy

4 What will this print out?
#include <stdio.h> void change(int array, int val); int main(void) { int val = 3; int array[1] = {3}; change(array[0], val); printf(“array=%d, val=%d”,array[0],val); } void change(int not_an_array, int val) not_an_array = not_an_array*2; val = val * 2; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6 Notice that the function no longer takes in an array, but an array element – which is passed by value

5 What will this print out?
#include <stdio.h> int change(int array[], int val); int main(void) { int val = 3; int array[1] = {3}; val = change(array, val); printf("array=%d, val=%d",array[0],val); } int change(int array2[], int val) array2[0] = array2[0]*2; val = val * 2; return val; 1. array=6, val=6 2. array=6, val=3 3. array=3, val=3 4. array=3, val=6

6 The function printout does not know anything about variable size
What is missing here? #include <stdio.h> void printout(int num[]); int main(void) { int size = 9; int num[9] = {10,20,30,40,50,60,70,80,90}; printout(num); } void printout(int num[]) int i; for (i=0;i<size;i++) printf(“num=%d”, num[i]); The function printout does not know anything about variable size

7 So the previous examples needs to look like one of these two:
#include <stdio.h> void printout(int num[], int size); int main(void) { int size = 9; int num[9] = {10,20,30,40,50,60,70,80,90}; printout(num, size); } void printout(int num[], int size) int i; for (i=0;i<size;i++) printf(“num=%d”, num[i]); #include <stdio.h> #define size 9 /* include a define */ void printout(int num[]); int main(void) { int num[9] = {10,20,30,40,50,60,70,80,90}; printout(num); } void printout(int num[]) int i; for (i=0;i<size;i++) printf(“num=%d”, num[i]);

8 Do you need to know the array size of a character array before hand?
1. No because a character array is a string with a terminating character, ‘\0’, and thus you can just use strlen 2. Yes because it is an array and every array has a length that needs to be known by the programmer beforehand 3. Yes because it is I think I read somewhere that says I must know the array size of a character array beforehand – some blog I think. 4. What is Garvin’s wife’s number, I’ll just ask her.

9 Skeleton for your inclass homework:
/* include all include files that you need */ void changearray( you must figure out what goes here); int main() { /* Declare variables */     /* Read in array elements from user of a specified size (arrayLength) */     for (i = 0; i < arrayLength; i++)     {         printf("Please enter an integer to be put in the array: ");         scanf("%d", &array[i]);     } /* print out array */ /* call the changearray function */ /* print out new array */ } void changearray(you must figure out this function)


Download ppt "Will these print the same thing?"

Similar presentations


Ads by Google