13.07.2015 Views

A practical introduction to Pascal programming language - GIARA

A practical introduction to Pascal programming language - GIARA

A practical introduction to Pascal programming language - GIARA

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

A Practical Introduction <strong>to</strong> <strong>Pascal</strong> Programming Language⊡(* Ask the user *)write(’Introduce the number a: ’);readln(a);(* Count *)sum:=0;for i:=1 <strong>to</strong> N dosum:=sum+matA[i][a]+matA[a][i];sum:=sum-matA[a][a];(* Display *)writeln(’The final value is ’,sum,’.’)end.Exercise 4.13. Create a program that reads a matrix of N × N integers (being N a constant), thenfinds the greater sum of the elements of a row or column.□In this exercise we need <strong>to</strong> sum the elements of each row and column. Then, we have <strong>to</strong> findout which one produces the greater sum. Conceptually, it is very similar <strong>to</strong> the Exercise 4.4 inwhich we had <strong>to</strong> find the greatest element of a table. In fact, we also have <strong>to</strong> address the problemof setting maxSum <strong>to</strong> some initial value.Program 91: Exercise 4.13program c04e13;constN=3;M=5;typeintMatrix=ARRAY[1..N,1..M] OF integer;vari,j:integer;matA:intMatrix;maxSum,currentSum:integer;begin(* Reading the matrix *)writeln(’Introduce the matrix.’);for i:=1 <strong>to</strong> N dobeginwriteln(’Introduce the row ’,i,’: ’);for j:=1 <strong>to</strong> M doread(matA[i][j])end;(* Initialize the variable *)maxSum:=-32000;(* Checking rows *)for i:=1 <strong>to</strong> N dobegincurrentSum:=0;for j:=1 <strong>to</strong> M docurrentSum:=currentSum+matA[i][j];if (currentSum>maxSum) thenmaxSum:=currentSumend;(* Checking colums *)for j:=1 <strong>to</strong> M dobegincurrentSum:=0;for i:=1 <strong>to</strong> N docurrentSum:=currentSum+matA[i][j];78

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!