A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.

Slides:



Advertisements
Similar presentations
Chapter 4 Systems of Linear Equations; Matrices
Advertisements

Chapter 4 Systems of Linear Equations; Matrices Section 2 Systems of Linear Equations and Augmented Matrics.
1 Copyright © 2015, 2011, 2007 Pearson Education, Inc. Chapter 4-1 Systems of Equations and Inequalities Chapter 4.
CISE301_Topic3KFUPM1 SE301: Numerical Methods Topic 3: Solution of Systems of Linear Equations Lectures 12-17: KFUPM Read Chapter 9 of the textbook.
Chapter 2 Matrices Finite Mathematics & Its Applications, 11/e by Goldstein/Schneider/Siegel Copyright © 2014 Pearson Education, Inc.
Goldstein/Schnieder/Lay: Finite Math & Its Applications, 9e 1 of 86 Chapter 2 Matrices.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
Mujahed AlDhaifallah (Term 342) Read Chapter 9 of the textbook
10.1 Gaussian Elimination Method
Finite Mathematics & Its Applications, 10/e by Goldstein/Schneider/SiegelCopyright © 2010 Pearson Education, Inc. 1 of 86 Chapter 2 Matrices.
Chapter 8 Multidimensional Arrays C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Matrices Write and Augmented Matrix of a system of Linear Equations Write the system from the augmented matrix Solve Systems of Linear Equations using.
1 1.1 © 2012 Pearson Education, Inc. Linear Equations in Linear Algebra SYSTEMS OF LINEAR EQUATIONS.
Barnett/Ziegler/Byleen Finite Mathematics 11e1 Review for Chapter 4 Important Terms, Symbols, Concepts 4.1. Systems of Linear Equations in Two Variables.
Matrices King Saud University. If m and n are positive integers, then an m  n matrix is a rectangular array in which each entry a ij of the matrix is.
Copyright © Cengage Learning. All rights reserved. 7.4 Matrices and Systems of Equations.
Section 1.1 Introduction to Systems of Linear Equations.
Sec 3.1 Introduction to Linear System Sec 3.2 Matrices and Gaussian Elemination The graph is a line in xy-plane The graph is a line in xyz-plane.
Linear Systems Gaussian Elimination CSE 541 Roger Crawfis.
Three variables Systems of Equations and Inequalities.
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
Multi-Dimensional Arrays
A First Book of ANSI C Fourth Edition
Matrices Jordi Cortadella Department of Computer Science.
Chapter 8 Arrays and Strings
Yasser F. O. Mohammad Assiut University Egypt. Previously in NM Introduction to NM Solving single equation System of Linear Equations Vectors and Matrices.
Gaussian elimination Jordi Cortadella Department of Computer Science.
Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 8 Multidimensional Arrays.
Two-Dimensional Arrays ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Copyright © 2011 Pearson Education, Inc. Solving Linear Systems Using Matrices Section 6.1 Matrices and Determinants.
Matrices and Systems of Equations
Matrices and Systems of Linear Equations
 SOLVE SYSTEMS OF EQUATIONS USING MATRICES. Copyright © 2012 Pearson Education, Inc. Publishing as Addison Wesley 9.3 Matrices and Systems of Equations.
Matrices and Systems of Equations
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 1 Systems of Linear Equations Linear Algebra.
Matrices Digital Lesson. Copyright © by Houghton Mifflin Company, Inc. All rights reserved. 2 A matrix is a rectangular array of real numbers. Each entry.
H.Melikian/12101 System of Linear Equations and Augmented Matrices Dr.Hayk Melikyan Departmen of Mathematics and CS
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Student Handbook 1. Matrices A matrix (plural: matrices, not matrixes) is a rectangular array of numbers such as Matrices are useful when modelling a.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Arrays and Matrices. One-Dimensional Arrays An array is an indexed data structure All variables stored in an array are of the same data type An element.
Slide Copyright © 2009 Pearson Education, Inc. 7.4 Solving Systems of Equations by Using Matrices.
Chapter 5: Matrices and Determinants Section 5.5: Augmented Matrix Solutions.
Gaussian Elimination Digital Lesson. Copyright © by Houghton Mifflin Company, Inc. All rights reserved. 2 Gaussian elimination with back-substitution.
College Algebra Chapter 6 Matrices and Determinants and Applications
Linear Equations in Linear Algebra
Chapter 4 Systems of Linear Equations; Matrices
Gaussian Elimination and Gauss-Jordan Elimination
Solving Systems of Equations Using Matrices
Matrices and Systems of Equations
Chapter 4 Systems of Linear Equations; Matrices
Lets Play with arrays Singh Tripty
Linear Equations in Linear Algebra
Chapter 4 Systems of Linear Equations; Matrices
Matrices are identified by their size.
Presentation transcript:

A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional array and box[2][1] is an element in row 2, column 1 and char box[][3] can be used in the function prototype note that only the first dimension can be omitted

A.Abhari CPS1252 Multidimensional Arrays For example : double table [NROWS][NCOLS]; Can be used as parameter in the function prototype as: void process_martrix( int in[ ][4], int out[ ][4], int nrows)

A.Abhari CPS1253 Two Dimensional Array Char box [3] [3] Row Column box [1] [2]

A.Abhari CPS1254 /* * Checks whether a box is completely filled */ int filled(char box[3][3]) /* input - box to check*/ { int r,c, /* row and column subscripts*/ ans; /* whether or not box is filled.*/ /* Assumes box is filled until blank is found*/ ans = 1; /* Resets ans to zero if a blank is found*/ for (r = 0; r < 3; ++r) for (c = 0; c < 3; ++c) if (box[r][c] == ' ') ans = 0; return (ans); }

A.Abhari CPS1255 Arrays with Several Dimensions int soil_type[4] [7] [MAXDEPTH]

A.Abhari CPS1256 Case Study: Cellular Telephone System Problem: Finding the best way to build a cellular network. There is some marketing data that predicts the demand will be at tree time of interest. There are only 10 transmitters and there is a need for a program to help analyzing call demand data.

A.Abhari CPS1257 Case Study: Cellular Telephone System Analysis: There should be three matrices shows traffic density for each time of the day: Input: int commuters[GRID_SIZE][GRID_SIZE] int salesforce[GRID_SIZE][GRID_SIZE] int weekend[GRID_SIZE][GRID_SIZE] Output: int summed_data[GRID_SIZE][GRID_SIZE] int location_i, location_j

A.Abhari CPS1258 Case Study: Cellular Telephone System Design: initial algorithm: 1.Get traffic data for three time period 2.Get the weights from user 3.Multiply weight by each matrix entry and store the sum in the summed data 4.Find highest valued cells in the summed data and display them as the pair of location_i and location_j Implementation

Filling the multidimensional array /* Fills 3 GRID_SIZE x GRID_SIZE arrays with traffic data from TRAFFIC_FILE*/ void get_traffic_data(int commuters[GRID_SIZE][GRID_SIZE], /* output */ int salesforce[GRID_SIZE][GRID_SIZE], /* output */ int weekend[GRID_SIZE][GRID_SIZE]) /* output */ { int i, j; /* loop counters */ FILE *fp; /* file pointer */ fp = fopen(TRAFFIC_FILE, "r"); for (i = 0; i < GRID_SIZE; ++i) for (j = 0; j < GRID_SIZE; ++j) fscanf(fp, "%d", &commuters[i][j]); for (i = 0; i < GRID_SIZE; ++i) for (j = 0; j < GRID_SIZE; ++j) fscanf(fp, "%d", &salesforce[i][j]); for (i = 0; i < GRID_SIZE; ++i) for (j = 0; j < GRID_SIZE; ++j) fscanf(fp, "%d", &weekend[i][j]); fclose(fp); }

A.Abhari CPS12510 /* Computes and displays the weighted, summed_data */ for (i = 0; i < GRID_SIZE; ++i) for (j = 0; j < GRID_SIZE; ++j) summed_data[i][j] = commuter_weight* commuters[i][j] + salesforce_weight * salesforce[i][j] + weekend_weight * weekend[i][j]; Modifying the multidimensional array

/* Finds the NUM_TRANSMITTERS highest values in the summed_data matrix.Temporarily stores the coordinates in location_i and location_j, and then displays the resulting locations */ printf("\n\nLocations of the %d transmitters:\n\n", NUM_TRANSMITTERS); for (tr = 1; tr <= NUM_TRANSMITTERS; ++tr) { current_max = SELECTED; /* Starts off our search with a value that is known to be too low. */ for (i = 0; i < GRID_SIZE; ++i) { for (j = 0; j < GRID_SIZE; ++j) { if (current_max < summed_data[i][j]) { current_max = summed_data[i][j]; location_i = i; location_j = j; } Searching in the multidimensional array

A.Abhari CPS12512 Printing the contents of multidimensional array /* * Displays contents of a GRID_SIZE x GRID_SIZE matrix of integers */ void print_matrix(int matrix[GRID_SIZE][GRID_SIZE]) { int i, j; /* loop counters */ for (i = 0; i < GRID_SIZE; ++i) { for (j = 0; j < GRID_SIZE; ++j) printf("%3d ", matrix[i][j]); printf("\n"); }

A.Abhari CPS12513 Vectors Vector: a mathematical object consisting of a sequence of numbers. /* a vector */ int vect[3] = {4, 12, 19}; Differences between vector and array: 1- an n_dimensional vector is represented in C as a one dimensional array of size n. 2- vect 3 is vect[2] in C

A.Abhari CPS12514 Vectors Calculating scalar product:. = 1*2 + 2*3 +4*1=12 In C: sum_prod = 0; for (k=0; k<n; k++) sum_prod += x[k] * w[k];

A.Abhari CPS12515 Matrices Matrix: a mathematical object consisting of a rectangular arrangement of numbers called the element of matrix.. /* a matrix int x[2][2] = {{3, 6}, {4, 5}}; x

A.Abhari CPS12516 Matrices Multiplying a matrix by a vector A * X = V multiplication * 2 = -3 on the right In C for each member of V: v[i] = 0; for (k=0; k<n; k++) v[k] += a[i][k] * x[k];

A.Abhari CPS12517 /* Computes the product of M-by-N matrix a and the N- dimensional vector x. The result is stored in the output parameter v, an M-dimensional vector.*/ void mat_vec_prod(double v[], /* M-dimensional vector */ double a[M][N], /* M-by-N matrix */ double x[]) /* N-dimensional vector */ { int i, k; for (i = 0; i < M; ++i) { v[i] = 0; for (k = 0; k < N; ++k) { v[i] += a[i][k] * x[k]; }

A.Abhari CPS12518 Matrix Multiplication * = for ( i=0; i< m, ++i) { for (j=0; j<p; ++j) { …….. compute c[i][j]…. }

A.Abhari CPS12519 /* Multiplies matrices A and B yielding product matrix C */ void mat_prod(double c[M][P], /* output - M by P matrix */ double a[M][N], /* input - M by N matrix */ double b[N][P]) /* input - N by P matrix */ { int i, j, k; for (i = 0; i < M; ++i) { for (j = 0; j < P; ++j) { c[i][j] = 0; for (k = 0; k < N; ++k) c[i][j] += a[i][k] * b[k][j]; }

A.Abhari CPS12520 Solving System of Linear Equations To solve many problems such as force equation in three-dimensional system we need to solve a three linear equations: A X = Y x * x 2 = x 3 -2 It is multiplication of a matrix by a vector on the right

A.Abhari CPS12521 Gaussian Elimination Gaussian elimination can be used to solve a linear equation. The algorithm for Gussian elimination is: 1.Transform the original system into scaled triangular form. 2.Solve for x i by back substitution

A.Abhari CPS12522 Gaussian Elimination triangular form x * x 2 = x 3 1 back substitution x 1 + x 2 + x 3 = 4 x 2 - x 3 = 1 x 3 = 1

A.Abhari CPS12523 Gaussian Elimination For doing that we need to triangularizing the augmented matrix by following operations: 1.Multiply any row of aug by nonzero number 2.Add to any row of aug a multiple of other rows 3.Swap any two rows If system has a unique solution, we can get the system into desired form by this three operations.

A.Abhari CPS12524 /* * Performs pivoting with respect to the pth row and the pth column * If no nonzero pivot can be found, FALSE is sent back through piv_foundp */ void pivot(double aug[N][N+1], /* input/output - augmented matrix */ int p, /* input - current row*/ int *piv_foundp) /* output - whether or not nonzero pivot found*/ { double xmax, xtemp; int j, k, max_row; /* Finds maximum pivot*/ xmax = fabs(aug[p][p]); max_row = p; for (j = p+1; j < N; ++j) { if (fabs(aug[j][p]) > xmax) { xmax = fabs(aug[j][p]); max_row = j; }

A.Abhari CPS12525 /* Swaps rows if nonzero pivot was found*/ if (xmax == 0) { *piv_foundp = FALSE; } else { *piv_foundp = TRUE; if (max_row != p) { /* swap rows */ for (k = p; k < N+1; ++k) { xtemp = aug[p][k]; aug[p][k] = aug[max_row][k]; aug[max_row][k] = xtemp; }

/* * Performs back substitution to compute a solution vector to a system of * linear equations represented by the augmented matrix aug. Assumes that * the coefficient portion of the augmented matrix has been triangularized, * and its diagonal values are all 1. */ void back_sub(double aug[N][N+1], /* input - scaled, triangularized augmented matrix*/ double x[N]) /* output - solution vector */ { double sum; int i, j; x[N - 1] = aug[N - 1][N]; for (i = N - 2; i >= 0; --i) { sum = 0; for (j = i + 1; j < N; ++j) sum += aug[i][j] * x[j]; x[i] = aug[i][N] - sum; }

A.Abhari CPS12527 Common Programming Errors Use constants for each dimension’s size when declaring multidimensional array When declaring the array as a parameter of a function if you omit the first dimension all other dimensions must be supplied Since access to the elements of a multidimensional array requires nested counting loops it is easy to make out-of-range error. Since using multidimensional arrays as local variables requires large memory space, you may need to tell to operating system to increase stack size when the program is running