Vous êtes sur la page 1sur 4

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #18, September 6, 2011

Please switch off your mobile phones.

Announcements
Quiz on lab days from 5th to 9th September in Lab at 2:00 PM.
We will try again today. If it does not work, then the quiz will be during tutorial on 7th. No makeup for the quiz.

Mid-semester exam at 7:30 AM on 14th September, 2011.

Lec-18

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Recursion
An example of mutual recursion An example of double recursion

Lec-18

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Passing of 2-D Arrays to Functions


Second size is required in the argument list. For example:
void mul (int a [ ] [3], int b [ ] [2], int c [ ] [2], int size) { int i, j, k; for (i = 0; i < size; i++) for (k = 0; k < 2; k++) { c [i] [k] = 0; for (j = 0; j < 3; j++) c [i][k] = c [i][k] + a[i][j] * b[j][k]; } }

Lec-18

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Passing of 2-D arrays to functions (contd.)


int main () { int a [2] [3], b [3] [2], c [2] [2]; int i, j, k; ( ) for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) scanf (%d, &a[i][j]); for (j = 0; j < 3; j++) for (k = 0; k < 2; k++) scanf (%d, &b[j][k]); mul (a, b, c, 2); for (i = 0; i < 2; i++) ( ; ; ) { for (k = 0; k < 2; k++) printf (%d\t, c [i][k]); printf (\n); } }
Lec-18 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 4

Strings
A string is an array of characters characters. Declaration:
char name [30];

A string is terminated with the null character, \0. String can be initialized in double quotes as:
char place [ ] = IIT Kanpur;

The array place would be allocated 10+1 (11) memory areas. The last area would have \0 null character.

Lec-18

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

String input and output


Use %s specification char name[30]; scanf (%s, name); printf (% name); i f (%s, ) Reading will stop as soon as the first white-space character (blank, tab, return) is encountered Notice, no & in front of variable, name. All the characters read are stored in the array starting from 0th element. After last character, an additional array element is used to store \0 character. (So name can only store at most 29 characters.) ( y ) If size of array is larger, rest of the characters are undefined. If size of array is smaller, we will be writing beyond array boundary.

A string is printed up to the null character.


Lec-18 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 6

Any Questions?

Lec-18

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Vous aimerez peut-être aussi