07-01-2021 Ref: Programming with C, Byron S. Gottfried. 4
Matrix Multiplication • It can be inferred that ,Three loops are required • One to control i • One to control j • One to control k • It can be inferred that, each change in i, there is change in j for n times • Similarly for every change in j, there is change in k for n times • Finally it can be concluded that all three loops are nested with i as outer most loop, k forming the inner most loop, and j loop is between i and k loop. 07-01-2021 Ref: Programming with C, Byron S. Gottfried. 5 Logic for multiplying two matrices for (i=0;i<n;i++) { for(j=0;j<n;j++) { for(k=0;k<n;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } }
07-01-2021 Ref: Programming with C, Byron S. Gottfried. 6
Additional Programs • Write a program to multiply matrices of different sizes • Write a program to check whether the given matrix is a • Symmetric matrix • Skew symmetric matrix • Identity matrix • Diagonal matrix • Write a program to find the trace of given matrix.
07-01-2021 Ref: Programming with C, Byron S. Gottfried. 7
Strings
07-01-2021 Ref: Programming with C, Byron S. Gottfried. 8
Strings • Strings are basically arrays that store characters • General syntax for declaring a string • char variableName[size] • E.g., char name[30] • Unlike an integer array which requires a loop to handle the input, string does not require a loop. (but can be used if required) • scanf can be used to get input for string • Use %s as the control string • Don’t use & before the string variable scanf(“%s”, name);
07-01-2021 Ref: Programming with C, Byron S. Gottfried. 9
String – Input with embedded space • scanf treats space as well as newline character as termination character • scanf function cannot handle embedded space • If the input is given as raj kumar it will store only raj • To handle space as part of input need to use scanset in control string • scanf(“%[^\n]s”, name); • Above statement can take string with embedded space
07-01-2021 Ref: Programming with C, Byron S. Gottfried. 10
String – Multi line input • To handle text with multiple lines we can use a different symbol for termination • scanf(“%[^#]s”, name); • Above statement can take multiple lines as input • # is used for termination
07-01-2021 Ref: Programming with C, Byron S. Gottfried. 11
String vs integer array • How to create a string with initial value? • char str[15]=“abcd fgh”; • What is the difference between initializing int array and char array? • How string is stored in array? a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10] a[11] a[12] a[12] a[13] a[14] a b c d f g h \0
• All strings end with null character
• If a character array doesn’t end with null character it cannot be called as string 07-01-2021 Ref: Programming with C, Byron S. Gottfried. 12 String • Is str a string? • char str[30] = {‘a’, ‘b’, ‘c’, ‘d’}; • What will be the output? • char str[30]={‘a’, ‘b’, ‘c’, ‘\0’, ‘d’, ‘e’};
07-01-2021 Ref: Programming with C, Byron S. Gottfried. 13
References • Byron S. Gottfried, “Programming with C”, McGraw Hill, Third Edition, 2017. • Dromey R.G, “How to Solve it by Computers”, Pearson Education, 2012.
07-01-2021 Ref: Programming with C Byron S. Gottfried. 14