Presentation is loading. Please wait.

Presentation is loading. Please wait.

DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/2016 1.

Similar presentations


Presentation on theme: "DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/2016 1."— Presentation transcript:

1 DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 2/18/2016 1

2 #include int LargerInt(int a, int b); int main(void) { int x, y; printf("Please input two integers:"); scanf("%d", &x); scanf("%d", &y); if(LargerInt(x,y)) { printf("a is larger than b.\n"); } else { printf("a is not larger than b.\n"); } return 0; } int LargerInt(int a, int b) { if(a > b) return 1; else return 0; } Compare two Integers

3 This program will convince you pass an array to a function is copy by reference, because the second printf() will tell you the new value stored in d[2]. #include void fx(int a[]); int main(void) { int d[] = {9, 2, 6}; printf("%d\n", d[2]); fx( d ); printf("%d\n", d[2]); } void fx(int a[]) { printf("In fx\n", a); a[2] = 7; } Pass Array to A Function

4 If you want to pass an array and use it in your function, but you don’t want to change the values stored in the array for main() function, you can make a copy of that array manually in your function. See the example in the following.

5 #include void anotherarray(int arr[], int s); int main(void) { int intarr[] = {33, 45, 86}; int size = sizeof(intarr)/sizeof(int); int i; printf("Before call my function, the array in the main function is:"); for (i = 0; i< size; i ++) { printf("%d ", intarr[i]); } printf("\n"); anotherarray(intarr, size); printf("After call my function, the array in the main function is:"); for (i = 0; i< size; i ++) { printf("%d ", intarr[i]); } return 0; } void anotherarray(int arr[], int s) { int anotherarray [100], j; for (j = 0; j < s; j++)/*make value copy manually*/ anotherarray[j] = arr[j]; for (j = 0; j < s; j++)/*change the values in the new array*/ anotherarray[j] = 2*anotherarray[j]; printf("In my function, the array is : "); for (j = 0; j < s; j++)/*print out the new array*/ { printf("%d ", anotherarray[j]); } printf("\n"); }

6 So far we have studied int, char, double. There are some other variations of these data types, which require different amount of memory. Use sizeof() to check the amount. Example: float short int (or written as short) long int (or written as long) unsigned int (or written as unsigned) More Variable Types

7 Computer programs and their data are stored in memory as numerical values. These numerical values are not stored in base-10 (decimal), but instead are stored in base-2 (binary). Computer Memory

8 Used to describe the units of computer memory and data Bit – the smallest unit of memory, holding only two values 0 or 1 Byte – 8 bits form 1 byte Words - Collections of bytes are called words. How many bytes it takes to form a word is machine-dependent Bits, Bytes, and Words

9 In your function, the key word “return” can only return a single value, NOT multiple values. But you can define an array in main function body, and pass it to your function. In your function, we may store multiple values in that array, which will be visible in the main function. So it’s sort of returning multiple values. See the example in the next slide. Functions

10 #include void minmax(int myarr[], int myminmax [], int s) { int i; for(i = 0; i < s; i ++) { if (i == 0) { myminmax[0] = myarr[0]; myminmax[1] = myarr[1]; /*myminmax[0] is used to store the minimum value; myminmax[1] is used to store the maxmum value*/ } else if (myminmax[0] > myarr[i]) myminmax[0] = myarr[i]; else if (myminmax[1] < myarr[i]) myminmax[1] = myarr[i]; } int main(void) { int arr[] ={42, 2, 443, 23, 12}, minmaxarr[2], size; size = sizeof(arr)/sizeof(int); minmax(arr, minmaxarr, size); printf("The array [%d] has a minimum value of %d, and a maximum value of %d.\n", size, minmaxarr[0], minmaxarr[1]); return 0; } Define a function to calculate the minimum and maximum values in an array of integers

11 Input 10 integers, and sort them. The function is finding the maximum value in an array

12 #include void maxvalue(int arr[], int k); int main(void) { int iarr[4], i; printf("Please input 4 integers:"); for (i = 0 ; i < 4; i++) { scanf("%d", &iarr[i]); } printf("\n"); for (i = 0 ; i < 4-1 ; i++) { maxvalue(iarr, i); } for (i = 0 ; i < 4; i++) printf("%d\t", iarr[i]); printf("\n"); return 0; } void maxvalue(int arr[], int k) { int j, max = 0, indexmax = 0; for(j = k; j < 4; j++) { if (j == k || arr[j] > max) { max = arr[j]; indexmax = j; } /*swap arr[k] and max, if needed*/ if(indexmax != k) { arr[indexmax] = arr[k]; arr[k] = max; }

13 So far we have studied int, char, double. There are some other variations of these data types, which require different amount of memory. Use sizeof() to check the amount. Example: float short int (or written as short) long int (or written as long) unsigned int (or written as unsigned) More Variable Types

14 Computer programs and their data are stored in memory as numerical values. These numerical values are not stored in base-10 (decimal), but instead are stored in base-2 (binary). Computer Memory

15 Used to describe the units of computer memory and data Bit – the smallest unit of memory, holding only two values 0 or 1 Byte – 8 bits form 1 byte Words - Collections of bytes are called words. How many bytes it takes to form a word is machine-dependent Bits, Bytes, and Words

16 The number is divided by two, and the remainder is the least-significant bit. The (integer) result is again divided by two, its remainder is the next least-significant bit. This process repeats until the result of further division becomes zero. Convert from a base-10 to a base - 2 5 2 1 2 2 0 12 1 0 5 right left 1 01 right left

17 Storing the integer 13 using a binary code Convert from a base-2 to a base - 10 0000 1101 2020 2121 2 2323 2424 2525 2626 2727 2323 2 2020 + + =13

18 To determine how much memory is assigned to a variable, we need to know the number of bytes it is allocated. To determine the range of values that can be represented, we need to also know if the number is signed (i.e., can represent zero or positive and negative numbers) or unsigned (i.e., can only represent zero and positive integers).

19 An unsigned integer uses all of its bits to represent the magnitude of its value. An unsigned integer of n bits can represent 2 n possible values. A signed integer uses the left-most bit to indicate the sign of the value (0 is positive or zero, 1 is negative) A signed integer of n bits can represent 2 n−1 possible nonnegative (2 n−1 − 1 positive and zero) values and 2 n−1 possible negative values.

20

21 Given 4-byte for (signed) int type, the maximum value is 2147483647= 2 31 -1 The entire range for int is [-2147483648, 2147483647]

22 #include int main(void) { int i = 2147483647; unsigned int j = 4294967295; printf("There %d bytes for int type.\n", sizeof(unsigned int)); printf("%d, %d, %d\n", i, i+1, i+2); printf("%u, %u, %u\n", j, j+1, j+2); return 0; } Make sure your input value is valid. What to write a program to check whether an input integer will cause overflow or not????? - softhomework Overflow problem

23  A real number including numbers between the integers, e.g., 7.00, 2.13E4  Storing – breaking up a number into a fractional part and an exponent part, and storing them separately  Use “%f” as format specifier to print out float or double values  Floating-point numbers can represent a much larger range of values than integers can Floating-Point +.311 sign fraction exponent 3.1

24 #include int main(void) { double rent = 3852.99; printf("*%f*\n", rent); printf("*%4.2f*\n", rent); printf("*%3.1f*\n", rent); printf("*%10.3f*\n", rent); printf("*%+4.2f*\n", rent); printf("*%010.2f*\n", rent); return 0; } “m.nf” – there two defaults – the field width and the number of digits to the right of the decimal. The second default if six digits, and the field width is whatever it takes to hold the number. + flag causes the result to be printed with its algebraic sign 0 flag produces leading zeros to pad the result to the full field width Floating-Point cont.

25 *01234567890123456789* *3852.990000* *3852.99* *3853.0* * 3852.990* *+3852.99* *0003852.99* Floating-Point cont.

26 Sometimes we need to change the type of a variable temporarily for some reasons: A calculation needs a certain variable type in order to store the answer correctly An expectation by a function for a variable of a certain type To do this, we cast a variable to a new type. The form for this is (new variable type) some_variable Casting

27 Up casting. float x; int i = 12; x = (float) i; Note: the value store in variable i is not changed at all. How to test it???? Down casting float x = 3.3; int i; i = (int) x; Casting cont.

28 The bitwise OR (|) A bitwise OR takes two binary representations of equal length Perform the logical OR operation on each pair of corresponding bits. For each pair, the result is 1 when at least one of the two bits is 1; otherwise, the result is 0. x i y i x i | 1 y i 000 011 101 111

29 The bitwise XOR (^) (exclusive OR) A bitwise XOR takes two binary representations of equal length The usual bitwise OR operator is inclusive OR. XOR is 1 only if exactly one of the two bits is 1 x i y i x i ^ 1 y i 000 011 101 110

30 The bitwise NOT (~) A bitwise NOT is one unary bitwise operator, Bitwise NOT flips all of the bits. x i ~ 1 x i 01 10

31 #include int main(void) { int a = 9, b = 3; /*a = 0...0001001, b = 0...0000011*/ printf("The bitwise AND is %d.\n", a&b); printf("The bitwise OR is %d.\n", a|b); printf("The bitwise XOR is %d.\n", a^b); printf("The bitwise NOT (for a, b) is (%d, %d).\n", ~a, ~b); return 0; } Example for Bitwise Operation

32 The bitwise AND is 1. The bitwise OR is 11. The bitwise XOR is 10. The bitwise NOT (for a, b) is (-10, -4). Example for Bitwise Operation cont.

33 Suppose given an arbitrary integer, we focus on its binary representation. We are interested in whether there is any “1” at a 1, a 2 We design a mask, 00100100 We calculate the integer & mask and check whether the result is equal to zero or not An application of bitwise AND & a0a0 a1a1 a2a2 a3a3 a4a4 a5a5 a6a6 a7a7 an integer 0000 0110 mask &

34 #include int main (void) { int myint, mask; scanf("%d", &myint); mask = 6; printf("The input integer is %d.\n", myint); if (myint&mask != 0) printf("There is at least one \"1\" at a1 and a2.\n"); else printf("There is no \"1\" at a1 or a2.\n"); return 0; }

35 There are operators that do assignment such as +=, -=, *=, and so on. They apply to bitwise logical operators too. For example, |=, &=, ^=. Nearly all binary operators have a version with = after it.

36 Recursive Function Definition Recursive function is a function that contains a call to itself. Why Recursive Function Recursive function allows you to divide your complex problem into identical single simple cases which can handle easily. Note of Using Recursive Function Recursive function must have at least one exit condition that can be satisfied. Otherwise, the recursive function will call itself repeatedly into a dead loop until the runtime stack overflows. Recursive Function

37 # include int sum(int number) { if(number <= 1) return 1; return number + sum(number - 1); } void main() { int x = 5; printf("The sum of all positive integers (<= %d) is %d.\n",x,sum(x)); return 0; }


Download ppt "DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/2016 1."

Similar presentations


Ads by Google