Lec10 PDF
Lec10 PDF
Lecture 10
V. Kamakoti
29th January 2008
Arrays
Matrix is
A two dimensional array
Array of one dimensional arrays
Stored in memory
Row-wise (Row Major order)
Column-wise (Column Major order)
Storage of matrix
A - 2X3 matrix has elements
A[0][0],A[0][1],A[0][2];
A[1][0],A[1][1],A[1][2];
A[2][0],A[2][1],A[2][2];
Assume it is an integer matrix and
A[0][0] is stored at 1000
Row-major order
Store the first row, then, the second,
then, the third and so on
A - 2X3 matrix has elements
A[0][0],A[0][1],A[0][2]; stored at 1000-1011
A[1][0],A[1][1],A[1][2]; stored 1012 - 1023
A[2][0],A[2][1],A[2][2]; stored 1024 - 1035
Column-major order
Store the first column, then, the second
column, then, the third and so on..
A - 2X3 matrix has elements
A[0][0],A[1][0],A[2][0]; stored at 1000-1011
A[0][1],A[1][1],A[2][1]; stored 1012 - 1023
A[0][2],A[1][2],A[2][2]; stored 1024 - 1035
The C compiler assumes Row major order.
Lab - 3
Write a program that reads in the
entries of a 3 by 3 matrix, and prints it
out in the form of a matrix. The entries
could be floating point entries too.
Solution
#include<stdio.h>
main() {
int i,j, a[3][3];
for (i = 0; i <= 2; i++) {
for (j = 0; j <=2; j++) {
printf(Enter a[%d][%d] \n,i,j);
scanf(%d,&a[i][j]);
}
}
}
What Happens
for (i = 0; i <= 2; i++) {
for (j = 0; j <=2; j++)
{
printf(Enter a[%d][%d] \n,i,j);
scanf(%d,&a[i][j]);
}
}
For every i, 0 <= i <= 2, j repeats thrice taking values
0,1 and 2. Therefore when i = 0 and j = 0, a[0][0]
shall be read in, when i = 0 and j = 1, a[0][1] shall be
read in and so on .
To Print
for (i = 0; i <= 2; i++) {
for (j = 0; j <=2; j++)
{
printf(%d ,a[i][j]);
}
printf(\n);
}
Print all elements of a given row on the same
line with a space in between. After one row is
printed go to the next line.
You have to
Repeat above for Floating point numbers
The effort would be to
Format the output properly
Let p be a floating point number
printf(%16f,..) prints a floating point with 16
spaces - if less then blanks are introduced.
Use the tab escape sequence \t to align.
This would be true for integers too.
Next in Lab - 3
Write a program that reads in orders of two
matrices and decides whether two such
matrices can be multiplied. Print out the
decision.
main() {
int row1,col1,row2,col2;
printf(Enter number of rows and columns in
Matrix 1 \n);
scanf(%d %d,&row1,&col1);
//Similarly for Matrix 2
if (col1 != row2) { printf(Not possible\n); exit(0);
} // To use exit() we need to include <stdlib.h>
Next in Lab - 3
Write a program that reads in two
arbitrary matrices and multiplies them.
Your output should be the two matrices
and the resulting product matrix.
Matrix Multiplication : Outline
main() {
1. declare all variables required
2. read in Matrix A
3. read in Matrix B
4. check if A and B are compatible to be multiplied
5. initialize Matrix C to have zeroes to begin with
6. multiply A and B to give C
7. print Matrix C
}
Of course, all the steps above have to
follow C languages syntax rules
Using Matrix Operations :
Read a Matrix
main(){
int a[11][11], b[11][11], c[11][11]; / *max size 10 by 10 */
int i,j,k;
int aRows, aCols, bRows, bCols, cRows, cCols;
bRows
Multiply two numbers
aRows
aCols
Sum of N products
Multiply Matrices and Print the
Result
/* multiply both the matrices and store in matrix c */
for(i =1; i <= aRows; i++)
for(j = 1; j <= bCols; j++)
for(k = 1; k <= aCols; k++)
c[i][j] += a[i][k]*b[k][j];
/* print matrix c */
for(i = 1; i <= aRows; i++){
for(j = 1; j <= bCols; j++) /* print a row */
printf("%d ", c[i][j]); /* notice missing \n */
printf("\n"); /* print a newline at the end a row */
}
} /* End of main program */
Points to Ponder
Some repetition in the program
Reading in matrix A seems to be similar to reading
in matrix B
Except for changes in the number of rows and columns
You will learn how to write functions in C later
Functions are written to avoid repeated actions
Example C program would look like
readMat(A, aRows, aCols);
readMat(B, bRows, bCols);
Function readMat( ) must perform the operations
desired
Celebrity Problem
(More efficient Solution)
(cont.)
Celebrity Problem
(Best Solution)
We can keep track of the tree thus obtained. Let us take a
case having 8 persons. The tree looks likes something below.
1 2 3 4 5 6 7 8
1 4 6 7
4 6 So totally 7 Qs asked.
This is a binary tree with a root,
leaves and children (at most 2).
4
Learn more about this later
Generalizing above
Given N people
N/2 Qs in level 1
N/4 Qs in level 2
1 Question in level k, where, N = 2k
So total is N(1/2 + 1/4 + 1/2k)
N (1/2) ( 1 - 1/2k)/(1/2)
N - N/2k = N - 1
Celebrity Problem
(Best Solution)
Clearly, if at all there is a celebrity, it can be only 4.
Also note that out of the 2n-2(= 6) questions asked to confirm
whether 4 is actually a celebrity, we have already asked
log2n (= 3) questions (Notice the yellow circles). So once we
maintain this tree, the number of additional questions that we
need to ask is 2n 2 log2n.
Therefore the total number of questions asked in this case is
n-1 + 2n 2 log2n = 3n 3 log2n.