Tutorial #6 Summer 2005. functions ( parameters list ) { body }

Slides:



Advertisements
Similar presentations
Arrays Write a program that first reads in 20 integers and then asks the user whether they want to display all the even integers or all the odd integers.
Advertisements

Tutorial #5 Summer while x = -2; while (x < 0) { printf("x is still negative :(\n"); x++; } printf("x is no longer negative.\n");
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
Tutorial #7 Summer sizeof int main() { char x; sizeof(x); sizeof(int); }
Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Create your own types: typedef #define N 3 typedef double scalar; /* note defs outside fns */ typedef scalar vector[N]; typedef scalar matrix[N][N]; /*
C programming---Arrays scalar: capable of holding a single data item aggregate variables: capable of holding a collections of values. Two kinds of aggregates.
Programming Arrays. Example 1 Write a program that reads 3 numbers from the user and print them in reverse order. How many variables do we need to store.
Functions, Pointers, Structures Keerthi Nelaturu.
Return function Random function Recursion function Function in C 1.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Computer Science 210 Computer Organization Arrays.
UNIT 14 Functions with Pointer Parameters.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Multidimensional Arrays Lecture 20 4/3/2002.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
Lecture 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
FUNCTION Dong-Chul Kim BioMeCIS UTA 12/7/
FUNCTIONS. Funtions  The heart of effective problem solving is problem decomposition.  breaking a problem into small, manageable pieces  In C, the.
CSCI 171 Presentation 6 Functions and Variable Scope.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Arrays as Function Arguments Array can be used as a function argument. E.g., #include int sum(int b[], int n) { int i, res; res = 0; for(i = 0; i < n;
Introduction to Programming Lecture 7: Repeating Statements.
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function.
CPT: Types/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to look at how new types can be created 6. User-defined.
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
Arrays and Pointers.
Functions. Flow of Control Review while for do while goto break & continue switch Relational operators short circuits.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
WEEK 8 Class Activities Lecturer’s slides.
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
8. ARRAYS. Aggregate variables Scalar variables –Single value Aggregate variables –Collection of values –Arrays: elements have the same type.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Arrays. Arrays are objects that help us organize large amounts of information.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Tutorial #4 Summer 2005.
CSE 220 – C Programming Pointers.
C Functions -Continue…-.
CS1010 Programming Methodology
Functions.
Array 9/8/2018.
מבוא כללי למדעי המחשב תרגול 2
Engineering Programming A
توابع. توابع دليل استفاده از تابع اجتناب از نوشتن تكراري خطوط يكسان در يك برنامه ساختار برنامه بهتر و قابل فهم ميشود استقلال زير روالها از تابع main.
Stack Memory 2 (also called Call Stack)
Lecture 10 Arrays.
CSC215 Homework Homework 04 Due date: Oct 14, 2016.
CS150 Introduction to Computer Science 1
A function with one argument
Dr Tripty Singh Tutorial for Fuctions
Week 6 CPS125.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
More Loops Topics Counter-Controlled (Definite) Repetition
Functions.
Presentation transcript:

Tutorial #6 Summer 2005

functions ( parameters list ) { body }

Max function int max(int a, int b) { if (a > b) return a; return b; }

Max function #include int main() { int x, y; scanf("%d%d", &x, &y); printf("max = %d\n", max(x, y)); return 0; }

Max function int max(int a, int b) { return (a > b ? a : b); }

Max function void max(int a, int b) { if (a > b) { printf("%d > %d\n", a, b); return ; } if (b > a) { printf("%d > %d\n", b, a); return ; } printf("%d = %d\n", a, b); }

Max function void max(int a, int b) { if (a > b) printf("%d > %d\n", a, b); else if (b > a) printf("%d > %d\n", b, a); else printf("%d = %d\n", a, b); }

absolute int absolute(int x) { return (x >= 0 ? x : -x); }

absolute int absolute(int x) { return (x >= 0 ? x : -x); }

Triangle area double triangle_area(double width, double height) { double area = width * height / 2; return area; }

Triangle area double triangle_area(double width, double height) { return (width * height / 2); }

messages void messages(int i) { switch(i) { case 0 : printf("first message\n"); break; case 1 : printf("second message\n"); break; case 2 : printf("third message\n"); }

power double power(double x, int y) /* y >= 0 */ { double res; for (res = 1; y > 0; y--) res *= x; return res; }

power double power(double x, int y) /* y >= 0 */ { double res = 1; for(; y > 0; y--) res *= x; return res; }

What happens here? void f(int x) { x++; } int main() { int x = 5; f(); printf(“x = %d\n”, x); return 0; }

Arrays - Counting int count(int arr[], int n, int val) { int i, cnt = 0; for (i = 0; i < n; i++) if (arr[i] == val) ++cnt; return cnt; }

Arrays - Assignments void f(int arr[], int n) { for(i = 0; i < n; i++) arr[i] = val; }

Programs #include int mult(int, int); int power(int, int); void power_table(int, int); int main() { int base, power; printf("Enter base and power of the power table :"); if (scanf("%d%d", &base, &power) < 2) { printf("Input Error"); return 1; } power_table(base, power); return 0; }

mult int mult(int x, int y) /* returns x*y */ { int res = 0; for (; y > 0; y--) res += x; return res; }

Power table void power_table(int n, int k) { int i, j ; for (i = 1; I <= k; i++) { for (j = 1; j <= n; j++) printf("%-5d",power(j, i)); putchar('\n'); } return ; }

Power int power(int x, int y) /* returns x to the power of y */ { int res = 1; for (; y > 0; y--) res = mult(res, x); return res; }

Variables Scope Static Global

scope #include int main() { int x = 10, y = 15; { int x = 20; printf(  x  %d, y = %d\n , x++, y++); } printf(  x  %d, y = %d\n , x, y); return 0; }

scope #include int main( void ) { int count = 1; for ( ; count <= 5; count++) { int count = 1; printf(“%d\n”, count); } return 0 ; }

scope #include int main( void ) { int count = 1; while (count <= 5) { int count = 1; printf(“%d\n”, count); count++; } return 0 ; }

scope int main() { int a = 1, b = 2, c = 3; printf(  %3d %3d %3d\n , a, b, c); { int b = 4; double c = 5.0; printf(  %d %d %f\n , a, b, c); a  b; { int c; c  b; printf(  %d %d %d\n , a, b, c); } printf(  %d %d %f\n , a, b, c); } printf(  %d %d %d\n , a, b, c); return 0; }

static #include void f(void) { static int cnt = 1; printf(  %d , cnt++); } int main() { int i; for(i = 0; i < 5; ++i) f(); for(i = 0; i < 5; ++i) f(); return 0; }

globals #include int x = 20, y; void f1(int x) { printf(  x = %d y = %d\n , x, y); x = y = 10; } void f2(int y) { printf(  x = %d y = %d\n , x, y); x = y = 15; } int main() { y = 5; f1(20); f2(y); printf(  x = %d y = %d\n , x, y); return 0; }

fun #include int x = 5, y = 6, z = 7; void printvals(int a, int b, int c, int x, int y,int z) { printf(  a = %d b = %d c = %d x = %d” “ y = %d z = %d , a, b, c, x, y, z); } void oof(int a, int b, int c) { int x; static int z; printvals(a, b, c, x, y, z); x = a; z = x; y++; { int a; a = x; b = y; c = z; printvals(a, b, c, x, y, z); } printvals(a, b, c, x, y, z); } int main() { int a = 0; x = 1; y = 2; z = 3; oof(7, 8, 9); x++; y++; z++; oof(x, y, z); return 0 ; }

arrays #include int main( void ) { int a[3];... return 0 ; } a[0] a[1]a[2]

arrays #include int main( void ) { int a[3][5];... return 0 ; } [rows][columns] ; a[0][0] a[1][3]

arrays #include int main( void ) { int a[3][5];... return 0 ; }

arrays #include int main( void ) { int a[3][5], i, j; for (i = 0; i < 3; i++) for (j = 0; j < 5; j++) scanf(  %d”, &a[i][j]); return 0 ; }

arrays #include int main( void ) { int a[3][5], i, j; for (j = 0; j < 5; j++) for (i = 0; i < 3; i++) scanf(  %d”, &a[i][j]); return 0 ; }

arrays #include int main( void ) { int a[3][5], j, k, sum = 0 ; for (j = 0; j < 3; j++) for (k = 0; k < 5; k++) scanf(  %d”, &a[j][k]); for (j = 0; j < 3; j++) for (k = 0; k < 5; k++) sum += a[j][k]; printf(  The sum is : %d\n , sum) ; return 0 ; }

arrays #include #define N 3 int main( void ) { int matrix[N][N], j, k, sum = 0; for (i = 0; i < N; i++) for (j = 0; j < N; j++) matrix[i][j] = i + j; for (i = 0; i < N; i++) { for (j = 0; j < N; j++) printf(  %d “, matrix[i][j]); printf(  \n  ) ; } return 0; }

typedef typedef double scalar ; typedef int length; int main( void ) { double a, b; scalar c, d; int i, j; length k, l; }

typedef #include #defineN10 #define M20 typedef double scalar ; typedef scalar vector[M] ; typedef scalar matrix[N][M] ; int main( void ) { vector a, b; /* a & b are both arrays */ matrix mat; /* mat is a two-dimensions array N  M */ } typedef scalar matrix[N][M] ; typedef vector matrix[N] ;