ge 211 programming in c matrix dr. ahmed telba. example write function to take coefficients of...
Post on 22-Dec-2015
220 views
TRANSCRIPT
GE 211 Programming in C
MatrixDr. Ahmed Telba
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.
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);}
Matrix
• #include <stdio.h>• int main()• {• int Grade[5];• Grade[0]=...• Grade[1]=...• Grade[2]=...• Grade[3]=...• Grade[4]=...
• }
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]); } }
#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]); } }
#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 );}
#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 ); }}
A matrix is just a rectangular array ("grid") of numbers.
Matrix defenation
• To specify the size of a matrix, we need to talk about rows and columns:
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
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
• The product AB has as many rows as A and as many columns as B
Matrix Inversion
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
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);}
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");
}}
#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");
}}
Matrix addition
//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);}
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");
}}
// 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]){
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");
}}