Lecture_10_Dec_01_2015

22
Computing Fundamentals Dr. Muhammad Yousaf Hamza

Transcript of Lecture_10_Dec_01_2015

Page 1: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 1/22

Computing Fundamentals

Dr. Muhammad Yousaf Hamza

Page 2: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 2/22

2-D ArraysNesting in Loops

Dr. Yousaf, PIEAS

Page 3: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 3/22

Multidimensional Arrays

•Arrays in C can have virtually as many dimensions asyou want.

• Definition is accomplished by adding additional

subscripts when it is defined.

• For example:

 –  int a [4] [3] ;

 –  defines a two dimensional array

a[0][0] a[0][1] a[0][2]…

Dr. Yousaf, PIEAS

Page 4: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 4/22

2-D Arrays

Dr. Yousaf, PIEAS

How to store these values?

1 2 3

4 5 67 8 9

10 11 12

How to print these values?

Page 5: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 5/22

#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?

Page 6: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 6/22

123456789101112

Dr. Yousaf, PIEAS

Output of the Program

Page 7: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 7/22

for (row = 0; row <=3; row++){ for (col = 0; col <=2; col++)

{printf(“%d\t",a[row][col]);

}}

Output:1 2 3 4 5 6 7 8 910 11 12

Dr. Yousaf, PIEAS

Output with Tabs

Page 8: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 8/22

for (row = 0; row <=3; row++){ for (col = 0; col <=2; col++)

{printf(“%d\t", a[row][col]);

}

printf(“\n”);}

Output:1 2 34 5 67 8 910 11 12

Dr. Yousaf, PIEAS

Output in Matrix Form

Page 9: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 9/22

#include<stdio.h>

int main()

{

int a[4] [3];

int row, col;

for (row = 0; row <=3; row++)

{printf("Enter 3 elements of row %d\n", 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?

Page 10: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 10/22

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 toa[0][0] = 1;

a[0][1] = 2;

a[0][2] = 3;

a[1][0] = 4;

...

a[3][2] = 12;

Dr. Yousaf, PIEAS

Page 11: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 11/22

Multiple-Subscripted Arrays

• Multiple subscripted arrays

 –  Tables with rows and columns ( m by n array)

 –  Like matrices: specify row, then column

Row 0Row 1Row 2

Col umn 0 Col umn 1 Col umn 2 Col umn 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 subscr i pt

Ar r ay nameCol umn subscr i pt

Dr. Yousaf, PIEAS

Page 12: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 12/22

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 zeroint 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

Page 13: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 13/22

Multidimensional Arrays

• Array declarations read right-to-left

• int a[10][3][2];

• “an 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

Page 14: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 14/22

#include<stdio.h>

int main()

{

int marks[6] =

{36,78,7,99,43,29};

int i, sum;

Maximum = marks[0];

for (i = 1; i <6;i++){

if (Maximum <marks[i])

{

Maximum = marks[i];

}

}

printf("Maximum Marks are%d\n",Maximum);

getchar();return 0;

}

Dr. Yousaf, PIEAS

To Find the Maximum Marks

Page 15: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 15/22

Addition of Two Matrices

#include<stdio.h>

int main ()

{

int a[5] = {3, 6, -7, 10, 4};int b[5] = {10, 5, 8, 0, -7},

result[5], i;

for(i=0;i<5;i++)result[i] = a[i] + b[i];

printf("The resultant array by

the addition of these two

arrays is:\n\n");

for(i=0;i<5;i++)printf("\t%d",result[i]);

getch();

return 0;

}

Dr. Yousaf, PIEAS

Let us first two addition of the elements of two single

dimension arrays

Page 16: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 16/22

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("\n\tAddition of two matrices

is");

for (i = 0; i<2; i++)

{printf("\n\n\t");

for (j = 0; j<2; j++)

{

add[i][j] = X[i][j] + Y[i][j];printf("%d\t", add[i][j]);

}

}

getchar();

return 0;

}

Dr. Yousaf, PIEAS

Page 17: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 17/22

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 add[2][2], mul[2][2];

int i, j, k, sum = 0;

printf("\n\n\tMultiplications of two

matrices is");

for (i = 0; i<2; i++)

{

printf("\n\n\t");

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("%d\t", mul[i][j]);

sum = 0;

}}

getchar(); return 0; }

Dr. Yousaf, PIEAS

Page 18: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 18/22

Some Examples

Dr. Yousaf, PIEAS

Page 19: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 19/22

//Linear Search

#include <stdio.h>

int main()

{

int array[100], search, c, n;

printf("Enter the number of elements in

array\n");

scanf("%d",&n);

printf("Enter %d integer(s)\n", n);

for (c = 0; c < n; c++)

scanf("%d", &array[c]);

printf("Enter the number to search\n");

scanf("%d", &search);

for (c = 0; c < n; c++)

{

if (array[c] == search)

{printf("%d is present at location

%d.\n", search, c+1);

break;

}

}if (c == n)

printf("%d is not present in

array.\n", search);

getchar();

return 0;

}

Dr. Yousaf, PIEAS

Page 20: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 20/22

#include <stdio.h>

#include <stdlib.h>

#include<math,h>

int main()

{

int c, n;

printf("Ten random numbersin [1,100]\n");

for (c = 1; c <= 10; c++) {

n = rand()%100 + 1;

printf("%d\n", n);

}

getchar();return 0;

}

Dr. Yousaf, PIEAS

To Generate Random Numbers

If we use rand() only as

n = rand();

It can generate random number

of value in thousands. So when

we usen = rand()%100 + 1;

Whatever be the value of

random number, its modulus

with hundred will always be in

the range 0 to 99. By adding 1, n

will always be in the range from

1 to 100.

Page 21: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 21/22

// Sorting an array in ascending//order

#include <stdio.h>

int main()

{

int s, i ,j, temp, a[20];

printf("Enter total elements: ");

scanf("%d",&s);

printf("Enter %d elements: ",s);

for(i=0;i<s;i++)

scanf("%d",&a[i]);

for(i=0;i<s;i++)

{

for(j=i+1; j<s; j++)

{

if(a[i]>a[j])

{

temp=a[i];

a[i]=a[j];a[j]=temp;

}

}

}

printf("After sorting: \n");for(i=0; i<s; i++)

printf(" %d",a[i]);

getchar(); return 0;

}

Dr. Yousaf, PIEAS

Sorting an Array

Page 22: Lecture_10_Dec_01_2015

7/23/2019 Lecture_10_Dec_01_2015

http://slidepdf.com/reader/full/lecture10dec012015 22/22

The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3,

5, 8, 13, 21, 34, ... The next number is found by adding up

the two numbers before it. The 2 is found by adding the

two numbers before it (1+1)

Dr. Yousaf, PIEAS

To Generate Fibonacci Series

#include<conio.h>

#include<stdio.h>

int main () {int fib[50], nterms,j;

printf("This program generates

Fibonacci series upto 20

terms\n");printf("Please enter the number

of terms: ");

scanf("%d", &nterms);

fib[0] = 0;

fib[1] = 1;

for(j=2;j<nterms;j++)

fib[j] = fib[j-2] + fib[j-1];printf("\n Feibinacci series

is:\n\n");

for(j=0;j<nterms;j++)printf("%d\t", fib[j]);

getch(); return 0;

}