Administrative things

Slides:



Advertisements
Similar presentations
Modular Programming With Functions
Advertisements

Computer Programming w/ Eng. Applications
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Differences between Java and C CS-2303, C-Term Differences between Java and C CS-2303, System Programming Concepts (Slides include materials from.
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.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
1 CSE1301 Computer Programming: Lecture 9 Input/Output.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
Computer Science 210 Computer Organization Introduction to C.
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.
Functions in C++ Eric Roberts CS 106B January 9, 2013.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Programming III SPRING 2015
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
IT253: Computer Organization
1 Lecture04: Basic Types & Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
1 Lecture04: Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Data Types. Data types Data type tells the type of data, that you are going to store in memory. It gives the information to compiler that how much memory.
Numeric types of C Integers – Signed and unsigned – In a few different sizes – 209 Floating point – In different sizes –
Chapter 7: Basic Types Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Basic Types.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
Data Type. Syntax Rules Recap keywords breakdoubleifsizeofvoid caseelseintstatic..... Identifiers not#me123th scanfprintf _idso_am_igedd007 Constant ‘a’‘+’
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Chapter 7: Basic Types Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 7 Basic Types Last item in Page 89: check stdint.h.
CSE1301 Computer Programming: Lecture 6 Input/Output.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
DATA TYPE, MEMORY, AND FUNCTION Dong-Chul Kim BioMeCIS UTA 2/18/
1 Lecture03: Control Flow 9/24/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
7. BASIC TYPES. Systems of numeration Numeric Types C’s basic types include integer types and floating types. Integer types can be either signed or unsigned.
CSE 251 Dr. Charles B. Owen Programming in C1 Pointers and Reference parameters.
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Chapter 6 - Functions modular programming general function format
CSE 220 – C Programming Pointers.
Administrative things
Tokens in C Keywords Identifiers Constants
Input/output.
7. BASIC TYPES.
INC 161 , CPE 100 Computer Programming
EPSII 59:006 Spring 2004.
Fundamental Data Types
C programming---basic
Administrative things
Input and Output Lecture 4.
Formatted and Unformatted Input/Output Functions
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lectures on Numerical Methods
Basic Types Chapter 7 Copyright © 2008 W. W. Norton & Company.
Differences between Java and C
Fundamental Data Types
Introduction to C Programming
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Data Type.
Administrative things
Administrative things
Presentation transcript:

Lecture04: Basic Types & Function Slides modified from Yin Lou, Cornell CS2022: Introduction to C

Administrative things Assignment #3 is on the course webpage due next week. Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Review from Last Week if, else-if, else < condition> ? <expression1> : <expression2> switch, case, default, break for (<exp1>; <exp2>; <exp3>) <statement> while (<condition>) <statement> do <statement> while (<condition>); break, continue Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Basic Types Have learned these basic types; int, float, double, char Learn the rest of the basic types Integer type: short int, int, long int, long long int + signed, unsigned Floating types: float, double, long double Character types: char Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Integer Type: short int Different integer types have different sizes For examples: size of (short int) is 2 bytes Declare variable with short int: short int a; // short a; What is a byte? A byte = 8 bits. What is a bit? How to represent an integer 5 in binary form? How to represent a positive/negative sign? The sign bit: 0xxxxxx (positive or zero), 1xxxxxx (negative) What is the largest number for short int? 0111111 What is the smallest number for short int? 00000000 Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Integer Type: unsigned short int An integer with no sign bit (zero or positive) Declare variable with unsigned short int: unsigned short int a; What is the largest/smallest number for unsigned short int? Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Integer Type: short int, int, long int, long long int Size rule: short int < int <= long int <= long long int How do you find out these sizes? sizeof() #include <stdio.h> int main() { printf("sizes: short int(%d), int(%d), long int(%d), long long int(%d)\n", sizeof(short int), sizeof(int), sizeof(long int), sizeof(long long int)); } On my machine: short int: 2 bytes int: 4 bytes long int: 4 bytes long long int: 8 bytes Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Integer Type: short int, int, long int, long long int Size rule: short int < int <= long int <= long long int How do you find out these sizes? sizeof() #include <stdio.h> int main() { printf("sizes: short int(%d), int(%d), long int(%d), long long int(%d)\n", sizeof(short int), sizeof(int), sizeof(long int), sizeof(long long int)); } On my machine: short int(2B): -32,768 ~ 32,767 int(4B): -2,147,483,648 ~ 2,147,483,648 long long int(8B): -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,808 Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Octal and Hexadecimal Numbers Decimal(base 10) 15 255 32767 Octal(base 8) 017 0377 077777 Hexadecimal(base 16) 0xf 0xff 0x7fff Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Reading & Writing Integers unsigned int u; scanf(“%u”, &u); // read u in base 10 printf(“%u”, u); // write u in base 10 scanf(“%o”, &u); // read u in base 8 printf(“%o”, u); // write u in base 8 scanf(“%x”, &u); // read u in base 16 printf(“%x”, u); // write u in base 16 short int s; scanf(“%hd”, &s); // add h printf(“%hd”, s); long int l; scanf(“%ld”, &l); // add l printf(“%ld”, l); long long int ll; scanf(“%lld”, &ll); // add ll printf(“%lld”, ll); Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Common Pitfall: Integer Overflow short i; i = 10000 + 20000 + 30000; printf(“%d\n”, i); Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Floating Types float: 4B, one bit for sign, 8 bits for exponent, 23 bits for fraction Range: 1.17549 * 10^-38 ~ 3.40282 * 10^38 6 digits precision double: 8B Range: 2.22507*10^-308 ~ 1.79769*10^308 15 digits precision long double: 12B Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Reading & Writing Floats float f; scanf(“%f”, &f); printf(“%f”, f); double d; scanf(“%lf”, &d); // add l printf(“%lf”, d); long double ld; scanf(“%Lf”, &ld); // add L printf(“%Lf”, ld); Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

In-Class Exercise 3-1 Write a program that computes the factorial of a positive integer. Note that factorial function F(n) = n*(n-1)*(n-2)*…1 Enter a positive number: 6 Factorial of 6: 720 (a) Use a short variable to store the value of the factorial. What is the largest value of n for which the program correctly prints the factorial of n? (b) Repeat (a) using an int variable instead. (c) Repeat (a) using a long variable instead. (d) Repeat (a) using a long long variable instead. (e) Repeat (a) using a float, double, long double variable instead. Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Character Types char: 1B 256 characters char ch; ch = ‘a’; ch = ‘A’; Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

C treats characters as small integers char ch; int i; i = ‘a’; // i is now 97 ch = 65; // ch is now ‘A’ ch = ch + 1; // ch is now ‘B’ ch++; // ch is now ‘C’ if (‘a’ <= ch && ch <= ‘z’) // ch can also be compared like numbers ch = ch – ‘a’ + ‘A’; for (ch = ‘A’; ch <= ‘Z’; ch++) … Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Reading & Writing Characters char ch; scanf(“%c”, &ch); // read a single character, including a space! printf(“%c”, ch); scanf(“ %c”, &ch); // skip white spaces, then read ch do { scanf(“%c”, &ch); } while (ch != ‘\n’); // detec the end of line char putchar(ch); // write a char ch = getchar(); // read a char and store it in ch while ((ch = getchar()) == ‘ ‘); // skip spaces Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

In-Class Exercise 3-2 Write a program that takes a first name and last name entered by the user and displays the last name, a comma, and the first initial, followed by a period. Use getchar() and putchar(). Enter a first and last name: Lloyd Fosdick Fosdick, L. Break it down into small steps: Read all characters Find the white space Print the last name (skip the first name and the white space) Save the first name initial character in a variable Print ‘, ’ and the first name initial character Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Type Conversion & Casting char c; short s; int I; unsigned int u long int l; usigned long int ul; float f; double d; long double ld; i = i + c; // c is converted to int i = i + s; // s is converted to int u = u + I; // l is converted to int l = l + u; // u is converted to long int i = (int) f; // f is converted to int f = (float) i; // I is converted to float c = 10000; // wrong s = 999999999999999; // wrong Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Math Functions Many math functions are defined in <math.h> pow(a, b) - Compute ab exp(a) - Compute ea log(a) - Compute natural logarithm log10(a) - Compute common logarithm sqrt(a) - Compute square root fabs(a) - Compute absolute value ceil/floor - Round up/down value cos, sin, tan acos, asin, atan Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Functions Purpose of functions Break a program into pieces that are easier to write and understand Break down a big complex problem into small sub-problems that are easy to solve What if the subproblems are still difficult to solve? More about this in the next lecture. Makes recursive algorithms easier to implement Promotes code reuse Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Define a Simple Function <func return type> <func name> (<parameter#1: param_type name>, <parameter #2: param_type name>, …) { // func code } int sum(int a, int b) { int sum; sum = a + b; return sum; } Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Define another Simple Function <func return type> <func name> (<parameter#1: param_type name>, <parameter #2: param_type name>, …) { // func code } int power(int base, int exp) { int i, p = 1; for (i = 1; i <= exp; ++i) p *= base; // p = p * base; } return p; Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Simple Function in Context #include <stdio.h> int power(int base, int exp); // function prototype or declaration void main() { int i = 3, j = 4; printf("%d^%d is %d.\n", i, j, power(i, j)); // function call } int power(int base, int exp) // function definition int i, p = 1; for (i = 1; i <= exp; ++i) p *= base; return p; Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Function Return Values If a function returns type void, then no return statement is needed. If a function returns another type, then a return statement is required along all possible execution paths. What’s wrong with the left? #include <stdio.h> int foo(int param) { if (param== 1) return 1; } void main() printf("%d\n", foo(0)); Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Call by Value Function arguments in C are passed by value The value of the argument is passed, not a reference Functions are given a new copy of their arguments So a function can't modify the value of a variable in the calling function (unless you use pointers) Pointers in the next lecture Variable scope #include <stdio.h> int foo(int c) { c = 3; return c; } void main() int a = 1, b; b = foo(a); printf("%d %d\n", a, b); // Output 1 3 Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Call by Value: More example #include <stdio.h> void swap(int a, int b) { int t = a; a = b; b = t; } void main() int a = 1, b = 2; swap(a, b); printf("%d %d\n", a, b); // Output 1 2 Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Call by Value: Tradeoff Call by value has advantages and disadvantages Advantage: some functions are easier to write int power(int base, int exp) { int result = 1; for (; exp >= 1; exp = exp - 1) result *= base; } return result; Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Call by Value: Tradeoff Disadvantage: sometimes you’d like to modify an argument (e.g. swap() function) What’s wrong with the left? We’ll see how to do this using pointers later void swap (int x, int y) { int temp = x; x = y; y = temp; } Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

In-Class Exercise 3-3 Write a function that prints the following diamond shape. Your program first reads an odd number in the range 1 to 19 to specify the number of rows in the diamond and a character. You then call a function void printline(int ns, int nc, char c) that print one line with ns number of spaces, nc number of characters, and then ns number of spaces. Input the number of rows and the symbol char in the diamond> 5 * * *** ***** Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

Recursion int fact(int n) { if (n == 0) return 1; } else return n * fact(n - 1); // non-recursive version int fact(int n) { int f = 1; int i; for (i=n; i>1; i--) f = f*i; } return f; Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?

In-Class Exercise 3-4 Write a recursive function to compute the Fibonacci number, namely, besides main function, you must implement another Fibonacci function. Note that the Fibonacci number sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21. Your program should have the following input & output. Enter an integer> 0 Fib(0) = 0 Enter an integer> 6 Fib(6) = 8 Software infrastructure: support integration between physical space and virtual computing space, adapting to different smart environment, etc. Sensors: acquire user context such as techniques to estimate location from 802.11, etc. Context-aware applications: how can we use the context information to make application or everyday objects intelligent Security and privacy: user context information (where you are) should be protected. Human experience: user experience in ubicomp environment – how to evaluate it quantitatively or qualitatively? Ubiquitous data access: access information from any devices in any modalities from any networks Coping with uncertainty: sensors may have errors (GPS). Social computing: ubicomp is used in everyday physical / virtual activities. It will affect your social activities. How can we use it to improve your social lives?