ge 211 programming in c matrix dr. ahmed telba. example write function to take coefficients of...

30
GE 211 Programming in C Matrix Dr. Ahmed Telba

Post on 22-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

GE 211 Programming in C

MatrixDr. Ahmed Telba

Page 2: 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

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.

Page 3: 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

Answer:#include<stdio.h>#include<math.h>#include<stdlib.h>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);}

Page 4: 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

Matrix

• #include <stdio.h>• int main()• {• int Grade[5];• Grade[0]=...• Grade[1]=...• Grade[2]=...• Grade[3]=...• Grade[4]=...

• }

Page 5: 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

Matrix #include <stdio.h>int main(){ int Grade[5]={20,30,52,40,77};Grade[0]=20Grade[1]=30Grade[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]); } }

Page 6: 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
Page 7: 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

#include <stdio.h>int main(){ /* int Grade[5]={20,30,52,40,77};Grade[0]=20Grade[1]=30Grade[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]); } }

Page 8: 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

#include <stdio.h>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 );}

Page 9: 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
Page 10: 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

#include <stdio.h>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 ); }}

Page 11: 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
Page 12: 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

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

Page 13: 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

Matrix defenation

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

Page 14: 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

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

Page 15: 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

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 = Aij + Bij ijth entry of the sum = sum of the ijth entries (A - B)ij = Aij - Bij ijth entry of the difference = difference of the ijth entries

Page 16: 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

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

Page 17: 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

Matrix Inversion

Page 18: 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

where Mij is the determinant of the matrix formed by deleting the ith row and jth column of A. (Note that the formula refers to Mji and not Mij.) 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 = (Aij) is the matrix with elements

Page 19: 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
Page 20: 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

Matrix multiplier code #include <stdio.h>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);}

Page 21: 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

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");

}}

Page 22: 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

#include <stdio.h>

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");

}}

Page 23: 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

Matrix addition

Page 24: 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
Page 25: 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
Page 26: 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

//Matrix addition #include <stdio.h> 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);}

Page 27: 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

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");

}}

Page 28: 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
Page 29: 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

// Matrix Addation #include <stdio.h> 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]){

Page 30: 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

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");

}}