Functions - Experimental Techniques - Lecture Slides, Slides of Electrical Engineering

These are the key points discussed in the given Slides : Functions, Arrays, Statement, Comparisons, Remember to Double, Functional Notes, Declaration, Match, Number of Arguments, Value Out

Typology: Slides

2012/2013

Uploaded on 07/24/2013

bulla.baba
bulla.baba 🇮🇳

5

(7)

87 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C-Programming, Part 2 02/28/2008
Lecture 13 1
CProgramming,continued
Functions
Arrays
Theifstatement(andcomparisons)
Thefollowingvarietymightbeused
if (i < 2)
if (i <= 2)
if (i >= -1)
if (i == 4) // note difference between == and =
if (x == 1.0)
if (fabs(x) < 10.0)
if (i < 8 && i > -5) // && = and
if (x > 10.0 || x < -10.0) // || = or
Remembertodoubleup==,&&,||
Functionsdostuff
#include <stdio.h>
#include <math.h>
double gauss(double x, double amplitude, double center, double sigma);
int main()
{
double gaussval1,gaussval2;
double xval=-1.4,ampl=100.0,ctr=0.1, sig=2.0;
gaussval1 = gauss(1.5,10.0,0.0,2.0);
gaussval2 = gauss(xval,ampl,ctr,sig);
printf("Gaussval1 = %f; Gaussv al2 = %f\n",gaussval1,gaussval2) ;
return 0;
}
double gauss(double x_val, doubl e amplitude, double center, doub le sigma)
{
return amplitude*exp(-0.5*pow((x_val -center)/sigma,2));
}
FunctionalNotes
Inthepreviousprogram:
wehaveafunctiondeclarationbeforemain(),specifyingthe
argumenttypes,withtemporary(descriptiveisgood)names
notstrictlynecessary,butaidsincheckingerrorsduringcompilation
wecanpasseithernumericalargumentsorvariables(oramix)
namesdon’thavetomatchfromdeclarationtouseinmain()
tonamesinfunction()
buthavetomatchwithinfunction(notexvs.xvalvs.x_val)
canpassanynumberofargumentsofanytypeintofunction
limitedtoasinglevalueout
Docsity.com
pf3
pf4

Partial preview of the text

Download Functions - Experimental Techniques - Lecture Slides and more Slides Electrical Engineering in PDF only on Docsity!

C‐Programming,

continued Functions Arrays

The^ if^ statement

(and^ comparisons)

•^ The^ following

variety^ might

be^ used

-^ if^ (i^ <^ 2) –^ if^ (i^ <=^ 2) –^ if^ (i^ >=^ -1) –^ if^ (i^ ==^ 4)^

//^ note^ difference

between^ ==^ and^ =

-^ if^ (x^ ==^ 1.0) –^ if^ (fabs(x)^ <^ 10.0) –^ if^ (i^ <^8 &&^ i^

>^ -5)^ //

&&^ =^ and

-^ if^ (x^ >^ 10.0^ ||

x^ <^ -10.0)^ //

||^ =^ or

•^ Remember

to^ double^ up

==,^ &&,^ ||

Functions

do^ stuff

#include^ <stdio.h>#include^ <math.h>double^ gauss(double

x,^ double^ amplitude,

double^ center,^ double

sigma);

int^ main(){double^ gaussval1,gaussval2;double^ xval=-1.4,ampl=100.0,ctr=0.1,sig=2.0;gaussval1^ =^ gauss(1.5,10.0,0.0,2.0);gaussval2^ =^ gauss(xval,ampl,ctr,sig);printf("Gaussval

=^ %f;^ Gaussval^

=^ %f\n",gaussval1,gaussval2); return^ 0;} double^ gauss(double

x_val,^ double^ amplitude,

double^ center,^ double

sigma)

{return^ amplitudeexp(-0.5pow((x_val-center)/sigma,2));}

Functional

Notes

•^ In^ the^ previous

program: – we have a function^ declaration^ before

main(),^ specifying

the

argument^ types,^ with

temporary^ (descriptive

is^ good)^ names

-^ not^ strictly^ necessary,

but^ aids^ in^ checking^ errors

during^ compilation

-^ we^ can^ pass^ either

numerical^ arguments

or^ variables^ (or^ a^ mix)

-^ names^ don’t^ have

to^ match^ from^ declaration

to^ use^ in^ main()

to^ names^ in^ function()^ •^ but^ have^ to^ match

within^ function^ (note

x^ vs.^ xval^ vs.^ x_val

-^ can^ pass^ any^ number

of^ arguments^ of^ any

type^ into^ function

-^ limited^ to^ a^ single

value^ out

Docsity.com

Arrays

•^ We^ can^ hold^ more

than^ just^ one^ value

in^ a^ variable

-^ but^ the^ program

needs^ to^ know^ how

many^ places^ to^ save

in

memory • Examples: – we^ can^ either^ say^ how

many^ elements^ to

allow^ and^ leave

them^ unset;^ say^ how

many^ elements^ and

initialize^ all

elements^ to^ zero;^ leave

out^ the^ number^ of

elements^ and

specify^ explicitly;^ specify

number^ of^ elements

and^ contents

-^ character^ arrays

are^ strings

-^ strings^ must^ end

in^ ‘\0’^ to^ signal^

the^ end

-^ must^ allow^ room:

char^ name[4]=“Bob” • fourth element is^ ‘\0’^ by^ default

int^ i[8],^ j[8]={0},

k[]={9,8,6,5,4,3,2,1,0}; double^ x[10],^ y[10000]={0.0},

z[2]={1.0,3.0}; char^ name[20],^ state[]=“California”;

Indexing^ Arrays • Index array integers,^ starting^ with^ zero • Sometimes initialize^ in^ loop^ (z[]^ above) • String assignment^ awkward^ outside^ of

declaration^ line

-^ #include <string.h>

provides^ “useful”^ string

routines

int^ i,j[8]={0},k[]={2,4,6,8,1,3,5,7};double^ x[8]={0.0},y[2]={1.0,3.0},z[8];char^ name[20],state[]="California";for^ (i=0;^ i<8;^ i++){z[i]^ =^ 0.0;printf(”j[%d]^ =

%d,^ k[%d]^ =^ %d\n",i,j[i],i,k[i]); }name[0]='T';name[1]='o';name[2]='m';name[3]^ =^ '\0';printf("%s^ starts

with^ %c^ and^ lives

in^ %s\n",name,name[0],state);

Memory^

Allocation

in^ Arrays

•^ state[]=“California”;

•^ name[11]=“Bob”;

-^ empty^ spaces^ at

the^ end^ could^ contain

any^ random^ garbage

•^ int^ i[]^ = {9,8,7,6,5,4,3,2};

-^ indexing^ int[8]

is^ out^ of^ bounds,^ and

will^ either^ cause^ a

segmentation^ fault

(if^ writing),^ or^ return

garbage^ (if^ reading)

C^ a^ l^ i^ f^

o^ r^ n^ i^ a^

\

B^ o^ b^ \0^9 8 7 6

#define^ to

ease^ the^

coding

-^ #define^ comes

before^ the^ function^

definitions,^ up^ with^ the

#include^ statements^ –^ note^ no^ semi‐colons^ –^ just^ a^ text^ replacement

process:^ any^ appearance

of^ NPOINTS^ in^ the

source^ code^ is^ replaced

by^10

-^ Convention^ to^ use^ all

CAPs^ to^ differentiate^ from

normal^ variables^ or

commands – Now^ to^ change^ the^ number

of^ points^ processed^ by

that^ program,^ only

#define^ NPOINTS^10 #define^ NDIMS^3 int^ main(){int^ shots[NPOINTS],hits[NPOINTS],flag[NDIMS];double^ coords[NDIMS][NPOINTS],time_hit=[NPOINTS];…}^ have^ to^ modify^ one^ line

Docsity.com

Notes^ on^

mm3x

•^ The^ function

is^ made^ to^ deal

with^1 ‐d^ instead

of^2 ‐d^ arrays^ –^9 elements^

instead^ of^ 3x3 – it could have^ been^ done^ either

way

•^ There^ is^ a^ pointer,

*cptr^ being

used

–^ by^ specifying

cptr^ as^ a^ double

pointer,^ and

assigning^ its^ address

(just^ cptr)^ to

c,^ we^ can

stock^ the^ memory

by^ using^ “pointer

math”

–^ cptr^ is^ the^ address;

*cptr^ is^ the^

value^ at^ that

address – just^ like^ &x_val

is^ an^ address,^

while^ x_val

contains^ the^ value

Using^ mm3x

•^ passing^ just

the^ names^ (addresses)

of^ the

arrays^ –^ defining^ a^ and

b,^ just^ making

space^ for^ c

#include^ <stdio.h>void^ mm3x3(double^ a[],^ –^ note function declaration before main

double^ b[],^ double

c[]); int^ main(){double^ a[]={1.0,

2.0,^ 3.0,^ 4.0,^ 5.0,

6.0,^ 7.0,^ 8.0,^ 9.0};

double^ b[]={1.0,^

2.0,^ 3.0,^ 4.0,^ 5.0,

4.0,^ 3.0,^ 2.0,^ 1.0};

double^ c[9];mm3x3(a,b,c);printf("c^ =^ %f^ %f

%f\n",c[0],c[1],c[2]); printf("^ %f^ %f

%f\n",c[3],c[4],c[5]); printf("^ %f^ %f

%f\n",c[6],c[7],c[8]); return^ 0;}

Another^ way

to^ skin^ the

cat

•^ Here,^ we^ define

the^ arrays^ as

2 ‐d,^ knowing

that^ in^ memory

they^ will^ still

be^1 ‐d

–^ we^ will^ get^ compiler

warnings,^ but^

the^ thing

will^ still^ work – not^ a^ recommended

approach,^ just

presented

here^ for^ educational

purposes

Note that we could replace

a^ with^ &a[0][0]

double^ a[3][3]={{1.0,

2.0,^ 3.0},{4.0, 5.0,^ 6.0},{7.0, 8.0,^ 9.0}};

double^ b[3][3]={{1.0,

2.0,^ 3.0},{4.0, 5.0,^ 4.0},{3.0, 2.0,^ 1.0}};

double^ c[3][3];mm3x3(a,b,c);

Docsity.com