37
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS CSE @ UTA 04/27/22 1

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

Embed Size (px)

DESCRIPTION

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 ", d[2]); fx( d ); printf("%d ", d[2]); } void fx(int a[]) { printf("In fx ", a); a[2] = 7; } Pass Array to A Function

Citation preview

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

DATA TYPE, MEMORY, AND FUNCTION

Dong-Chul KimBioMeCISCSE @ UTA

05/04/23 1

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

#include <stdio.h>

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

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

• 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 <stdio.h>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

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

• 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.

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

#include <stdio.h>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");

}

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

• 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

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

• 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

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

• 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

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

• 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

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

#include <stdio.h>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

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

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

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

#include <stdio.h>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;

}

}

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

• 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

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

• 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

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

• 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

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

• 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

52 1

22 0

12 1

0

5

right

left

1 0 1

rightleft

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

• Storing the integer 13 using a binary code

Convert from a base-2 to a base - 10

0 0 0 0 1 1 0 1

2021222324252627

23 22 20+ + =13

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

• 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).

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

• An unsigned integer uses all of its bits to represent the magnitude of its value.

• An unsigned integer of n bits can represent 2n 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 2n−1 possible nonnegative (2n−1− 1 positive and zero) values and 2n−1

possible negative values.

Page 20: DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/2016 1
Page 21: DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/2016 1

• Given 4-byte for (signed) int type, the maximum value is 2147483647= 231-1

• The entire range for int is • [-2147483648, 2147483647]

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

#include <stdio.h>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

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

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

+ .31 1

sign fraction exponent

3.1

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

#include <stdio.h>int main(void){

double rent = 3852.99;printf("*%f*\n", rent);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.

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

• *01234567890123456789*• *3852.990000*• *3852.99*• *3853.0*• * 3852.990*• *+3852.99*• *0003852.99*

Floating-Point cont.

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

• 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

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

• 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.

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

• 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.

  xi     yi   xi |1 yi

0 0 0

0 1 1

1 0 1

1 1 1

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

• 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

  xi     yi   xi ^1 yi

0 0 0

0 1 1

1 0 1

1 1 0

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

• The bitwise NOT (~) • A bitwise NOT is one unary bitwise operator, • Bitwise NOT flips all of the bits.

  xi   ~1 xi 0 1 1 0

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

#include <stdio.h>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

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

• 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.

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

• Suppose given an arbitrary integer, we focus on its binary representation.

• We are interested in whether there is any “1” at a1, a2

• 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 &

a0a1a2a3a4a5a6a7

an integer

0 0 0 0 0 1 1 0mask

&

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

#include<stdio.h>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;}

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

• 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.

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

• 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

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

# include<stdio.h> 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;}