Lesson 15 Array1

Embed Size (px)

Citation preview

  • 7/30/2019 Lesson 15 Array1

    1/3

    Lesson 15: Matrixes and 2D Arrays - C Tutorial

    There was a slight pause in my C++ Maniac programming tutorial, due to meanswering some of your C & C++ lesson-related questions. You can find someexplanations on previous programming materials in my sidebar now, along withother new C & C++ stuff. It seems to me finally, after all troubleshoots areanswered (or are they?!), we can bravely continue. This 15th lesson is aboutmatrixes and two-dimensional C++ arrays. Although this one may looksignificantly more complicated, if you read through it carefully, you willconquer programming matrixes with no problem I promise you that.

    Declaration of a 2D array in C language:

    int x[3][2] - matrix 3X2 (3 rows, 2 columns), elements: integerschar myascii[2][4] - array of characters (2 rows & 4 columns)float sequence[MAXROW][MAXCOL] - MAXROW X MAXCOL matrix

    Example of declaration with initialization:

    int array[3][3] = {1, 2, 3, 4, 5};array[0][0] = 1array[0][1] = 2 array[0][2] = 3array[1][0] = 4 array[1][1] = 5array[1][2] = 0 // even though nowhere statedarray[2][0] = 0array[2][1] = 0array[2][2] = 0char cmaniac[7] = {'C', 'M', 'A', 'N','I', 'A', 'C'} /* array of characters */int y[3][4] = { {1, 2, 3},

    {4, 5, 6}, {7, 8, 9} };y[0][0]= 1 y[0][1]= 2 y[0][2]= 3 y[0][3]= 0y[1][0]= 4 y[1][1]= 5 y[1][2]= 6 y[1][3]= 0y[2][0]= 7 y[2][1]= 8 y[2][2]= 9 y[2][3]= 0

    Declaration of multidimensional array:

    int x[3][2][4] 3D array of integer numbersfloat x[3][2][4][1] 4D array of real numbers

    Example:

    Write your own C program that reads through real matrix, 10x10 dimensioned andfinds the smallest element in main diagonal and smallest element in secondarydiagonal.

    #include #define NR_ROW 10#define NR_COL 10int main(void) {int i, j;float mat[NR_ROW][NR_COL], min_maindg, min_secdg;printf("Input matrixelements :");for (i = 0; i < NR_ROW; i++) { for (j = 0; j < NR_COL; j++) {printf("\nInput element [%d][%d] : ", i, j); scanf("%f", &mat[i][j]); }}

    min_maindg = mat[0][0];//min el. is mat(0,0), this is why loop starts from 1for (i = 1; i < NR_ROW; i++) { if (mat[i][i] < min_maindg) {

    min_maindg = mat[i][i]; } }min_secdg = mat[0][NR_COL -1];for (i= 1; i < NR_ROW; i++) { if (mat[i][NR_COL-i-1] < min_secdg) { min_secdg = mat[i][NR_COL-i-1]; }} printf("\nSmallest el. in main diagonal is: %f", min_maindg);printf("\nSmallest el. in second diagonal is: %f", min_secdg);}

  • 7/30/2019 Lesson 15 Array1

    2/3

    Shorter version single run through the matrix:

    #include #define NR_ROW 10#define NR_COL 10int main(void) { int i, j; float mat[NR_ROW][NR_COL], min_maindg, min_secdg; printf("\nInput matrix elements :\n"); for (i = 0; i < NR_ROW; i++) { for (j = 0; j< NR_COL; j++) { printf("\nInput element [%d][%d] : ", i, j); scanf("%f", &mat[i][j]); if (i == 0 && j == 0) { min_maindg =mat[i][j]; } if (i == 0 && j == NR_COL - 1) { min_secdg = mat[i][j]; } if (i == j) { if (mat[i][j]< min_maindg) { min_maindg = mat[i][j]; }

    } if (i == NR_COL - 1 - j) { if (mat[i][j] < min_secdg) { min_secdg = mat[i][j]; } }} }printf("\nSmallest element in main diagonal is : %f", min_maindg);

    printf("\nSmallest element in second diagonal is : %f", min_secdg);}

    Example:

    Write your own C program that transposes matrix. Program stores given matrixdimensions and every single matrix element must be given. Transposed matrix isthe one with rows and columns switched.

    #include #define MAX_ROW 50#define MAX_COL 50int main(void) {int i, j, m, n, temp;int mat[MAX_ROW][MAX_COL]; // variable dim is set to smaller value of defined // maximal number of rows and columns int dim = (MAX_ROW < MAX_COL)? MAX_ROW : MAX_COL; // storing matrix size do { printf("Input number of rows < %d :", dim); scanf("%d", &m); printf("Input number of columns < %d:", dim); scanf("%d", &n); } while (m< 1 || m > dim || n < 1 || n > dim); // storing matrix elements printf("\nInput of matrix elements :\n"); for (i = 0; i < m; i++) { for (j = 0; j

    < n; j++) { printf("Input element [%d][%d] : ", i, j); scanf("%d", &mat[i][j]); } } // printing matrix before transposing printf("\n\nMatrix before transposing:\n"); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { printf("%3d", mat[i][j]); } printf("\n"); } // transposing for ( i=0; i

  • 7/30/2019 Lesson 15 Array1

    3/3

    Example:

    Write your own C program that stores real matrix whose dimensions are 10x10, andfinds the sum of elements from every column and product of elements from everyrow. Program prints the smallest sum (including parent columns index), andbiggest product (including parent rows index). Sums and products should bestored in one-dimensional arrays.

    #include #define NR_ROW 10#define NR_COL 10int main(void) { int i, j; int min_sum_ind, max_prod_ind; float mat[NR_ROW][NR_COL]; float sum[NR_COL], prod[NR_ROW];/*1.variant of input and calculatingsums & products*/ for (i = 0; i < NR_ROW; i++) { for (j = 0; j < NR_COL; j++) { printf("\nInput element[%d][%d] : ", i, j); scanf("%f", &mat[i][j]); } } for (j = 0; j < NR_COL; j++) { sum[j] = 0; for (i = 0; i < NR_ROW; i++) { sum[j] += mat[i][j]; } } for (i = 0; i < NR_ROW; i++) { prod[i] = 1;for (j = 0; j < NR_COL; j++) { prod[i] *= mat[i][j]; } }

    /*end of input, and sums & products calculation*//* finding columns index for smallest sum */ min_sum_ind = 0; for (j = 1; j < NR_COL; j++) { if (sum[j] < sum[min_sum_ind]) { min_sum_ind = j; } } /* findingrows index for biggest product */ max_prod_ind = 0; for (i = 1; i < NR_ROW;i++) { if (prod[i] > prod[max_prod_ind]) { max_prod_ind = i;

    } } printf("\nSmallest sum: %f, parent index: %d\n", sum[min_sum_ind], min_sum_ind); printf("\nBiggest product: %f, parent index: %d\n", prod[max_prod_ind], max_prod_ind);}

    Shorter variant for storing elements and calculating sums & products:

    /*2.variant of input and calculating sums & products*/for (i = 0; i < NR_ROW; i++) { prod[i] = 1; for (j = 0; j < NR_COL; j++) { printf("\nInputelement [%d][%d] : ", i, j); scanf("%f", &mat[i][j]); prod[i] *= mat[i][j]; if (i == 0) { sum[j] = 0; } sum[j] += mat[i][j]; } }

    /* end of input, and sums & products calculation */

    Technorati Tags: Array, 2D, Matrix, Matrixes, Two Dimensional, Diagonal, Element