Lec21-CS110 Computational Engineering

33 %
67 %
Information about Lec21-CS110 Computational Engineering
Education

Published on March 3, 2014

Author: SriHarsha1508

Source: slideshare.net

Description

A keynote on Problem Solving using Computers

CS110 - Intro. To Computing Pointers !!!!!!!

Lab - 5 • Last Monday’s batch - come today • Last Friday’s batch - class cancelled because of server problem - come tomorrow - 2 PM

Let us Revise • What are these? – int *p, *q, *r; • What are these? – int a,b,c;

Let us Revise • What are these? – int *p, *q, *r; - p, q, r are address storing integer variables • What are these? – int a,b,c; - a, b, c are names of integer variables

Let us Revise • What are these? – int *p, *q, *r; – *p = 5; Content of address p is 5. • What are these? – int a,b,c; – &a = 0x1000; Address storing variable named a is 0x1000

Let us Revise • Given int *p; – &p is wrong - It means address of address. • Given int b; – *b is wrong - it means value of value

Let us Revise • Function Calls • Given int my_func(int *p); • This can be called as follows: – int a,b; b = my_func(&a); – int *a; int b; b = my_func(a); – Int *a, *b; *b = my_func(a);

Let us Revise • Function Calls • Given int my_func(int p); • This can be called as follows: – int a,b; b = my_func(a); – int *a; int b; b = my_func(*a); – Int *a, *b; *b = my_func(*a);

Let us Revise • Arrays – int a[100]; • Now a is a pointer to the start of the location - address of a variable • a[5] is a value of the 5th location - name of a variable

Passing Arrays • All our previous functions we passed only single values - not arrays. • In C array is passed “by reference”. – Implication • If you change the value inside the function it gets reflected in the calling function.

Example void modify(int a[]) { int count; printf(“n From the function, after modifying the values:n”); for (count = 0; count <= 2; count++) { a[count] = -9; printf(“a[%d] = %dn”, count, a[count]); } return; }

main() { int count, a[3]; void modify(int a[]); printf(“n From main, before calling the functionn”); for (count = 0; count <= 2; ++count) { a[count] = count + 1; printf(“a[%d] = %dn”,count, a[count]); } modify(); printf(“n From main, after calling the functionn”); for (count = 0; count <= 2; ++count) { printf(“a[%d] = %dn”,count, a[count]); } }

From main, before calling the function: a[0] = 1 a[1] = 2 a[2] = 3 main() { int count, a[3]; void modify(int a[]); printf(“n From main, before calling the functionn”); for (count = 0; count <= 2; ++count) { a[count] = count + 1; printf(“a[%d] = %dn”,count, a[count]); } modify(); printf(“n From main, before calling the functionn”); for (count = 0; count <= 2; ++count) { printf(“a[%d] = %dn”,count, a[count]); } }

Example void modify(int a[]) { int count; printf(“n From the function, after modifying the values:n”); for (count = 0; count <= 2; count++) { a[count] = -9; printf(“a[%d] = %dn”, count, a[count]); } return; } From the function, after modifying the values: a[0] = -9 a[1] = -9 a[2] = -9

main() { int count, a[3]; void modify(int a[]); printf(“n From main, before calling the functionn”); for (count = 0; count <= 2; ++count) { a[count] = count + 1; printf(“a[%d] = %dn”,count, a[count]); } modify(a); printf(“n From main, after calling the functionn”); for (count = 0; count <= 2; ++count) { printf(“a[%d] = %dn”,count, a[count]); } From main, after calling the function: } a[0] = -9 a[1] = -9 a[2] = -9

Multidimensional Arrays main() { int *nrows, *ncols; int a[30][30]; void readinput(int a[][30], int *,int *); void writeoutput(int a[][30], int, int); readinput(a,nrows,ncols); printf(“n Number of rows %dn”,*nrows); printf(“n Number of columns %dn”,*ncols); writeoutput(a,*nows,*ncols); }

void readinput(int a[][30], int *m, int *n) { int i, j; printf(“Number of rowsn”); scanf(“%d”,m); printf(“Number of columnsn”); scanf(“%d”,n); for (i = 0; i < *m; i++) for (j = 0; j < *n; j++) scanf(“%d”, &a[i][j]) }

void writeoutput(int a[][30], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) printf(“%4d”, a[i][j]); printf(“n”); } return; }

int u = 3; int v; int *pu; int *pv; pu = &u; v = *pu; pv = &v; When you print * and & versions of them? What happens?

Add a comment

Related presentations

Related pages

Lec21-CS110 Computational Engineering - Education

1. CS110 - Intro. To Computing Pointers !!!!! 2. Lab - 5 • Last Monday’s batch - come today • Last Friday’s batch - class cancelled because of ...
Read more

Lec14-CS110 Computational Engineering - Education

International Journal of Computational Engineering Research(IJCER) ... Lec21-CS110 Computational Engineering. Lec04-CS110 Computational Engineering.
Read more

Computational Engineering | LinkedIn

View 4978 Computational Engineering posts, presentations, experts, and more. Get the professional knowledge you need on LinkedIn.
Read more