Pascal Code Examples: Arrays, Matrices, and Logic
Classified in Computers
Written at on English with a size of 5.97 KB.
Pascal Code Examples
1. String Search in Array
This program searches for a given name within an array of strings.
Program Pzim; var vet1: array[1..10] of string; nome: string; i, j: integer; begin j := 0; for i := 1 to 10 do begin write('Enter the names: '); readln(vet1[i]); end; write('Enter the name to be searched: '); readln(nome); for i := 1 to 10 do begin if (nome = vet1[i]) then begin writeln('FOUND'); j := j + 1; end; end; if (j = 0) then writeln('NOT FOUND'); end.
2. Merging Two Arrays
This program merges two arrays (vet1 and vet2) into a third array (vetx).
Program Pzim; var vet1: array[1..5] of integer; vet2: array[1..10] of integer; vetx: array[1..15] of integer; i, j: integer; begin j := 0; for i := 1 to 5 do begin write('Enter the values for array A: '); readln(vet1[i]); j := j + 1; vetx[j] := vet1[i]; end; for i := 1 to 10 do begin write('Enter the values for array B: '); readln(vet2[i]); j := j + 1; vetx[j] := vet2[i]; end; for i := 1 to 15 do begin write(' ', vetx[i]); end; end.
3. Finding Common Elements
This program finds and displays the common elements between two arrays.
Program Pzim; var vet1: array[1..5] of integer; vet2: array[1..10] of integer; vetx: array[1..15] of integer; i, j, z: integer; begin j := 0; for i := 1 to 5 do begin write('Enter the values for array A: '); readln(vet1[i]); end; for i := 1 to 10 do begin write('Enter the values for array B: '); readln(vet2[i]); end; for i := 1 to 5 do begin for z := 1 to 10 do begin if (vet1[i] = vet2[z]) and (vetx[i] <> vet2[z]) then begin j := j + 1; vetx[j] := vet1[i]; end; end; end; for i := 1 to j do begin write(', ', vetx[i]); end; end.
4. Matrix Operations and Sorting
This program reads two matrices, sorts their elements, and stores them in a new matrix.
Program Pzim; var mat1, mat2: array[1..5, 1..5] of integer; matx: array[1..25, 1..2] of integer; vet1, vet2: array[1..25] of integer; i, j, z, aux, h: integer; begin z := 0; h := 25; for i := 1 to 5 do begin for j := 1 to 5 do begin write('Enter the values for matrix A: '); readln(mat1[i, j]); z := z + 1; vet1[z] := mat1[i, j]; end; end; z := 0; for i := 1 to 5 do begin for j := 1 to 5 do begin write('Enter the values for matrix B: '); readln(mat2[i, j]); z := z + 1; vet2[z] := mat2[i, j]; end; end; for i := 2 to 25 do begin aux := vet1[i]; j := i - 1; while (j > 0) and (vet1[j] > aux) do begin vet1[j + 1] := vet1[j]; j := j - 1; end; vet1[j + 1] := aux; end; for i := 2 to 25 do begin aux := vet2[i]; j := i - 1; while (j > 0) and (vet2[j] > aux) do begin vet2[j + 1] := vet2[j]; j := j - 1; end; vet2[j + 1] := aux; end; for i := 1 to 25 do begin matx[i, 1] := vet1[i]; end; for i := 1 to 25 do begin matx[i, 2] := vet2[h]; h := h - 1; end; Writeln('Matrix C '); for i := 1 to 25 do begin for j := 1 to 2 do begin write(matx[i, j], ' '); end; writeln(''); end; end.
5. Matrix and Factorial Calculation
This program calculates factorials and performs operations on a matrix.
Program Pzim; var mat1: array[1..2, 1..5] of integer; matx: array[1..10, 1..3] of integer; vet1: array[1..10] of integer; i, j, c, fat, cont: integer; begin c := 0; for i := 1 to 2 do begin for j := 1 to 5 do begin write('Enter the values for matrix A: '); readln(mat1[i, j]); c := c + 1; vet1[c] := mat1[i, j]; end; end; for i := 1 to 10 do begin fat := 1; cont := 1; repeat fat := fat * cont; cont := cont + 1; until cont > vet1[i]; matx[i, 2] := fat; end; for i := 1 to 10 do begin matx[i, 1] := vet1[i] + 3; matx[i, 3] := vet1[i] * vet1[i]; end; writeln(' Matrix C:'); for i := 1 to 10 do begin for j := 1 to 3 do begin write(matx[i, j]:7:0, ' '); end; writeln(''); end; end.
6. Matrix Row and Column Analysis
Analyzes a matrix for zeros and even numbers in rows/columns, calculates row averages.
Program Pzim; const n = 100; m = 100; var mat1: array[1..n, 1..m] of integer; vet1: array[1..n] of real; i, j, i2, j2, c, i3, x, y: integer; media: real; begin write('Enter the number of rows: '); readln(i2); write('Enter the number of columns: '); readln(j2); for i := 1 to i2 do begin for j := 1 to j2 do begin write('Enter the matrix value at position ', i, ',', j, ': '); readln(mat1[i, j]); end; end; writeln('Enter the row to check for even numbers:'); readln(x); writeln('Enter the column to check for even numbers:'); readln(y); for i := 1 to i2 do begin c := 0; for j := 1 to j2 do begin if (mat1[i, j] = 0) then c := c + 1; end; writeln('The number of zeros in row ', i, ' is: ', c); end; for j := 1 to j2 do begin c := 0; for i := 1 to i2 do begin if (mat1[i, j] = 0) then c := c + 1; end; writeln('The number of zeros in column ', j, ' is: ', c); end; c := 0; for j := 1 to j2 do begin if (mat1[x, j] mod 2 = 0) then c := c + 1; end; writeln('The number of even numbers in row ', x, ' is: ', c); c := 0; for i := 1 to i2 do begin if (mat1[i, y] mod 2 = 0) then c := c + 1; end; writeln('The number of even numbers in column ', y, ' is: ', c); for i := 1 to i2 do begin i3 := 0; media := 0; for j := 1 to j2 do begin i3 := i3 + 1; media := (media + mat1[i, j]); end; vet1[i] := media / i3; writeln('Average of row ', i, ' is: ', vet1[i]:2:1); end; end.