0% found this document useful (0 votes)
7 views

Multidimensional Array & Strings

multi dimensional array
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Multidimensional Array & Strings

multi dimensional array
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CSE101 –

Problem Solving &


Programming in C
Unit III

07-01-2021 Ref: Programming with C, Byron S. Gottfried.


Multi-Dimensional Arrays

07-01-2021 Ref: Programming with C, Byron S. Gottfried. 2


Matrix Multiplication
𝑎11 𝑎12 𝑎13 𝑏11 𝑏12 𝑏13
𝑎11 𝑎12 𝑏11 𝑏12 a= 𝑎21 𝑎22 𝑎23 b= 𝑏21 𝑏22 𝑏23
a= 𝑎 𝑎22 b= 𝑎31 𝑎32 𝑎33 𝑏31 𝑏32 𝑏33
21 𝑏21 𝑏22
c= a x b
c= a x b 𝑐11 = 𝑎11 ∗ 𝑏11 + 𝑎12 ∗ 𝑏21 + 𝑎13 ∗ 𝑏31
𝑐12 = 𝑎11 ∗ 𝑏12 + 𝑎12 ∗ 𝑏22 + 𝑎13 ∗ 𝑏32
𝑐11 = 𝑎11 ∗ 𝑏11 + 𝑎12 ∗ 𝑏21 𝑐13 = 𝑎11 ∗ 𝑏13 + 𝑎12 ∗ 𝑏23 + 𝑎13 ∗ 𝑏33
𝑐12 = 𝑎11 ∗ 𝑏12 + 𝑎12 ∗ 𝑏22 𝑐21 = 𝑎21 ∗ 𝑏11 + 𝑎22 ∗ 𝑏21 + 𝑎23 ∗ 𝑏31
𝑐21 = 𝑎21 ∗ 𝑏11 + 𝑎22 ∗ 𝑏21 𝑐22 = 𝑎21 ∗ 𝑏12 + 𝑎22 ∗ 𝑏22 + 𝑎23 ∗ 𝑏32
𝑐22 = 𝑎21 ∗ 𝑏12 + 𝑎22 ∗ 𝑏22 𝑐23 = 𝑎21 ∗ 𝑏13 + 𝑎22 ∗ 𝑏23 + 𝑎23 ∗ 𝑏33

07-01-2021 Ref: Programming with C, Byron S. Gottfried. 3


Matrix Multiplication
• For a 2x2 matrix
• 𝑐𝑖𝑗 = 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 + 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 𝑤ℎ𝑒𝑟𝑒 1 ≤ 𝑖 ≤ 2, 1 ≤ 𝑗 ≤ 2, 1 ≤ 𝑘 ≤ 2
• For 3x3 matrix multiplication
• 𝑐𝑖𝑗 = 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 + 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 + 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 𝑤ℎ𝑒𝑟𝑒 1 ≤ 𝑖 ≤ 3, 1 ≤ 𝑗 ≤ 3, 1 ≤ 𝑘 ≤ 3
• For nxn matrix multiplication
• 𝑐𝑖𝑗 = 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 + 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 + 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 + ⋯ 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗
𝑤ℎ𝑒𝑟𝑒 1 ≤ 𝑖 ≤ 𝑛, 1 ≤ 𝑗 ≤ 𝑛, 1 ≤ 𝑘 ≤n
• In general, 𝑐𝑖𝑗 can be written as
• 𝑐𝑖𝑗 = 𝑐𝑖𝑗 + 𝑎𝑖𝑘 ∗ 𝑏𝑘𝑗 𝑤ℎ𝑒𝑟𝑒 1 ≤ 𝑖 ≤ 𝑛, 1 ≤ 𝑗 ≤ 𝑛, 1 ≤ 𝑘 ≤n

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

You might also like