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

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

Enumerated Types 4 Besides the built-in types, ANSI C allows the definition of user-defined enumerated types –To define a user-define type, you must give.
Assembly Language and Computer Architecture Using C++ and Java
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
Signed Numbers.
Assembly Language and Computer Architecture Using C++ and Java
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Computer ArchitectureFall 2007 © August 29, 2007 Karem Sakallah CS 447 – Computer Architecture.
Operations on data CHAPTER 4.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Simple Data Type Representation and conversion of numbers
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
Computer Organization and Architecture Computer Arithmetic Chapter 9.
Computer Arithmetic Nizamettin AYDIN
Computer Arithmetic. Instruction Formats Layout of bits in an instruction Includes opcode Includes (implicit or explicit) operand(s) Usually more than.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
C Tokens Identifiers Keywords Constants Operators Special symbols.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
Lecture12. Outline Binary representation of integer numbers Operations on bits –The Bitwise AND Operator –The Bitwise Inclusive-OR Operator –The Bitwise.
Bitwise Operators Fall 2008 Dr. David A. Gaitros
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Variables Symbol representing a place to store information
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Chapter 3 Structured Program Development in C Part II C How to Program, 8/e, GE © 2016 Pearson Education, Ltd. All rights reserved.1.
Chapter 9 Computer Arithmetic
William Stallings Computer Organization and Architecture 8th Edition
CSE 220 – C Programming Bitwise Operators.
Formatted Input and Output
ECE Application Programming
Functions Dr. Sajib Datta
Functions Dr. Sajib Datta
William Stallings Computer Organization and Architecture 7th Edition
Data Structures Mohammed Thajeel To the second year students
Fundamental Data Types
Control Structures Lecture 7.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
ECEG-3202 Computer Architecture and Organization
Chapter 8 Computer Arithmetic
Fundamental Data Types
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

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

#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

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

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.

#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"); }

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

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

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

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

#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

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

#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; }

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

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

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

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 right left 1 01 right left

Storing the integer 13 using a binary code Convert from a base-2 to a base =13

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

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.

Given 4-byte for (signed) int type, the maximum value is = The entire range for int is [ , ]

#include int main(void) { int i = ; unsigned int j = ; 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

 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 sign fraction exponent 3.1

#include int main(void) { double 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.

* * * * * * *3853.0* * * * * * * Floating-Point cont.

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

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.

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

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

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

#include int main(void) { int a = 9, b = 3; /*a = , b = */ 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

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.

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, 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 mask &

#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; }

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.

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

# 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; }