Successfully reported this slideshow.
Upcoming SlideShare
×

# Lec16-CS110 Computational Engineering

171 views

Published on

A keynote on Problem Solving using Computers

Published in: Education
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### Lec16-CS110 Computational Engineering

1. 1. Solutions to Creative Problems Lecture 16 Feb 12, 2008
2. 2. Creative Question • Given a Black box that takes as input two symmetric matrices and outputs the product of the same, use the black box to multiple two arbitrary matrices.
3. 3. a b x c d 0 0 a b 0 0 c d a c 0 0 b d 0 0 e g f h x = p q r s 0 0 e f 0 0 g h e g 0 0 f h 0 0 p r 0 0 q s 0 0 0 0 0 0 u v wx
4. 4. Creative Question • Given a Black box that takes as input a matrix and outputs the square of the same, use the black box to multiple two arbitrary matrices.
5. 5. a b x c d 0 0 a b 0 0 c d e g 0 0 f h 0 0 e g f h x = p q r s 0 0 a b 0 0 c d e g 0 0 f h 0 0 p r 0 0 q s 0 0 0 0 0 0 u v wx
6. 6. Creative Question • Given a Black box that takes as input a lower triangular matrix and an upper triangular matrix and outputs the product of the same, use the black box to multiple two arbitrary square matrices.
7. 7. a b x c d 0 0 0 0 0 0 0 0 a b 0 0 c d 0 0 e g f h x = p q r s 0 0 e g 0 0 f h 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 p r 0 0 q s
8. 8. Creative Question • Given a Black box that takes as input two lower triangular matrices and outputs the product of the same, use the black box to multiple two arbitrary square matrices.
9. 9. a b x c d 000000 000000 000000 000000 00ab00 00cd00 x e g f h = 000000 000000 eg0000 f h0000 000000 000000 p q r s = 000000 000000 000000 000000 pq0000 r s0000
10. 10. Functions = outsourcing • Break large computing tasks into small ones • Helps you to build on what others have done • You and others write functions • When you want to build a program, find out how to use the function and use it. • Use standard functions provided by the library. • You are hidden from the implementation • Example – you don’t have to worry about how pow(m,n) is implemented • As engineers from different disciplines you will use and develop different set of functions
11. 11. Modular Programming Subprograms functions in C, C++, procedures and functions in Pascal facilitate modular programming Overall task is divided into modules Each module - a collection of subprograms a subprogram may be invoked at several points A commonly used computation hiding the implementation incorporating changes easier
12. 12. Example of function sets • String manipulation • Mathematical • Finite Element Method • Used in structural analysis by Mechanical, Civil, Aero, etc. for stress calculations etc. • Most function libraries cost a lot • Business opportunity – identify functions that are useful to you area of study, create libraries. • Functions for use in different software. • Say, functions for web services
13. 13. Basics • Function is a part of your program. • It cannot be a part of any other function • Main() is a function: it is the main function. Execution starts there or the control flow starts there • From there it can flow from one function to another, return after a computation with some values, probably, and then flow on. • Transfer of control is affected by calling a function • • • • • • • With a function call, we pass some parameters These parameters are used within the function A value is computed The value is returned to the function which initiated the call The calling function can ignore the value returned It could use it in some other computation A function could call itself, these are called recursive function calls
14. 14. Add function to your Program • A program was a set of variables, and assignments to variables • Now add function to it. • • • • Set of variables Some functions including main() Communicating values to each other Computing and returning values for each other • Instead of one long program, we now write structured program composed of functions
15. 15. Features • C program -- a collection of functions – function main ( ) - mandatory - program starts here. • C is not a block structured language – a function can not be defined inside another function – only variables can be defined in functions / blocks • Variables can be defined outside of all functions – global variables - accessible to all functions – a means of sharing data between functions - caution • Recursion is possible – a function can call itself - directly or indirectly
16. 16. Function template Return-type function-name(argument declarations) { declaration and statements return expression; }
17. 17. Function Definition in C return-type function-name (argument declarations) { variable/constant declarations and No function statements } declarations here! Arguments or parameters: the means of giving input to the function type and name of arguments are declared names are formal - local to the function Return Value: for giving the output value return ( expression ); -- optional Invoking a function: funct-name(exp1,exp2,…,expn) Matching the number and type of arguments
18. 18. Function Prototype • defines – the number of parameters, type of each parameter, – type of the return value of a function • used by the compiler to check the usage – prevents execution-time errors • function prototype of power function – int power ( int, int ); – no need for naming the parameters • function prototypes - given in the beginning
19. 19. Power Function #include <stdio.h> function prototype int power (int, int); -- Computes the nth power of base. main () { for ( int i = 0; i < 20; i ++ ) printf(“%d %d %dn”, i, power(3,i), power(-4,i);} int power (int base, int n) { int i, p = 1; for ( i = 1; i <= n ; i ++) p = p ∗ base; return p; } A block Invocation with arguments
20. 20. Calling Power Function with i=3 printf(“%d %d %dn”, i, power(3,i), power(-4,i);} 27 int power (int base, int n) { int i, p = 1; for ( i = 1; i <= n ; i ++) p = p ∗ base; return p; } -64 int power (int base, int n) { int i, p = 1; for ( i = 1; i <= n ; i ++) p = p ∗ base; return p; }
21. 21. Thank You • For Exam – Bring BOTH your Institute ID card and RFID card - else you shall not be permitted to write the exam. • All the Best