Presentation is loading. Please wait.

Presentation is loading. Please wait.

GE 211 Programming in C Matrix Dr. Ahmed Telba. Example Write function to take coefficients of quadratic equation a, b and c as input parameter and return.

Similar presentations


Presentation on theme: "GE 211 Programming in C Matrix Dr. Ahmed Telba. Example Write function to take coefficients of quadratic equation a, b and c as input parameter and return."— Presentation transcript:

1 GE 211 Programming in C Matrix Dr. Ahmed Telba

2 Example Write function to take coefficients of quadratic equation a, b and c as input parameter and return two roots of quadratic equation as output parameters (using pointers). If the discriminant is negative, it should print “no real roots” and terminates the program execution. Write main program to call this function.

3 Answer: #include void quadratic(double a,double b, double c, dooble *root1, double *root2) // user-defined function { double d; d=b*b-4*a*c; if(d < 0) { printf("No real roots\n"); exit(0); } else { *root1=(-b+sqrt(d))/(2*a); *root2=(-b-sqrt(d))/(2*a); } void main() { double a,b,c,r1,r2; printf("Please input a, b, c :"); scanf("%lf %lf %lf",&a,&b,&c); quadratic(a,b,c,&r1,&r2); // function call printf("\nThe first root is : %f\n",r1); printf("The second root is : %f\n", r2); }

4 Matrix #include int main() { int Grade[5]; Grade[0]=... Grade[1]=... Grade[2]=... Grade[3]=... Grade[4]=... }

5 Matrix #include int main() { int Grade[5]={20,30,52,40,77}; Grade[0]=20 Grade[1]=30 Grade[2]=... Grade[3]=... Grade[4]=... } Int Grade[5]; Int index; For (index=0;index<5;++index){ printf( “Enter the Grade of Student No. %d: " ); scanf( "%d", &tGrade{index]); } }

6

7 #include int main() { /* int Grade[5]={20,30,52,40,77}; Grade[0]=20 Grade[1]=30 Grade[2]=... Grade[3]=... Grade[4]=...*/ } Int Grade[5]; Int index; For (index=0;index<5;++index){ printf( “Enter the Grade of Student No. %d: " ); scanf( "%d", &tGrade{index]); } }

8 #include int main() { int Grade[5]; int index; float Total=0.0; for (index=0; index<5; ++index) { printf( “Enter the Grade No. %d: ",index); scanf( "%d", &Grade[index] ); Total=Total + Grade[index]; } printf( “Student Grade \n"); printf( “= = = = = = \n"); for (index=0; index<5; ++ index) { printf( "%d \n", Grade[index] ); } printf( " Avarege of Student Grades is %5.2f \n", Total/5.0 ); }

9

10 #include void main() { int Grade[5]; int index; int Fail =0,Success =0; for (index=0; index<5; ++index) { printf( “Enter the Grade No. %d: ",index); scanf( "%d", &Grade[index] ); } printf( “Student Grade \n"); printf( “= = = = = = \n"); for (index=0; index<5; ++ index) { if (Grade[index]>50) { printf( "%d \t", Success \n,Grade[index] ); Success =Success +1; } else{ printf( "%d \t", Fail \n,Grade[index] ); Fail=Fail+1; } printf( " No of Success is %d \n", Success ); printf( " No of Failis %d \n", Fail ); } }

11

12 A matrix is just a rectangular array ("grid") of numbers.

13 Matrix defenation To specify the size of a matrix, we need to talk about rows and columns:

14 Matrix, Dimension, Entries An mn matrix A is a rectangular array of real numbers with m rows and n columns. We refer to m and n as the dimensions of the matrix A. The numbers that appear in the matrix are called its entries. We customarily use upper case letters A, B, C,... for the names of matrices. Example (2 * 3) matrix

15 Matrix Addition and Subtraction Two matrices can be added (or subtracted) if, and only if, they have the same dimensions. (That is, both matrices have matching numbers of rows and columns. For instance, you can't add, say, a 34 matrix to a 44 matrix, but you can add two 34 matrices.) To add (or subtract) two matrices of the same dimensions, just add (or subtract) the corresponding entries. In other words, if A and B are mn matrices, then A+B and A-B are the mn matrices whose entries are given by (A + B) ij = A ij + B ij ij th entry of the sum = sum of the ij th entries (A - B) ij = A ij - B ij ij th entry of the difference = difference of the ij th entries

16 Scalar Multiplication What about matrix multiplication? Do we multiply matrices as follows?

17 The product AB has as many rows as A and as many columns as B

18 Matrix Inversion

19 where M ij is the determinant of the matrix formed by deleting the i th row and j th column of A. (Note that the formula refers to M ji and not M ij.) The above rule is not a practical method for the evaluation of inverses. There are much faster methods based on row reduction techniques. In general, the Inverse of an invertible n x n matrix A = (A ij ) is the matrix with elements

20

21 Matrix multiplier code #include void mult_matrices(int a[][3], int b[][3], int result[][3]); void print_matrix(int a[][3]); void main(void) { int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} }; int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} }; int r[3][3]; mult_matrices(p, q, r); print_matrix(r); }

22 void mult_matrices(int a[][3], int b[][3], int result[][3]) { int i, j, k; for(i=0; i<3; i++) { for(j=0; j<3; j++) { for(k=0; k<3; k++) { result[i][j] = a[i][k] + b[k][j]; } void print_matrix(int a[][3]) { int i, j; for (i=0; i<3; i++) { for (j=0; j<3; j++) { printf("%d\t", a[i][j]); } printf("\n"); }}

23 #include void add_matrices(int a[][3], int b[][3], int result[][3]); void print_matrix(int a[][3]); void main(void) { int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} }; int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} }; int r[3][3]; add_matrices(p, q, r); printf("\nMatrix 1:\n"); print_matrix(p); printf("\nMatrix 2:\n"); print_matrix(q); printf("\nResult:\n"); print_matrix(r); } void add_matrices(int a[][3], int b[][3], int result[][3]) { int i, j; for(i=0; i<3; i++) { for(j=0; j<3; j++) { result[i][j] = a[i][j] + b[i][j]; } void print_matrix(int a[][3]) { int i, j; for (i=0; i<3; i++) { for (j=0; j<3; j++) { printf("%d\t", a[i][j]); } printf("\n"); }

24 Matrix addition

25

26

27 //Matrix addition #include void add_matrices(int a[][3], int b[][3], int result[][3]); void print_matrix(int a[][3]); void main(void) { int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} }; int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} }; int r[3][3]; add_matrices(p, q, r); printf("\nMatrix 1:\n"); print_matrix(p); printf("\nMatrix 2:\n"); print_matrix(q); printf("\nResult:\n"); print_matrix(r); }

28 void add_matrices(int a[][3], int b[][3], int result[][3]) { int i, j; for(i=0; i<3; i++) { for(j=0; j<3; j++) { result[i][j] = a[i][j] + b[i][j]; } void print_matrix(int a[][3]) { int i, j; for (i=0; i<3; i++) { for (j=0; j<3; j++) { printf("%d\t", a[i][j]); } printf("\n"); }

29

30 // Matrix Addation #include void mult_matrices(int a[][3], int b[][3], int result[][3]); void print_matrix(int a[][3]); void main(void) { int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} }; int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} }; int r[3][3]; mult_matrices(p, q, r); print_matrix(r); } void mult_matrices(int a[][3], int b[][3], int result[][3]) {

31 int i, j, k; for(i=0; i<3; i++) { for(j=0; j<3; j++) { for(k=0; k<3; k++) { result[i][j] = a[i][k] + b[k][j]; } void print_matrix(int a[][3]) { int i, j; for (i=0; i<3; i++) { for (j=0; j<3; j++) { printf("%d\t", a[i][j]); } printf("\n"); }


Download ppt "GE 211 Programming in C Matrix Dr. Ahmed Telba. Example Write function to take coefficients of quadratic equation a, b and c as input parameter and return."

Similar presentations


Ads by Google