Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer, PIEAS
Revision of Arrays
• Array
– Structures of related data items
– Group of consecutive memory locations
– Same name and type
– e.g int c[12];
• To refer to an element, specify
– Array name
– Position number
• Format:
arrayname[ position number ]
– First element at position 0
– n element array named c:
• c[ 0 ], c[ 1 ]...c[ n – 1 ]
Name of array (Note
that all elements of this
array have the same
name, c)
Position number of
the element within
array c
c[6]
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Dr. Yousaf, PIEAS
Revisions of Arrays
Dr. Yousaf, PIEAS
How to declare array?
typename variablename[size]
int marks[6]={36,78,29,36,7,99};
To print an array, we need loop.
for (i = 0; i <6;i++)
printf("Marks are %dn",marks[i]);
To take the elements of arrays from a user (scanf), we
need loop.
for (i = 0; i <6;i++)
{
printf(“Please enter the marks of studentsn”);
scanf(“ %d",&marks[i]);
}
Revisions of Arrays
Dr. Yousaf, PIEAS
Int a[4] = {2, 4, 3, 10};
We can use a[0]=10;
x=a[2];
a[3]=a[2]; etc.
printf( "%d", a[ 0 ] );
We can declare more than one array
in single line as:
int b[ 100 ], x[ 27 ];
If not enough initializers, rightmost elements become
0
int n[ 5 ] = { 1 } // All other elements would be 0
C arrays have no bounds checking
How to store and print a single value
Dr. Yousaf, PIEAS
#include <stdio.h>
int main( )
{
int x;
x = 5;
printf(“%d", x);
getchar();
return 0;
}
#include<stdio.h>
int main()
{
int marks[6]={36,78,29,89,7,99};
int i;
for (i = 0; i <6;i++)
printf("Marks are %dn",marks[i]);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
How to Store and Print an Array?
Dr. Yousaf, PIEAS
How to store these values?
1 2 3
4 5 6
7 8 9
10 11 12
How to print these values?
How to Store and Print a matrix?
2-D Arrays
2-D Arrays
Nesting in Loops
Dr. Yousaf, PIEAS
2-D Arrays
• Arrays in C can have virtually as many dimensions as
you want.
• Definition is accomplished by adding additional
subscripts when it is defined.
• For example:
– int a [4] [3] ; // 4 Rows, 3 Columns
– defines a two dimensional array
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]
a[3][0] a[3][1] a[3][2]
Dr. Yousaf, PIEAS
2-D Arrays
Dr. Yousaf, PIEAS
How to store these values?
1 2 3
4 5 6
7 8 9
10 11 12
How to print these values?
#include<stdio.h>
int main()
{
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} };
int row, col;
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%d", a[row][col]);
}
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
How to Print 2-D Arrays?
123456789101112
We want output in Matrix Form
Dr. Yousaf, PIEAS
Output of the Program
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%dt",a[row][col]);
}
}
Output:
1 2 3 4 5 6 7 8 9
10 11 12
Dr. Yousaf, PIEAS
Output with Tabs
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%dt", a[row][col]);
}
printf(“n”);
}
Dr. Yousaf, PIEAS
Output in Matrix Form
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%dt", a[row][col]);
}
printf(“n”);
}
Dr. Yousaf, PIEAS
Output in Matrix Form
Output:
1 2 3
4 5 6
7 8 9
10 11 12
#include<stdio.h>
int main()
{
int a[4] [3];
int row, col;
for (row = 0; row <=3; row++)
{
printf("Enter 3 elements of row %dn", row + 1);
for (col = 0; col <=2; col++)
{
scanf("%d",&a[row][col]);
}
}
//Rest of the code goes here
Dr. Yousaf, PIEAS
How to scan 2-D Arrays?
Initializing Multidimensional Arrays
• The following initializes a[4][3]:
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} };
• Also can be done by:
int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
– is equivalent to
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
...
a[3][2] = 12;
Dr. Yousaf, PIEAS
Multiple-Subscripted Arrays
• Multiple subscripted arrays
– Tables with rows and columns (m by n array)
– Like matrices: specify row, then column
Row 0
Row 1
Row 2
Column 0 Column 1 Column 2 Column 3
a[ 0 ][ 0 ]
a[ 1 ][ 0 ]
a[ 2 ][ 0 ]
a[ 0 ][ 1 ]
a[ 1 ][ 1 ]
a[ 2 ][ 1 ]
a[ 0 ][ 2 ]
a[ 1 ][ 2 ]
a[ 2 ][ 2 ]
a[ 0 ][ 3 ]
a[ 1 ][ 3 ]
a[ 2 ][ 3 ]
Row subscript
Array name
Column subscript
Dr. Yousaf, PIEAS
Multiple-Subscripted Arrays
• Initialization
– int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
– Initializers grouped by row in braces
– If not enough, unspecified elements set to zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
• Referencing elements
– Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );
1 2
3 4
1 0
3 4
Dr. Yousaf, PIEAS
Multidimensional Arrays
• Array declarations read right-to-left
• int a[10][3][2];
• “a is array of ten arrays of three arrays of two (type
ints)”. In memory
2 2 2
3
2 2 2
3
2 2 2
3
...
10
Dr. Yousaf, PIEAS
Some Examples
Dr. Yousaf, PIEAS
Addition of Two Matrices
#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} }, Y[2][2] =
{ {5,6},{7,8} };
int add[2][2];
Dr. Yousaf, PIEAS
Addition of Two Matrices
#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} }, Y[2][2] =
{ {5,6},{7,8} };
int add[2][2];
int i, j;
printf("ntAddition of two matrices
is");
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
add[i][j] = X[i][j] + Y[i][j];
printf("%dt", add[i][j]);
}
printf("n");
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Multiplication of Two Matrices
#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} },
Y[2][2] = { {5,6},{7,8} };
int mul[2][2];
int i, j, k, sum = 0;
Dr. Yousaf, PIEAS
Multiplication of Two Matrices
#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} },
Y[2][2] = { {5,6},{7,8} };
int mul[2][2];
int i, j, k, sum = 0;
printf("nntMultiplications
of two matrices is");
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
for (k=0; k<2; k++)
{
sum = sum + (X[i][k]*Y[k][j]);
}
mul[i][j] = sum;
printf("%dt", mul[i][j]);
sum = 0;
}
printf("n");
} getchar(); return 0; }
Dr. Yousaf, PIEAS
Try to write the program for the followings
Dr. Yousaf, PIEAS
(1)Accept 2x2 matrix. Determine its adjoint.
List goes on …

C Language Lecture 10

  • 1.
    Computing Fundamentals Dr. MuhammadYousaf Hamza Deputy Chief Engineer, PIEAS
  • 2.
    Revision of Arrays •Array – Structures of related data items – Group of consecutive memory locations – Same name and type – e.g int c[12]; • To refer to an element, specify – Array name – Position number • Format: arrayname[ position number ] – First element at position 0 – n element array named c: • c[ 0 ], c[ 1 ]...c[ n – 1 ] Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c c[6] c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Dr. Yousaf, PIEAS
  • 3.
    Revisions of Arrays Dr.Yousaf, PIEAS How to declare array? typename variablename[size] int marks[6]={36,78,29,36,7,99}; To print an array, we need loop. for (i = 0; i <6;i++) printf("Marks are %dn",marks[i]); To take the elements of arrays from a user (scanf), we need loop. for (i = 0; i <6;i++) { printf(“Please enter the marks of studentsn”); scanf(“ %d",&marks[i]); }
  • 4.
    Revisions of Arrays Dr.Yousaf, PIEAS Int a[4] = {2, 4, 3, 10}; We can use a[0]=10; x=a[2]; a[3]=a[2]; etc. printf( "%d", a[ 0 ] ); We can declare more than one array in single line as: int b[ 100 ], x[ 27 ]; If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 1 } // All other elements would be 0 C arrays have no bounds checking
  • 5.
    How to storeand print a single value Dr. Yousaf, PIEAS #include <stdio.h> int main( ) { int x; x = 5; printf(“%d", x); getchar(); return 0; }
  • 6.
    #include<stdio.h> int main() { int marks[6]={36,78,29,89,7,99}; inti; for (i = 0; i <6;i++) printf("Marks are %dn",marks[i]); getchar(); return 0; } Dr. Yousaf, PIEAS How to Store and Print an Array?
  • 7.
    Dr. Yousaf, PIEAS Howto store these values? 1 2 3 4 5 6 7 8 9 10 11 12 How to print these values? How to Store and Print a matrix? 2-D Arrays
  • 8.
    2-D Arrays Nesting inLoops Dr. Yousaf, PIEAS
  • 9.
    2-D Arrays • Arraysin C can have virtually as many dimensions as you want. • Definition is accomplished by adding additional subscripts when it is defined. • For example: – int a [4] [3] ; // 4 Rows, 3 Columns – defines a two dimensional array 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] a[3][0] a[3][1] a[3][2] Dr. Yousaf, PIEAS
  • 10.
    2-D Arrays Dr. Yousaf,PIEAS How to store these values? 1 2 3 4 5 6 7 8 9 10 11 12 How to print these values?
  • 11.
    #include<stdio.h> int main() { int a[4][3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} }; int row, col; for (row = 0; row <=3; row++) { for (col = 0; col <=2; col++) { printf(“%d", a[row][col]); } } getchar(); return 0; } Dr. Yousaf, PIEAS How to Print 2-D Arrays?
  • 12.
    123456789101112 We want outputin Matrix Form Dr. Yousaf, PIEAS Output of the Program
  • 13.
    for (row =0; row <=3; row++) { for (col = 0; col <=2; col++) { printf(“%dt",a[row][col]); } } Output: 1 2 3 4 5 6 7 8 9 10 11 12 Dr. Yousaf, PIEAS Output with Tabs
  • 14.
    for (row =0; row <=3; row++) { for (col = 0; col <=2; col++) { printf(“%dt", a[row][col]); } printf(“n”); } Dr. Yousaf, PIEAS Output in Matrix Form
  • 15.
    for (row =0; row <=3; row++) { for (col = 0; col <=2; col++) { printf(“%dt", a[row][col]); } printf(“n”); } Dr. Yousaf, PIEAS Output in Matrix Form Output: 1 2 3 4 5 6 7 8 9 10 11 12
  • 16.
    #include<stdio.h> int main() { int a[4][3]; int row, col; for (row = 0; row <=3; row++) { printf("Enter 3 elements of row %dn", row + 1); for (col = 0; col <=2; col++) { scanf("%d",&a[row][col]); } } //Rest of the code goes here Dr. Yousaf, PIEAS How to scan 2-D Arrays?
  • 17.
    Initializing Multidimensional Arrays •The following initializes a[4][3]: int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} }; • Also can be done by: int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; – is equivalent to a[0][0] = 1; a[0][1] = 2; a[0][2] = 3; a[1][0] = 4; ... a[3][2] = 12; Dr. Yousaf, PIEAS
  • 18.
    Multiple-Subscripted Arrays • Multiplesubscripted arrays – Tables with rows and columns (m by n array) – Like matrices: specify row, then column Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript Dr. Yousaf, PIEAS
  • 19.
    Multiple-Subscripted Arrays • Initialization –int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; – Initializers grouped by row in braces – If not enough, unspecified elements set to zero int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; • Referencing elements – Specify row, then column printf( "%d", b[ 0 ][ 1 ] ); 1 2 3 4 1 0 3 4 Dr. Yousaf, PIEAS
  • 20.
    Multidimensional Arrays • Arraydeclarations read right-to-left • int a[10][3][2]; • “a is array of ten arrays of three arrays of two (type ints)”. In memory 2 2 2 3 2 2 2 3 2 2 2 3 ... 10 Dr. Yousaf, PIEAS
  • 21.
  • 22.
    Addition of TwoMatrices #include <stdio.h> int main() { int X[2][2] = { {1,2},{3,4} }, Y[2][2] = { {5,6},{7,8} }; int add[2][2]; Dr. Yousaf, PIEAS
  • 23.
    Addition of TwoMatrices #include <stdio.h> int main() { int X[2][2] = { {1,2},{3,4} }, Y[2][2] = { {5,6},{7,8} }; int add[2][2]; int i, j; printf("ntAddition of two matrices is"); for (i = 0; i<2; i++) { for (j = 0; j<2; j++) { add[i][j] = X[i][j] + Y[i][j]; printf("%dt", add[i][j]); } printf("n"); } getchar(); return 0; } Dr. Yousaf, PIEAS
  • 24.
    Multiplication of TwoMatrices #include <stdio.h> int main() { int X[2][2] = { {1,2},{3,4} }, Y[2][2] = { {5,6},{7,8} }; int mul[2][2]; int i, j, k, sum = 0; Dr. Yousaf, PIEAS
  • 25.
    Multiplication of TwoMatrices #include <stdio.h> int main() { int X[2][2] = { {1,2},{3,4} }, Y[2][2] = { {5,6},{7,8} }; int mul[2][2]; int i, j, k, sum = 0; printf("nntMultiplications of two matrices is"); for (i = 0; i<2; i++) { for (j = 0; j<2; j++) { for (k=0; k<2; k++) { sum = sum + (X[i][k]*Y[k][j]); } mul[i][j] = sum; printf("%dt", mul[i][j]); sum = 0; } printf("n"); } getchar(); return 0; } Dr. Yousaf, PIEAS
  • 26.
    Try to writethe program for the followings Dr. Yousaf, PIEAS (1)Accept 2x2 matrix. Determine its adjoint. List goes on …