You are on page 1of 2

Lesson 19: C Programming Examples - C Tutorial Ive based this lesson on a single program example.

This part of C programming tutorial has dual purpose: firstly to teach you how to apply previously learned C/C++ knowledge, second: to show you how pointers, arrays, functions and matrixes can be combined together in one single program. Example: Maximal number of rows and columns matrix can have is predefined. Write your own main program which reads given number of matrixs rows and columns, and additionally reads matrixs given elements. Main program prints:

Matrixs elements sum (calls upon a function that calculates elements sum) Maximal value in every row of a matrix (calls upon a function that finds the biggest element in a flow)

Matrix mat is declared in a way of two dimensional array (2D array): float mat[MAXROW][MAXCOL]; Variables nrRow and nrCol store matrixs dimensions provided by user. The image below shows you this in a visual way. Click on the image to enlarge it.

Memory of a computer allocates MAXROW * MAXCOL * sizeof(float) byte. Matrixs rows follow one after another, in a way where every next row is placed right after the previous one; and so on. Picture below describes this perfectly. Click on the image to enlarge it.

Generally speaking, for mat[i][j] number of elements from the beginning of an array is: i * MAXCOL + j #include <stdio.h>#define MAXROW 100#define MAXCOL 100float max( int len , float *p ) {// OR: float max( int len, float p[] ) float res; int i;printf ("\nIn function max :"); printf("\nRows beginning address in memory : % ld", p); res = p[0]; for( i=1; i<len; i++ ) if( p[i] > res ) re s = p[i]; return res;}float sumEl(int nrRow,int nrCol, int maxCol,float *mat) {int i, j; float sum; printf("\nIn function sumEl:"); printf("\nMat rixs beginning address in memory: %ld", mat);printf("\nBegin. of 2nd rows addr. in mem.: %ld", &mat[maxCol]); printf("\nBegin. of 3rd rows addr. in mem.: %ld", &mat[2* maxCol]); sum = 0.0; for( i=0; i<nrRow; i++ ) for( j=0; j<nrCol; j++ ) sum += mat[i*maxCol + j]; // or: sum += *(mat + i*maxCol + j) return sum;}int main(void) { int row, col, i, j; fl

oat add, maxRow, mat[MAXROW][MAXCOL]; printf("\nInput nr. of rows and columns: "); scanf("%d %d", &row, &col ); printf("\nIn function main"); printf("\nM atrixs beginning address in memory: %ld", mat); printf("\nBegin. of 2nd rows addr.: %ld\n\n", &mat[1][0]); printf("\nBegin. of 3rd rows addr.: %ld\n\n", &mat[2][0]); for( i=0; i<row; i++ ) for( j=0; j<col; j+ + ) { printf("Input element[%d,%d]:",i,j); scanf("%f", &mat[i][j ] ); } //calculates elements sum in matrix add=sumEl(row,col,MAXCOL,(float *) mat); printf("\n\nSum of matrix elements is %f", add ); //prin ts maximal value of every mat. row for(i=0;i<row;i++) { maxRow = max(col , &mat[i][0]); // or: max(col, mat+i*MAXCOL) printf("\nBiggest eleme nt in row %d is %f\n",i,maxRow); }}

Example of programs execution: Input nr. of rows and columns: 3 2In function main :Matrixs beginning address i n memory : 1205032Begin. of 2nd rows addr. : 1205432Begin. of 3rd rows addr. : 12058323rd: 1205432 = 125032 + 1 * MAXCOL * sizeof(float) = 125032 + 400; 1205832 = 125 032 + 2 * MAXCOL * sizeof(float) = 125032 + 800;Input elemen t [0,0]: 2Input element [0,1]: 3Input element [1,0]: 4Input element [1,1]: 3 Input element [2,0]: 1Input element [2,1]: 5In function sumEl:Matrixs beginning address in memory: 1205032Begin. of 2nd rows addr. in mem.: 1205432Begi n. of 3rd rows addr. in mem.: 1205832Sum of matrix elements is 18.000000 In function max:Rows beginning address in memory: 1205032Biggest element in row 0 is 3.000000In function max:Rows beginning address in memory: 1205432Biggest el ement in row 1 is 4.000000In function max:Rows beginning address in memory: 1205 832Biggest element in row 2 is 5.000000

Example: Write your own C function which returns flow of matrix rows maximal values. void maxFlow(float *mat,int nrRow,int nrCol, int maxCol,float *flow) { //OR: void maxFlow(float mat[], int nrRow, int nrCol,int maxCol,float flow[] ) int i, j; for (i = 0; i < nrRow; i++) { flow[i] = mat[i * maxCol]; for(j = 1; j < nrCol; j++) if (mat[i*maxCol+j] > flow[i]) flow[i]=mat[i*maxCol+j]; }}

You might also like