19

Click here to load reader

Lec09-CS110 Computational Engineering

Embed Size (px)

DESCRIPTION

A keynote on Problem Solving using Computers

Citation preview

Page 1: Lec09-CS110 Computational Engineering

CS110: Arrays in C

Lecture 9V. Kamakoti

21st January 2008

Page 2: Lec09-CS110 Computational Engineering

Arrays

• String is array of characters• char a[10];• strcpy(a,”kamakoti”);• equivalent to• a[0] = ‘k’; a[1] = ‘a’; … a[7] = ‘i’; a[8] = ‘\0’

Page 3: Lec09-CS110 Computational Engineering

Arrays

• You can’t assign– a = “kamakoti”;– Except at time of declaration

• char a[ ] = “kamakoti”• When you would like to use string commands

inside the program– #include <string.h>

• Other commands– strlen() - Number of characters preceding NULL– strcat(a,b) - Concats the string “b” to “a”.

Page 4: Lec09-CS110 Computational Engineering

Example#include <stdio.h>#include <string.h> main() { char a[40],b[20]; strcpy(a,”kama”); strcpy(b,”koti”); strcat(a,b); printf(“%s\n”,a); printf(“%d %d\n”,strlen(a),strlen(b));}

Page 5: Lec09-CS110 Computational Engineering

Example

• kamakoti• 8 4

Page 6: Lec09-CS110 Computational Engineering

Other Arrays

• int A[100];• Can store 100 integers in sequence.• Let int be 4 bytes• A[0] is stored in 100 - 103• A[1] is stored in 104 - 107• A[2] is stored in 108 - 111• ….• A[i] is stored in (100 + i*4) - (100 + i*4 + 3)

Page 7: Lec09-CS110 Computational Engineering

Other Arrays

• This implies given where A[0] is storedsay B, you can compute where A[j] isstored.– (B + i*sizeof(int)) - (B+ i*sizeof(int) + sizeof(int)-1)

• Extend it for double A[100]– (B + i*sizeof(double)) - (B+ i*sizeof(double) + sizeof(double)-1)

Page 8: Lec09-CS110 Computational Engineering

Example

#include <stdio.h> main() { int A[100]; A[0] = 5; A[1] = 10; printf(“%d %d\n”,A[0],A[1]); printf(“%u %u %u %u”,A,&A[0],&A[2],&A[5]);}

Page 9: Lec09-CS110 Computational Engineering

Output

• 5 10• 3221224144 3221224144 3221224148

3221224164• scanf(“%d”,&A[3]); to input an element

of an array

Page 10: Lec09-CS110 Computational Engineering

Matrix

• int a[5][4] is a 5X4 matrix and you canstore upto 5 X 4 dimension.

• You can use the above for 3 X 3 also.• It is maximum dimension

Page 11: Lec09-CS110 Computational Engineering

Example

#include <stdio.h> main() { int A[10][10]; A[0][0] = 5; A[1][0] = 10; printf(“%d %d\n”,A[0][0],A[1][0]);}Output is5 10

Page 12: Lec09-CS110 Computational Engineering

Creative Exercise – 5The Celebrity problem

0 0 0 0 0 0 0 0

Celebrity is a person who is known to everyone butdoes not know anyone. To find a celebrity among “n”people by questioning them. You can ask only onetype of question. You can ask person X whether he

knows Y.

Page 13: Lec09-CS110 Computational Engineering

Solution

• Take any two person A and B• “Ask A if he knows B” - no one lies

– If yes then A is not a celebrity else B is not acelebrity

– Every question eliminates one– (n-1) questions eliminates n-1 person– The left out person CAN be a Celebrity– You need 2(n-1) questions more– Total 3(n -1) questions.

Page 14: Lec09-CS110 Computational Engineering

Creative Problem

• Suppose you are using a program thatreads in a large English text file as inputand processes it. The program for somereason does not like a set of words andwhenever it sees the same in your textfile it halts and outputs “Error”. It is soangry that it does not say what the wordis and which line it occurred or anyother info.

Page 15: Lec09-CS110 Computational Engineering

Creative Problem

• You do not have a list of offendingwords. Devise a strategy to identify thefirst offending word in your text. Expressthe efficiency of your strategy in termsof the number of words in the input textfile.

Page 16: Lec09-CS110 Computational Engineering

Solution

• The input file T is indeed erroneous. Theprogram gives error on input file T

• Suppose the text file T has n = 2k words• Split the text file T into two parts T1 and T2,

containing the first and the last n/2 wordsrespectively

• Give T1 to program (not T2 - why?)– If error then the offending word is in T1; else in T2

Page 17: Lec09-CS110 Computational Engineering

Solution

• Assume offending word in T1– Split T1 into T11 and T12 of n/4 words

each– Give T11 to program

• If error then offending word in T11 else in T12

• Finally you will land up with a file with 2words. Split them as one word each andgive them in order to the program to findout the first offending word.

Page 18: Lec09-CS110 Computational Engineering

Solution

• T(n) = T(n/2) + 1• = T(n/4) + 1 + 1• = T(n/2k) + k• = T(n/2logn) + log2n• = T(1) + log2n• = 1 + log2n

Page 19: Lec09-CS110 Computational Engineering

Thank You