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.

Slides:



Advertisements
Similar presentations
2.3 Modeling Real World Data with Matrices
Advertisements

1 ICS103 Programming in C Lecture 10: Functions II.
1 Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as a i,j and elements of.
Wednesday, July 15, 2015 EQ: What are the similarities and differences between matrices and real numbers ? Warm Up Evaluate each expression for a = -5,
Arithmetic Operations on Matrices. 1. Definition of Matrix 2. Column, Row and Square Matrix 3. Addition and Subtraction of Matrices 4. Multiplying Row.
Objective Video Example by Mrs. G Give It a Try Lesson 4.1  Add and subtract matrices  Multiply a matrix by a scalar number  Solve a matrix equation.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
4.2 Operations with Matrices Scalar multiplication.
Algebra 2: Lesson 5 Using Matrices to Organize Data and Solve Problems.
4.1 Matrix Operations What you should learn: Goal1 Goal2 Add and subtract matrices, multiply a matrix by a scalar, and solve the matrix equations. Use.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Algebra 3: Section 5.5 Objectives of this Section Find the Sum and Difference of Two Matrices Find Scalar Multiples of a Matrix Find the Product of Two.
Lesson 11-1 Matrix Basics and Augmented Matrices Objective: To learn to solve systems of linear equation using matrices.
Slide Copyright © 2009 Pearson Education, Inc. 7.3 Matrices.
Data Structures Lecture 2: Array Azhar Maqsood NUST Institute of Information Technology (NIIT)
Matrices: Simplifying Algebraic Expressions Combining Like Terms & Distributive Property.
Matrix Operations.
Sec 4.1 Matrices.
Algebra Matrix Operations. Definition Matrix-A rectangular arrangement of numbers in rows and columns Dimensions- number of rows then columns Entries-
Chapter 1 Section 1.5 Matrix Operations. Matrices A matrix (despite the glamour of the movie) is a collection of numbers arranged in a rectangle or an.
Matrices Digital Lesson. Copyright © by Houghton Mifflin Company, Inc. All rights reserved. 2 A matrix is a rectangular array of real numbers. Each entry.
GE 211 Programming in C++ Array &Matrix Dr. Ahmed Telba.
3.5 Perform Basic Matrix Operations Add Matrices Subtract Matrices Solve Matric equations for x and y.
Precalculus Section 14.1 Add and subtract matrices Often a set of data is arranged in a table form A matrix is a rectangular.
Where do you sit?. What is a matrix? How do you classify matrices? How do you identify elements of a matrix?
Matrix – is a rectangular arrangement of numbers in rows and columns. Dimensions – Size – m is rows, n is columns. m x n ( row ∙ column) Elements – The.
Designed by Victor Help you improve MATRICES Let Maths take you Further… Know how to write a Matrix, Know what is Order of Matrices,
2.3 MODELING REAL WORLD DATA WITH MATRICES By the end of the section students will be able to add, subtract, and multiply matrices of various sizes. Students.
A rectangular array of numeric or algebraic quantities subject to mathematical operations. The regular formation of elements into columns and rows.
Ch. 12 Vocabulary 1.) matrix 2.) element 3.) scalar 4.) scalar multiplication.
Matrices. Matrix A matrix is an ordered rectangular array of numbers. The entry in the i th row and j th column is denoted by a ij. Ex. 4 Columns 3 Rows.
4.5 Matrices.
Properties and Applications of Matrices
12-1 Organizing Data Using Matrices
Multiplying Matrices.
Matrix Operations Free powerpoints at
Matrix Operations.
ICS103 Programming in C Lecture 10: Functions II
4.6 Matrices.
Matrix Operations.
Matrix Operations Free powerpoints at
What we’re learning today:
Matrix Operations Monday, August 06, 2018.
Matrix Operations.
Matrix Operations SpringSemester 2017.
Computer Graphics Matrix
Matrix Operations Free powerpoints at
Multiplying Matrices.
7.3 Matrices.
Matrices Elements, Adding and Subtracting
Math-2 (honors) Matrix Algebra
MATRICES MATRIX OPERATIONS.
Section 2.4 Matrices.
Objectives Multiply two matrices.
Multiplying Matrices.
3.5 Perform Basic Matrix Operations
Matrices.
ICS103 Programming in C Lecture 10: Functions II
Chapter 4 Matrices & Determinants
Matrix Addition, C = A + B Add corresponding elements of each matrix to form elements of result matrix. Given elements of A as ai,j and elements of B as.
1.8 Matrices.
MATRICES MATRIX OPERATIONS.
What is the dimension of the matrix below?
Matrix Operations SpringSemester 2017.
1.8 Matrices.
Multiplying Matrices.
Multiplying Matrices.
ICS103 Programming in C Lecture 10: Functions II
Introduction to Matrices
Multiplying Matrices.
Presentation transcript:

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 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 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 int main() { int Grade[5]; Grade[0]=... Grade[1]=... Grade[2]=... Grade[3]=... Grade[4]=... }

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]); } }

#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]); } }

#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 ); }

#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 ); } }

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

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

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

Matrix Inversion

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

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

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