Download - Lec16-CS110 Computational Engineering
![Page 1: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/1.jpg)
Solutions to CreativeProblems
Lecture 16Feb 12, 2008
![Page 2: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/2.jpg)
Creative Question
• Given a Black box that takes as inputtwo symmetric matrices and outputs theproduct of the same, use the black boxto multiple two arbitrary matrices.
![Page 3: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/3.jpg)
dcba
hfge
srqp
x =
0000
dbca0000
dcba
hgfe
hfge0000
0000
x
xw00vu0000sr00qp
![Page 4: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/4.jpg)
Creative Question
• Given a Black box that takes as input amatrix and outputs the square of thesame, use the black box to multiple twoarbitrary matrices.
![Page 5: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/5.jpg)
dcba
hfge
srqp
x =
0000
hfge0000
dcba
dcba
hfge0000
0000
x
xw00vu0000sr00qp
![Page 6: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/6.jpg)
Creative Question
• Given a Black box that takes as input alower triangular matrix and an uppertriangular matrix and outputs theproduct of the same, use the black boxto multiple two arbitrary squarematrices.
![Page 7: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/7.jpg)
dcba
hfge
srqp
x =
0000
dcba0000
0000
hfge
00000000
0000
x
sr00qp0000000000
![Page 8: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/8.jpg)
Creative Question
• Given a Black box that takes as inputtwo lower triangular matrices andoutputs the product of the same, usethe black box to multiple two arbitrarysquare matrices.
![Page 9: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/9.jpg)
dcba
hfge
srqp
x =
x=
00dc0000ba00000000000000000000000000
0000000000000000hf0000ge000000000000
0000sr0000qp000000000000000000000000
![Page 10: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/10.jpg)
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 thelibrary.
• 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 youwill use and develop different set of functions
![Page 11: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/11.jpg)
Modular Programming
Subprogramsfunctions in C, C++, procedures and functions in
Pascalfacilitate modular programming
Overall task is divided into modulesEach module - a collection of subprograms
a subprogram may be invoked at several pointsA commonly used computation
hiding the implementationincorporating changes
easier
![Page 12: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/12.jpg)
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
![Page 13: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/13.jpg)
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
![Page 14: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/14.jpg)
Add function to your Program
• A program was a set of variables, andassignments 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 writestructured program composed of functions
![Page 15: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/15.jpg)
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
![Page 16: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/16.jpg)
Function template
Return-type function-name(argumentdeclarations)
{declaration and statementsreturn expression;
}
![Page 17: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/17.jpg)
Function Definition in C
return-type function-name (argument declarations){ variable/constant declarations and
statements }Arguments or parameters:
the means of giving input to the functiontype and name of arguments are declarednames are formal - local to the function
Return Value: for giving the output valuereturn ( expression ); -- optional
Invoking a function: funct-name(exp1,exp2,…,expn)
Matching thenumber and typeof arguments
No functiondeclarations here!
![Page 18: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/18.jpg)
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
![Page 19: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/19.jpg)
Power Function#include <stdio.h>int power (int, int);main () {for ( int i = 0; i < 20; i ++ )
printf(“%d %d %d\n”, 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;}
-- Computes the nth
power of base.
function prototype
Invocation witharguments
A block
![Page 20: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/20.jpg)
Calling Power Function with i=3printf(“%d %d %d\n”, 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;
}
int power (int base, int n) {int i, p = 1;for ( i = 1; i <= n ; i ++)
p = p ∗ base;return p;
}
27 -64
![Page 21: Lec16-CS110 Computational Engineering](https://reader033.vdocuments.site/reader033/viewer/2022052523/555e3731d8b42aad058b4953/html5/thumbnails/21.jpg)
Thank You
• For Exam– Bring BOTH your Institute ID card and RF-
ID card - else you shall not be permitted towrite the exam.
• All the Best