From b82425adda54e3d7dcbe02136b55ed4cc7acb61e Mon Sep 17 00:00:00 2001 From: Leandro Lucarella Date: Sun, 23 Mar 2003 07:10:16 +0000 Subject: [PATCH 1/1] =?utf8?q?Import=20inicial=20despu=C3=A9s=20del=20"/va?= =?utf8?q?r=20incident".=20:(?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- 2.03.pas | 13 ++ 2.10.pas | 21 ++ 2.16.pas | 18 ++ 2.33.pas | 72 ++++++ 2do_parcial_ej_a.pas | 117 ++++++++++ 2do_parcial_ej_b.pas | 100 ++++++++ BinAHex.pas | 60 +++++ Estadisticas.pas | 48 ++++ MCD_MCM.pas | 50 ++++ MaxComDiv.pas | 39 ++++ Promedios.pas | 51 ++++ Recuperatorio.pas | 36 +++ _.pas | 6 + bubble_sort.txt | 52 +++++ div.pas | 35 +++ downto.pas | 7 + ejercicios.html | 544 +++++++++++++++++++++++++++++++++++++++++++ fechas(2.31).pas | 71 ++++++ fibo2.pas | 20 ++ fibonacci.pas | 32 +++ invento.pas | 16 ++ loco.cpp | 41 ++++ loco.pas | 37 +++ numeros.pas | 16 ++ parcial.pas | 40 ++++ pedorro1.pas | 26 +++ pi.pas | 14 ++ potencia.pas | 19 ++ primos1.pas | 32 +++ quicksort.txt | 142 +++++++++++ raices.pas | 22 ++ selection.pas | 48 ++++ selection_sort.txt | 34 +++ strings.pas | 12 + template.pas | 9 + valid_shellsort.txt | 47 ++++ 36 files changed, 1947 insertions(+) create mode 100644 2.03.pas create mode 100644 2.10.pas create mode 100644 2.16.pas create mode 100644 2.33.pas create mode 100644 2do_parcial_ej_a.pas create mode 100644 2do_parcial_ej_b.pas create mode 100644 BinAHex.pas create mode 100644 Estadisticas.pas create mode 100644 MCD_MCM.pas create mode 100644 MaxComDiv.pas create mode 100644 Promedios.pas create mode 100644 Recuperatorio.pas create mode 100644 _.pas create mode 100644 bubble_sort.txt create mode 100644 div.pas create mode 100644 downto.pas create mode 100644 ejercicios.html create mode 100644 fechas(2.31).pas create mode 100644 fibo2.pas create mode 100644 fibonacci.pas create mode 100644 invento.pas create mode 100644 loco.cpp create mode 100644 loco.pas create mode 100644 numeros.pas create mode 100644 parcial.pas create mode 100644 pedorro1.pas create mode 100644 pi.pas create mode 100644 potencia.pas create mode 100644 primos1.pas create mode 100644 quicksort.txt create mode 100644 raices.pas create mode 100644 selection.pas create mode 100644 selection_sort.txt create mode 100644 strings.pas create mode 100644 template.pas create mode 100644 valid_shellsort.txt diff --git a/2.03.pas b/2.03.pas new file mode 100644 index 0000000..690f381 --- /dev/null +++ b/2.03.pas @@ -0,0 +1,13 @@ +program Volumen_De_Esfera; + +var + radio, PI: real; + +begin + PI := 4 * arctan(1); + write('Ingrese el radio (en cm): '); + readln(radio); + writeln(''); + writeln('El volumen de la esfera es: ', (4 / 3) * PI * radio * radio * radio: 8: 2, ' cm3.'); + writeln('La superficie es: ', 4 * PI * radio * radio: 8: 2, ' cm2.'); +end. diff --git a/2.10.pas b/2.10.pas new file mode 100644 index 0000000..3c589e9 --- /dev/null +++ b/2.10.pas @@ -0,0 +1,21 @@ +program Cifras_4; + +var + cant, max, min, max_veces, min_veces, i: word; + numero: string; + +begin + +max := 0; (********************************) +min := 9999; (* Inicializo algunas variables *) +max_veces := 0; (********************************) +min_veces := 0; + + write('Qu‚ cantidad de n£meros desea ingresar? '); + readln(cant); + for i := 1 to cant do + begin + write('Ingrese el n£mero ', cant, '(4 cifras, natural): '); + readln(actual); + if actual > 9999 then writeln('El n£mero tiene m s de 4 cifras + diff --git a/2.16.pas b/2.16.pas new file mode 100644 index 0000000..659fee0 --- /dev/null +++ b/2.16.pas @@ -0,0 +1,18 @@ +PROGRAM ej0216; + +VAR divi, { divisor } + n: integer; { valor ingesado para dividir } + +BEGIN + divi := 2; + write('Escriba un numero para descomponer en numeros primos: '); + readln(n); + REPEAT + IF (n MOD divi) = 0 THEN BEGIN + writeln(divi: 3); { writeln ('n = ',n);{ para debug } + n := n DIV divi; + END + ELSE divi := divi + 1; + UNTIL n = 1; +END. + diff --git a/2.33.pas b/2.33.pas new file mode 100644 index 0000000..2555fdb --- /dev/null +++ b/2.33.pas @@ -0,0 +1,72 @@ +PROGRAM CalculaCoseno; + +VAR + x: real; + +(****************************************************************************) +(* Funci¢n Potencia: calcula un real elevado a un exponente entero positivo *) +(****************************************************************************) + +FUNCTION potencia(base: real; expo: integer): real; + +VAR + final: real; + i: integer; + +BEGIN + final := 1; + IF expo <> 0 THEN FOR i := 1 TO expo DO final := final * base; + writeln('potencia = ', final); (* Para Debug *) + potencia := final; +END; + +(****************************************************************************) +(* Funcion Factorial: Dado un numero devuelve su factorial. *) +(****************************************************************************) + +FUNCTION factorial(numero: integer): real; + +VAR + i: integer; + final: real; + +BEGIN + final := 1; + IF numero <> 0 THEN FOR i := 1 TO numero DO final := final * i; + writeln('factorial = ', final); (* Para debug *) + factorial := final; +END; + +(****************************************************************************) +(* Funcion Coseno: dado un  ngulo en radianes calcula el seno aproximandolo *) +(****************************************************************************) + +FUNCTION coseno(angulo: real): real; + +VAR + cose, valor: real; + termino: integer; + +BEGIN + writeln('angulo = ', angulo); (* Para Debug *) + cose := 1; + FOR termino := 1 TO 50 DO BEGIN + valor := potencia(angulo, termino * 2) / factorial(termino * 2); + IF (termino MOD 2) = 0 THEN BEGIN + IF (termino MOD 4) <> 0 THEN valor := - valor; + cose := cose + valor; + writeln('coseno actual = ', cose); (*Para Debug *) END; + END; + coseno := cose; +END; + +(****************************************************************************) +(* Comienzo del Programa Principal *) +(****************************************************************************) + +BEGIN + write('Ingrese un  ngulo en radianes: '); + readln(x); + writeln('El coseno calculado de ', x: 4: 3, ' es ', coseno(x), '.'); + writeln('El coseno ''real'' es ', cos(x), ' y coseno de pi/2 es ', cos((arctan(1) * 4)/2)); +END. diff --git a/2do_parcial_ej_a.pas b/2do_parcial_ej_a.pas new file mode 100644 index 0000000..297dc61 --- /dev/null +++ b/2do_parcial_ej_a.pas @@ -0,0 +1,117 @@ +program ejercicioA; + +const + max = 200; + +type + matriz = array[1..max, 1..max] of integer; + +(***********************************************************************) + +function GetPrimo( n: integer ): integer; + + var + primo: boolean; + i: integer; + + begin + primo := false; + while not primo do + begin + primo := true; + for i:= 2 to trunc( sqrt( n ) ) + 1 do + if ( n mod i ) = 0 then primo := false; + if not primo then n := n + 1; + end; + + GetPrimo := n; + end; + +(***********************************************************************) + +procedure CargaMatriz( var mat: matriz; tope: integer ); + + var + prim, i, j: integer; + + begin + prim := 1; + for i := 1 to tope do + for j := 1 to tope do + begin + mat[i,j] := GetPrimo( prim + 1 ); + prim := mat[i,j]; + end; + end; + +(***********************************************************************) + +procedure MuestraMatriz( var mat: matriz; tope: integer); + + var + i, j: integer; + + begin + for i := 1 to tope do + begin + for j := 1 to tope do + write( mat[i,j] : 7 ); + writeln; + end; + end; + +(***********************************************************************) + +procedure RecorreMatriz( var mat: matriz; mn, mx: integer ); + + procedure RecMat( var mat: matriz; mn, mx: integer ); + + var + i, j: integer; + + begin + if mn <= mx then + begin + for j := mn to mx do + write( mat[mn,j] : 1, ', ' ); + for i := mn + 1 to mx do + write( mat[i,mx] : 1, ', ' ); + for j := mx - 1 downto mn do + write( mat[mx,j] : 1, ', ' ); + for i := mx - 1 downto mn + 1 do + write( mat[i,mn] : 1, ', ' ); + RecMat( mat, mn + 1, mx - 1 ); + end; + end; + + begin + RecMat( mat, mn, mx ); + writeln; + end; + +(***********************************************************************) + +var + n: integer; + mat: matriz; + +begin + write( 'Ingrese un n£mero (', max : 1, ' m ximo): ' ); + readln( n ); + while n > max do + begin + writeln( 'El n£mero que ingres¢ es muy grande, el m ximo es ', max : 1 ); + write( ' Ingrese otro n£mero: ' ); + readln( n ); + end; + writeln; + writeln( ' Cargando Matriz...' ); + writeln; + CargaMatriz( mat, n ); + { MuestraMatriz( mat, n ); } + writeln; + writeln( ' Matriz cargada.' ); + writeln; + RecorreMatriz( mat, 1, n ); +end. + diff --git a/2do_parcial_ej_b.pas b/2do_parcial_ej_b.pas new file mode 100644 index 0000000..3091db6 --- /dev/null +++ b/2do_parcial_ej_b.pas @@ -0,0 +1,100 @@ +program ejercicioB; + +const + max = 10; + +type + persona = record + apellido: string[30]; + telefono: string[20]; + end; + + matriz = array[1..max, 1..max] of persona; + + +(***********************************************************************) + +procedure Rotar90Matriz( var mat: matriz; mn, mx: integer ); + + type + vector = array[1..max] of persona; + + var + i: integer; + vtemp: vector; + + begin + if mn < mx then + begin + for i := mn to mx - 1 do + vtemp[i] := mat[mn,i]; + for i := mn to mx - 1 do + mat[mn,i] := mat[i,mx]; + for i := mn + 1 to mx do + mat[mx+1-i,mx] := mat[mx,i]; + for i := mn + 1 to mx do + mat[mx,i] := mat[i,mn]; + for i := mn to mx - 1 do + mat[mx+1-i,mn] := vtemp[i]; + Rotar90Matriz( mat, mn + 1, mx - 1 ); + end; + end; + +(***********************************************************************) + +procedure MostrarMatriz( var mat: matriz; tope: integer); + + var + i, j: integer; + + begin + for i := 1 to tope do + begin + for j := 1 to tope do + write( ' (', mat[i,j].apellido, ', ', mat[i,j].telefono, ') ' ); + writeln; + end; + end; + +(***********************************************************************) + +procedure CargarMatriz( var mat: matriz; tope: integer ); + + var + i, j: integer; + + begin + for i := 1 to tope do + for j := 1 to tope do + begin + write( 'Ingrese el nobre: ' ); + readln( mat[i,j].apellido ); + write( 'Ingrese el telefono: ' ); + readln( mat[i,j].telefono ); + end; + end; + +(***********************************************************************) + + +var + mat: matriz; + n, i: integer; + +begin + write( 'Ingrese el tama¤o de la matriz (', max : 1, ' m ximo): ' ); + readln( n ); + while n > max do + begin + writeln( 'El n£mero que ingres¢ es muy grande, el m ximo es ', max : 1 ); + write( ' Ingrese otro n£mero: ' ); + readln( n ); + end; + CargarMatriz( mat, n ); + for i := 1 to 4 do + begin + MostrarMatriz( mat, n ); + Rotar90Matriz( mat, 1, n ); + writeln; + end; +end. diff --git a/BinAHex.pas b/BinAHex.pas new file mode 100644 index 0000000..7c901ac --- /dev/null +++ b/BinAHex.pas @@ -0,0 +1,60 @@ +program BinAHex; + +function pot(base, exp: integer): integer; + +begin + if exp > 0 then pot := base * pot(base, exp - 1) + else pot := 1; +end; + +var + bin, hex: string[255]; + aux: string[4]; + i, j, dhex, tam, veces: integer; + +begin + write('Ingrese un numero binario: '); + readln(bin); + aux := '0000'; + hex := ''; + tam := length(bin); + while tam > 0 do + begin + if tam mod 4 = 0 then veces := tam div 4 + else veces := (tam div 4) + 1; + for j := 0 to (veces - 1) do + begin + aux[4] := bin[tam-j*4]; + for i := 1 to 3 do + if (tam - i - j*4) > 0 + then aux[4 - i] := bin[tam - i - j*4] + else aux[4 - i] := '0'; + dhex := 0; + for i := 0 to 3 do + if aux[4-i] = '1' then dhex := dhex + pot(2, i); + case dhex of + 0: hex := hex + '0'; + 1: hex := hex + '1'; + 2: hex := hex + '2'; + 3: hex := hex + '3'; + 4: hex := hex + '4'; + 5: hex := hex + '5'; + 6: hex := hex + '6'; + 7: hex := hex + '7'; + 8: hex := hex + '8'; + 9: hex := hex + '9'; + 10: hex := hex + 'A'; + 11: hex := hex + 'B'; + 12: hex := hex + 'C'; + 13: hex := hex + 'D'; + 14: hex := hex + 'E'; + 15: hex := hex + 'F'; + end; + end; + write('Numero en hexa: '); + for i := veces downto 1 do write(hex[i]); + writeln; + tam := 0; + end; + +end. diff --git a/Estadisticas.pas b/Estadisticas.pas new file mode 100644 index 0000000..4bf1e91 --- /dev/null +++ b/Estadisticas.pas @@ -0,0 +1,48 @@ +PROGRAM Ej0254; + +VAR + a, b, total, si_a, si_b: integer; + aux: real; + sigo: boolean; + +BEGIN + total := 0; + si_a := 0; + si_b := 0; + write( 'Para terminar de ingresar valores, ingrese un numero no v lido' ); + write( 'Ecriba 1 si le gusto el producto A (0 si no le gusto): ' ); + readln( a ); + write( 'Ecriba 1 si le gusto el producto B (0 si no le gusto): ' ); + readln( b ); + sigo := ( ( a = 1 ) OR ( a = 0 ) ) AND ( ( b = 1 ) OR ( b = 0 ) ); + WHILE sigo DO + BEGIN + total := total + 1; + IF a = 1 THEN si_a := si_a + 1; + IF a = 1 THEN si_a := si_a + 1; + writeln; + write( ' A: ' ); + readln( a ); + write( ' B: ' ); + readln( b ); + sigo := ( ( a = 1 ) OR ( a = 0 ) ) AND ( ( b = 1 ) OR ( b = 0 ) ); + END; + IF total > 0 THEN + BEGIN + writeln( 'Total ' , total ); + writeln( 'Aceptaron al A el ' , ( si_a / total ) * 100 , ' %' ); + aux := ( ( si_b - si_a ) / total ) * 100; + IF aux > 0 THEN writeln( 'Aceptaron s¢lo a B el ' , aux , ' %' ) + ELSE writeln( 'Nadie acept¢ s¢lo al B' ); + aux := ( ( si_a - si_b ) / total ) * 100; + IF aux > 0 THEN writeln( 'Aceptaron s¢lo a A el ' , aux , ' %' ) + ELSE writeln( 'Nadie acept¢ s¢lo al A' ); + IF si_a > si_b THEN aux := ( si_a / total ) * 100 + ELSE aux := ( si_b / total ) * 100; + writeln( 'Aceptaron alguno de los 2 productos el ' , aux , ' %' ); + aux := ( ( total - aux ) / total ) * 100; + writeln( 'No aceptaron ninguno el ' , aux , ' %' ); + writeln( 'Aceptaron por lo menos a B el ' , ( si_b / total ) * 100 , ' %' ); + END + ELSE writeln( 'No se ingresaron valores.' ); +END. diff --git a/MCD_MCM.pas b/MCD_MCM.pas new file mode 100644 index 0000000..8c3b484 --- /dev/null +++ b/MCD_MCM.pas @@ -0,0 +1,50 @@ +PROGRAM CalcularMCDyMCM; + +VAR a, b: integer; + +{*********************************************} +{ Funcion que calcula el Maximo Comun Divisor } +{*********************************************} + +FUNCTION MCD(a, b: integer): integer; + +VAR resto: integer; + +BEGIN + IF a < b THEN BEGIN + resto := a; + a := b; + b := resto; + END; + resto := a MOD b; + WHILE resto <> 0 DO BEGIN + a := b; + b := resto; + resto := a MOD b; + END; + MCD := b; +END; + +{**********************************************} +{ Funcion que calcula el Minimo Comun Multiplo } +{**********************************************} + +FUNCTION MCM(a, b: integer): integer; + +BEGIN + MCM := (a * b) DIV (MCD(a, b)); +END; + +{********************************} +{ Comienza el programa principal } +{********************************} + +BEGIN + write('Ingrese un numero: '); + readln(a); + write('Ingrese otro numero: '); + readln(b); + writeln; + writeln('El M¡nimo Com£n M£ltiplo es: ', MCM(a, b): 3); + writeln('El M ximo Com£n Divisor es: ', MCD(a, b): 3); +END. diff --git a/MaxComDiv.pas b/MaxComDiv.pas new file mode 100644 index 0000000..e2a7409 --- /dev/null +++ b/MaxComDiv.pas @@ -0,0 +1,39 @@ +program MaxComDiv; + +function mcd( a, b: integer ): integer; + +var + aux: integer; + +begin + if a < b then + begin + aux := a; + a := b; + b := aux; + end; + if (a mod b) <> 0 then + mcd := mcd( b, a mod b ) + else + mcd := b; +end; + +const + MAX = 5; + +type + arr = array[1..MAX] of integer; + +var + numero: arr; + i: integer; + +begin + for i := 1 to MAX do + begin + write("Ingrese el ", i : 1, "§ numero: "); + readln(numero[i]); + end; + for i := 1 to max - 1 do + writeln("El MCD de ", numero[i] : 1, " y ", numero[i+1] : 1, " es ", mcd(numero[i], numero[i+1]) : 1); +end. diff --git a/Promedios.pas b/Promedios.pas new file mode 100644 index 0000000..0bfb822 --- /dev/null +++ b/Promedios.pas @@ -0,0 +1,51 @@ +PROGRAM Promedios; + +VAR + nombre, mPromedio1, mPromedio2: string[20]; + edad, total, aprobados: integer; + nota1, nota2, promedio, mProm1, mProm2: real; + hayViejos: boolean; + +BEGIN + mProm1 := 0; + mProm2 := 0; + hayViejos := false; + total := 0; + aprobados := 0; + + write( 'Escriba el nombre del alumno ( ''*'' para terminar): ' ); + readln( nombre ); + WHILE nombre <> '*' DO + BEGIN + total := total + 1; + write( 'Escriba la edad: ' ); + readln( edad ); + write( 'Escriba la primera nota: ' ); + readln( nota1 ); + write( 'Escriba la segunda nota: ' ); + readln( nota2 ); + promedio := ( nota1 + nota2 ) / 2; + IF edad > 40 THEN hayViejos := true; + IF promedio >= 4 THEN aprobados := aprobados + 1; + IF promedio > mProm1 + THEN BEGIN + mProm1 := promedio; + mPromedio1 := nombre; + END + ELSE IF promedio > mProm2 + THEN BEGIN + mProm2 := promedio; + mPromedio2 := nombre; + END; + writeln; + write( 'Escriba el nombre del siguiente alumno (''*'' para terminar): ' ); + readln( nombre ); + END; + IF total = 0 THEN writeln( 'No se ingresaron datos.' ) + ELSE BEGIN + writeln( 'El ' , ( aprobados / total ) * 100 : 3 : 2 , '% de los alumnos aprobaron.' ); + IF hayViejos THEN writeln ( 'Hay alumnos de m s de 40 a¤os.' ); + writeln( 'El mejor promedio lo obtuvo ' , mPromedio1 , '.' ); + IF mPromedio2 <> '' THEN writeln( 'El segundo mejor promedio lo obtuvo ' , mPromedio2 , '.' ); + END; +END. diff --git a/Recuperatorio.pas b/Recuperatorio.pas new file mode 100644 index 0000000..6fadba2 --- /dev/null +++ b/Recuperatorio.pas @@ -0,0 +1,36 @@ +program recup; + +function pot2( exp: integer ): integer; + + var + j, resultado: integer; + + begin + resultado := 1; + if exp > 0 then for j := 1 to exp do resultado := resultado * 2; + pot2 := resultado; + end; + +var + num, n: string[255]; + i, dec, tam: integer; + +begin + write( 'Escriba un numero binario (negativo para terminar): ' ); + readln( num ); + while num[1] <> '-' do + begin + tam := length( num ); + n := ''; + dec := 0; + for i := tam downto 1 do + n[tam-i+1] := num[i]; + for i := 1 to tam do + if n[i] = '1' then dec := dec + pot2( i - 1 ); + writeln( 'El numero en base 10 es: ', dec: 1 ); + writeln; + write( 'Ingrese otro (negativo para terminar): '); + readln( num ); + end; + writeln( 'Fin del Programa.' ); +end. diff --git a/_.pas b/_.pas new file mode 100644 index 0000000..2bddcb2 --- /dev/null +++ b/_.pas @@ -0,0 +1,6 @@ +PROGRAM ; + +VAR + +BEGIN +END. diff --git a/bubble_sort.txt b/bubble_sort.txt new file mode 100644 index 0000000..1f9c696 --- /dev/null +++ b/bubble_sort.txt @@ -0,0 +1,52 @@ +Computing Course +AlgorithmBubble Sort + Implemented in : Turbo Pascal 6.0 (ish) + DescriptionBubble sort (Procedure only) + +Explanation +Warning (Plagiarism) + + + +procedure bubble( list : array_type; n : integer) +flag : boolean; +count, temp : integer +begin +repeat begin +flag := false; +for count in 1 to n loop +if list[count+1] > list[count] +then +....temp := list[count]; +....list[count] := list[count + 1]; +....list[count + 1] := temp; +....flag := true; +end if; +end loop; +until flag=false; +end bubble; + + + + +Explanation : Bubble Sort +list is the array we are sorting, +n is the number of elements in the array. +First you set the "flag" to false to say there have been ne swaps. +loop around between 1 and the number of elements in the array. +for each loop test that position against the next position to see if a swap is +needed, +if so then swap the items over using the temporary variable temp. +set flag to true to say there has been a swap this pass, +Once the array has been checked once, ie count has gone between 1 and n, if +there are no swaps done everything is in order and we drop out of the array, if +flag is true then we "repeat" the whole process again, until no swaps are +made... +This process can be made slightly quicker, on the first pass the highest number +will always "float" to the top end of the array, therefore this element needn't +be checked next pass around, simmerly on the second pass, two numbers at the top +of the array will be in order... slowly decreasing the number of items needing +to be checked. Another variable "bottom" can be included here to do this it's +incremented every pass, and on the next pass you loop "1 to n-bottom" times. +If you need any furthur help with this don't hesitate in contacting me... I'll +try to help. diff --git a/div.pas b/div.pas new file mode 100644 index 0000000..3e72bbc --- /dev/null +++ b/div.pas @@ -0,0 +1,35 @@ +program dividir; + + +{ En este procedimiento es imprecindible que el valor pasado + como argumento de resultado este inicializado a 0 (cero). +} + +procedure divi( numerador, divisor: integer; var resultado, resto: integer ); + +begin + resto := numerador - divisor; + if resto >= 0 then + begin + resultado := resultado + 1; + divi( resto, divisor, resultado, resto ); + end + else + resto := divisor + resto; +end; + +var + resto, result, divis, num: integer; + +begin + resto := 0; + result := 0; + writeln(" Este programa realiza la divisi¢n entre dos n£meros naturales."); + write("Ingrese el numerador: "); + readln(num); + write("Ingrese el divisor: "); + readln(divis); + divi(num, divis, result, resto); + writeln(" El cociente es: ", result : 1); + writeln(" El resto es: ", resto : 1); +end. diff --git a/downto.pas b/downto.pas new file mode 100644 index 0000000..057c39b --- /dev/null +++ b/downto.pas @@ -0,0 +1,7 @@ +PROGRAM ForDownto; + +VAR i: integer; + +BEGIN + FOR i := 5 DOWNTO 0 DO writeln(i); +END. diff --git a/ejercicios.html b/ejercicios.html new file mode 100644 index 0000000..fb27f62 --- /dev/null +++ b/ejercicios.html @@ -0,0 +1,544 @@ + + + + + +Práctica N°1 - ejercicios 1 al 11 + + + + +
+ +

netdome.gif (11332 bytes)

+ +

Volver a la página principal Ejercicios +12-23

+ +
+ + + + + + + + + + + + + + + + +
Ejercicios1234567891011
+ +

(n) significa "en base n"

+ +

^ significa "elevado a"

+ +
+ +

Ejercicio 1)

+ +

Pasar a base 10 los siguientes números, de las bases indicadas:

+ +

1101 (2) = 2³ * 1 + 2² * 1 + 21 * 0 + 20 * 1 = 8 + 4 + 1 = 13

+ +

0.101 (2) = 2-1 * 1 + 2-3 * 1 = 0.5 + 0.125 = 0.625

+ +

101.11 (2) = 2² * 1 + 20 * 1 + 2-1 * 1 + 2-2 * 1= 4 + 1 + 0.5 + 0.25 = 5.75

+ +

1.0111 (2) = 20 * 1 + 2-2 * 1 + 2-3 * 1 + 2-4 * 1 = 1 + 0.25 + 0.125 + 0.0625 = 1.4375

+ +

753 (8) = 8² * 7 + 81 * 5 + 80 * 3 = 448 + 40 + 3 = 491

+ +

0.63 (8) = 8-1 * 6 + 8-2 * 3 = 0.796875

+ +

17.134 (8) = 81 * 1 + 80 * 7 + 8-1 * 1 + 8-2 * 3 + 8-3 * 4 = 8 + 7 +0.125 + 0.0078125 + +0.046875 = 15.0078125

+ +

3A (16) = 161 * 3 + 160 * 10 = 48 + 10= 58

+ +

0.FF (16) = 16-1 * 1 + 16-2 * 1 = 0.0625 + 0.00390625 = 0.06640625

+ +

A5.3B (16) = 161 * 15 + 160 * 5 + 16-1 * 3 + 16-2 * 11 = 240 + 5 + 0.1875 + 0.04296875 += 245.23046875

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 2)

+ +

Pasar los siguientes números de base 10 a la base indicada

+ +

39(2)

+ +

39 div 2 = 19 resto = 1

+ +

19 div 2 = 9 resto = 1

+ +

9 div 2 = 4 resto = 1

+ +

4 div 2 = 2 resto = 0

+ +

2 div 2 = 1 resto = 0

+ +

39 en base 10 = 10011 en base 2

+ +

Comprobación: 25 * 1 + 24 * 0 + 2³ * 0 + 2² * 1 + 2^1 * 1 + 2^0 * 1 = 32 + 4 + 2 + 1 += 39

+ +

0.525(2)

+ +

0.525*2=1.05

+ +

0.05*2=0.1

+ +

0.1*2=0.2

+ +

0.2*2=0.4

+ +

0.4*2=0.8

+ +

0.8*2=1.6

+ +

0.6*2=1.2

+ +

Si continúo operando se repite la secuencia a partir de 0.2*2, en consecuencia este +número no tiene representación finita en base 2, con lo cual si aplico el teorema +fundamental obtengo una aproximación.

+ +

23.945(2)

+
+ + + + + + +

23 div 2= 11 resto 1

+

11 div 2= 5 resto 1

+

5 div 2= 2 resto 1

+

2 div 2= 1 resto 0

0.78 *2=1.56

+

0.56 *2=1.12

+

0.945*2=1.89

+

0.89 *2=1.78

+
+ +

23.945 en base 10 = 10111.1111 en base 2

+ +

123(8)

+ +

123 div 8 = 15 resto=3

+ +

15 div 8 = 1 resto=7

+ +

1< 8; entonces no continúo con el algoritmo

+ +

123 en base 10 = 173 en base 8

+ +

3.1(8)

+ +

0.1 * 8 = 0.8

+ +

0.8 * 8 = 6.4

+ +

0.4 * 8 = 3.2

+ +

0.2 * 8 = 1.6

+ +

0.6 * 8 = 4.8

+ +

3.1 en base 2 = 3.06314 en octal

+ +

0.14(8)

+ +

0.14 * 8 = 1.12

+ +

0.12 * 8 = 0.96

+ +

0.96 * 8 =7.98

+ +

0.14 en base 10 = 0.1070 en octal

+ +

1068(16)

+ +

1068 en base 10 = 42C

+ +

61.6(16)

+ +

61 div 16 = 3 resto = 13

+ +

0.6 * 16 = 9.6

+ +

0.6 * 16 = 9.6

+ +

61.6 en base 10 = 3D.999 en base 16; este es un ejemplo de un número periódico en +hexadecimal

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 3)

+ +

Pasar el siguiente decimal a la base indicada con un error menor o igual al indicado

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Número BaseErrorN Resultado
0.26720.001100,0100010001
52.3820.000114110100,01100001010001
129.6420.1410000001,101
163.9780.00015243,7605
+ +

a)

+ +

0.267

+ +

0.267*2=0.534

+ +

0.534*2=1.068

+ +

0.068*2=0.136

+ +

0.136*2=0.272

+ +

0.272*2=0.544

+ +

0.544*2=1.088

+ +

0.088*2=0.176

+ +

0.176*2=0.352

+ +

0.352*2=0.704

+ +

0.704*2=1.408

+ +

b)

+ +

52.38

+ +

52=110100

+ +

0.38*2=0.76

+ +

0.76*2=1.52

+ +

0.52*2=1.04

+ +

0.04*2=0.08

+ +

0.08*2=0.16

+ +

0.16*2=0.32

+ +

0.32*2=0.64

+ +

0.64*2=1.28

+ +

0.28*2=0.56

+ +

0.56*2=1.12

+ +

0.12*2=0.24

+ +

0.24*2=0.48

+ +

0.48*2=0.96

+ +

0.96*2=1.92

+ +

c)

+ +

129.64

+ +

129=10000001

+ +

0.64*2=1.28

+ +

0.28*2=0.56

+ +

0.56*2=1.12

+ +

0.12*2=0.24

+ +

d)

+ +

163.97

+ +

163 div 8=20 r 3

+ +

20 div 8=2 r 4

+ +

163=243(8)

+ +

0.97*8=7.76

+ +

0.76*8=6.08

+ +

0.08*8=0.64

+ +

0.64*8=5.12

+ +

0.12*8=0.96

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 4)

+ +

Pasar a las bases indicadas usando la propiedad de base de potencia de otra base

+ +

32 en base 8 => (2) = 011010

+ +

32 div 2 = 16 resto = 0

+ +

16 div 2 = 8 resto = 0

+ +

8 div 2 = 4 resto = 0

+ +

4 div 2 = 2 resto = 0

+ +

2 div 2 = 1 resto = 0

+ +

F1 en base 16=>(2) = 11110001

+ +

73 en base 8 => (16) = 111011(2) = 3B

+ +

1010 en base 2 => (16)= A

+ +

10.10 en base 2 => (8) 010.100(2) => 2.4(8)

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 5)

+ +

Realizar las siguientes sumas:

+ +

1010(2) + 0101(2) = 1111(2)

+ +

7354(8) + 1123(8) = 10477(8) ===> 111 011 101 100 (2) + 001 001 010 011 (2) = 001 +000 100 111 111 (2)

+ +

F1E5(16) + ABC1(16) = 19DA6(16) ===> 1111 0001 1110 0101 (2) + 1010 1011 1100 0001 +(2) = 00011001110110100110(2)

+ +

3231(4) + 2123(4) = 12020(4) ===> 11 10 11 01 (2) + 10 01 10 11 (2) = 01 10 00 10 00 +(2)

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 6)

+ +

Realizar las siguientes restas:

+ +

F91F(16)- 0101(16)= 1111100100011111(2)-0000000100000001(2)

+ +

= F81E(16) = 1111100000011110(2)

+ +

0334(8) - 0137(8) =  0175(8)

+ +

1060(8) - 1776(8) = -272(8)

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 7)

+ +

Realizar las siguientes operaciones por Complemento a la base

+ + + + + + + + + + + + +

1 0 0 1 1 1 0 1 (2)

+

- 0 0 1 1 0 0 1 1 (2)

0 1 1 1 0 1 0 1 (2)

+

- 0 0 0 1 1 1 1 1 (2)

0 0 1 0 0 0 1 1 (2)

+

- 0 0 0 1 1 0 0 1 (2)

1 0 0 1 1 1 0 1 (2)

+

+ 1 1 0 0 1 1 0 1(2)

+

1 0 1 1 0 1 0 10 (2)

0 1 1 1 0 1 0 1 (2)

+

+ 1 1 1 0 0 0 0 1(2)

+

1 0 1 0 1 0 1 1 0(2)

 0 0 1 0 0 0 1 1 (2)

+

+ 1 1 1 0 0 1 1 1 (2)

+

1 0 0 0 0 1 0 1 0 (2)

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 8)

+ +

Realizar las siguientes restan en base 2

+ + + + + + + +

001000

+

- 000101

+

000011

11001

+

- 00111

+

10010

00110

+

-11000

+

Es equivalente a hacer

+

11000

+

-00110

+

10010

+

Y luego le cambio el signo

+

-10010

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 9)

+ +

Realizar los siguientes productos

+ + + + + + + +

0018 (16)

+

X 100 (16)

+

001800 (16)

047(8)

+

X 010(8)

+

470(8)

0018(18)

+

X 010(18)

+

180(18)

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 10)

+ +

Escribir con notación exceso 10000000 2

+ +

10102

+ +

10102 + 10000000 = 100010102

+ +

-F116

+ +

-11110001(2) necesita más bits para ser representado de los que permite el exceso

+ +

3014 (8)

+ +

011000001100 no puede ser representado(idem anterior)

+ +

-11002

+ +

00001100

+ +

10000000

+ +

10001100

+ +

Como es negativo; 011100112 + 1 = 011101002

+ +

-5138 no puede ser representado

+ +

-3716

+ +

00100101

+ +

10000000

+ +

10100101; 01011010+1=01011011

+ +

Volver al tope de la página

+ +
+ +

Ejercicio 11)

+ +

Escribir como complemento a dos (en 16 bits)

+ + + + + + +

-4710

+

0000000000101111

+

1111111111010000

+

+1

+

1111111111010001

-1616 

+

0000000000010000

+

1111111111101111

+

+1

+

1111111111110000

+ +

 

+ + + + + + +

3510

+

0000000000100011

F116

+

0000000011110001

+ +

Volver al tope de la página

+ +
+ +

netdome.gif (11332 bytes)

+ +
+ + diff --git a/fechas(2.31).pas b/fechas(2.31).pas new file mode 100644 index 0000000..a839f1b --- /dev/null +++ b/fechas(2.31).pas @@ -0,0 +1,71 @@ +(* + * Programa que consiste en dos funciones, la primera devuelve, + * a partir de un mes y un a¤o v lido, el numero de dias que tiene + * dicho mes. La segunda, a partir de un dia, un mes y un anio, da + * el numero de dias que tiene un mes y retorna 0 si la fecha es + * invalida. El programa en s¡, toma una fecha y devuelve el dia + * siguiente. Si la fecha ingresada es invalida, avisa al usuario + * y sale. + * + *) + +PROGRAM Fechas; + +VAR + dia, mes, anio, dias: integer; + +FUNCTION maxDias(mes, anio: integer): integer; + + BEGIN + CASE mes OF + 2: IF ((anio MOD 4 = 0) AND (anio MOD 100 <> 0)) OR (anio MOD 400 = 0) + THEN maxDias := 29 + ELSE maxDias := 28; + 4, 6, 9, 11: maxDias := 30; + ELSE maxDias := 31; + END; + END; + +FUNCTION maxDiasValido(dia, mes, anio: integer): integer; + + VAR + max: integer; + + BEGIN + IF (anio > 2100) OR (anio < 1900) + THEN maxDiasValido := 0 + ELSE IF (mes > 12) OR (mes < 1) + THEN maxDiasValido := 0 + ELSE BEGIN + max := maxDias(mes, anio); + IF (dia > max) OR (dia < 1) + THEN maxDiasValido := 0 + ELSE maxDiasValido := max; + END; + END; + +BEGIN + write('Escriba el dia: '); + readln(dia); + write('Escriba el mes: '); + readln(mes); + write('Escriba el aÏo: '); + readln(anio); + dias := maxDiasValido(dia, mes, anio); + IF dias = 0 + THEN writeln('La fecha que ingres½ no es vÿlida. Pruebe otra vez...') + ELSE BEGIN + IF dia < dias + THEN dia := dia + 1 + ELSE BEGIN + dia := 1; + IF mes = 12 + THEN BEGIN + anio := anio + 1; + mes := 1; + END + ELSE mes := mes + 1; + END; + writeln('El d­a siguiente es el ', dia: 2, '-', mes: 2, '-', anio: 4, '.'); + END; +END. diff --git a/fibo2.pas b/fibo2.pas new file mode 100644 index 0000000..beb22c2 --- /dev/null +++ b/fibo2.pas @@ -0,0 +1,20 @@ +program fibonacci; + +const + MAX = 20; + +var + i, a0, a1, ai: integer; + +begin + a0 := 0; + a1 := 1; + write( a0, a1 ); + for i := 2 to MAX do + begin + ai := a0 + a1; + a0 := a1; + a1 := ai; + write( ai ); + end; +end. diff --git a/fibonacci.pas b/fibonacci.pas new file mode 100644 index 0000000..e3e6b61 --- /dev/null +++ b/fibonacci.pas @@ -0,0 +1,32 @@ +PROGRAM Fibonacci; + +VAR x1, x2, n, aux: real; + i: integer; + + +FUNCTION numeroValido(numero: real): boolean; + +BEGIN + IF numero < 1 THEN numeroValido := false + ELSE numeroValido := true; +END; + + +BEGIN + write('Que cantidad de n£meros de la sucesion de Fibonacci desea ver? '); + readln(n); + IF numeroValido(n) THEN + BEGIN + x1 := 0; + x2 := 1; + IF n = 1 THEN writeln(x1) + ELSE writeln(x1, x2); + FOR i := 3 TO trunc(n) DO BEGIN + aux := x2; + x2 := x1 + x2; + x1 := aux; + writeln(x2); + END; + END + ELSE writeln('El n£mero debe ser mayor o igual que 1. Pruebe otra vez...'); +END. diff --git a/invento.pas b/invento.pas new file mode 100644 index 0000000..eed01b1 --- /dev/null +++ b/invento.pas @@ -0,0 +1,16 @@ +program salida; + +const + MAX = 4; + +var + i, j: integer; + +begin + for i := 0 to MAX do + begin + for j := 0 to i do + write(chr(65+j)); + writeln; + end; +end. diff --git a/loco.cpp b/loco.cpp new file mode 100644 index 0000000..60a4111 --- /dev/null +++ b/loco.cpp @@ -0,0 +1,41 @@ +/* + * Igual que "loco.pas". + * + * NO ANDA, REVISAR!!! ---> YA ANDA JOYA!!!! + * + */ + +#include +#include + +int main() +{ + bool haydoblecero = false, haycero = false; + int n = 0, suma = 0, max = -32768; + + do + { + cout << "Ingrese un numero (dos 0 seguidos para finalizar): "; + cin >> n; + if (n == 0) + { + if (haycero) haydoblecero = true; + else + { + haycero = true; + if (suma >= max) max = suma; + suma = 0; + } + } + else + { + suma += n; + haycero = false; + } + // cout << "max = " << max << " | suma = " << suma; // Para debug, + // cout << " | n = " << " | hayc = " << haycero; // muestra el estado + // cout << " | haydc = " << haydoblecero << endl; // de las variables + } while (!(haydoblecero)); + cout << "La suma mayor es: " << max << endl; + return 0; +} \ No newline at end of file diff --git a/loco.pas b/loco.pas new file mode 100644 index 0000000..430ac18 --- /dev/null +++ b/loco.pas @@ -0,0 +1,37 @@ +(*************************************************************************) +(* *) +(* Programa Loco: se ingresa una sucesion de numeros. Se utilizan los *) +(* ~~~~~~~~~~~~~~ ceros (0) para separar. Lo que se encuentra entre ce- *) +(* ros se suma. Ingresando dos ceros seguidos se finaliza la entrada de *) +(* datos. Al finalizar el programa, imprime el valor de la suma m s alta *) +(* *) +(*************************************************************************) + +PROGRAM Loco; + +VAR + haydoblecero, haycero: boolean; + n, ante, suma, max: integer; + +BEGIN + haycero := false; + haydoblecero := false; + max := -MAXINT; + suma := 0; + writeln('Ingrese una sucesion de n£meros, ingrese dos ceros seguidos para finalizar.'); + REPEAT + write('Ingrese un n£mero: '); + readln(n); + IF n = 0 THEN IF haycero THEN haydoblecero := true + ELSE BEGIN + haycero := true; + IF suma >= max THEN max := suma; + suma := 0; + END + ELSE BEGIN + suma := suma + n; + haycero := false; + END; + UNTIL haydoblecero; + writeln('La suma mayor es ', max,'.'); +END. diff --git a/numeros.pas b/numeros.pas new file mode 100644 index 0000000..026cd0f --- /dev/null +++ b/numeros.pas @@ -0,0 +1,16 @@ +program numeros_alreves; + +const + MAX = 5; + +var + i, j: integer; + +begin + for i := MAX downto 1 do + begin + for j := 1 to i do + write( j: 2 ); + writeln; + end; +end. diff --git a/parcial.pas b/parcial.pas new file mode 100644 index 0000000..940770f --- /dev/null +++ b/parcial.pas @@ -0,0 +1,40 @@ +PROGRAM DecABin; + +VAR + binario: string[255]; + numero: integer; + resto: integer; + result: integer; + i: integer; + +BEGIN + write('Ingrese el numero entero decimal a convertir (-1 termina): '); + readln(numero); + WHILE numero >= 0 DO + BEGIN + binario := ''; + resto := numero MOD 2; + result := numero DIV 2; + WHILE result > 1 DO + BEGIN + IF resto = 1 THEN binario := binario + '1' + ELSE binario := binario + '0'; + resto := result MOD 2; + result := result DIV 2; + END; + IF resto = 1 THEN binario := binario + '1' + ELSE binario := binario + '0'; + IF result = 1 THEN binario := binario + '1' + ELSE binario := binario + '0'; + writeln(''); + write('El numero es: '); + FOR i := length(binario) DOWNTO 1 DO write(binario[i]); + writeln(''); + writeln(''); + writeln(''); + write('Ingrese otro numero (-1 para terminar): '); + readln(numero); + END; + writeln(''); + writeln('Fin del Programa.'); +END. diff --git a/pedorro1.pas b/pedorro1.pas new file mode 100644 index 0000000..1b2101c --- /dev/null +++ b/pedorro1.pas @@ -0,0 +1,26 @@ +program PEDORRO1; + +var + num, suma: integer; (* num: numero ingresado/ suma: suma de numeros ingresados *) + negativo: boolean; (* flag que se activa cuando se ingresa un negativo *) + +begin + negativo := false; + suma := 0; + write ('Ingrese un n£mero entero (-1 para terminar): '); + readln (num); + while num <> -1 do + begin + suma := suma + num; + if num < 0 + then negativo := true; + write ('Ingrese otro n£mero entero (-1 para terminar: '); + readln (num); + end; + write ('La suma es ', suma); + if negativo + then writeln (' y se ingresaron valores negativos.') + else writeln (' y no se ingresaron valores negativos.'); +end. + + diff --git a/pi.pas b/pi.pas new file mode 100644 index 0000000..c93f033 --- /dev/null +++ b/pi.pas @@ -0,0 +1,14 @@ +PROGRAM NumeroPi; + +VAR + pi: real; + i, n: integer; + +BEGIN + pi := 0; + write( 'Ingrese el n£mero de t‚rminos a tomar para calcular PI: ' ); + readln( n ); + FOR i := 0 TO n DO IF ( i MOD 2 ) = 0 THEN pi := pi + ( 4 / ( ( 2 * i ) + 1 ) ) + ELSE pi := pi - ( 4 / ( ( 2 * i ) + 1 ) ); + writeln( 'PI = ' , pi : 12 : 11 , ' PI = ' , 4 * arctan( 1 ) : 40 : 39 ); +END. diff --git a/potencia.pas b/potencia.pas new file mode 100644 index 0000000..18259ac --- /dev/null +++ b/potencia.pas @@ -0,0 +1,19 @@ +program potencia; + +function pot(base, exp: integer): integer; + +begin + if exp > 0 then pot := base * pot(base, exp - 1) + else pot := 1; +end; + +var + i: integer; + cadena: string[4]; + +begin + writeln (' 2 a la 4 es: ', pot(2,4)); + writeln (' 3 al cubo es: ', pot(3,3)); + cadena := 'Luca'; + for i := 0 to 3 do if cadena[i+1] = 'a' then writeln('letra ', i+1, ' = ', pot(2,i)); +end. diff --git a/primos1.pas b/primos1.pas new file mode 100644 index 0000000..4a87fcb --- /dev/null +++ b/primos1.pas @@ -0,0 +1,32 @@ +(******************************************************************) +(* *) +(* Programa que pregunta por un n£mero y dice si es o no primo. *) +(* *) +(******************************************************************) + +program PRIMOS1; + +var + num: integer; (* Entrada de usuario *) + raiz_trunc: integer; (* Almacenamiento de la raiz cuadrada de num truncada *) + i: integer; (* Contador *) + es_primo: boolean; (* Flag para ver si es primo o no *) + +begin + +es_primo := true; + + write('Ingrese el n£mero entero que desea ver si es primo o no: '); + readln(num); + raiz_trunc := trunc(sqrt(abs(num))); + for i := 2 to raiz_trunc do + begin + if (num mod i) = 0 then + begin + writeln('No es primo, es divisible por ', i, '.'); + es_primo := false; + end; + end; + if es_primo then writeln('El n£mero ingresado es primo.'); +end. + diff --git a/quicksort.txt b/quicksort.txt new file mode 100644 index 0000000..69ae35e --- /dev/null +++ b/quicksort.txt @@ -0,0 +1,142 @@ +Computing Course +AlgorithmQuick Sort + Implemented in : Turbo Pascal 6.0 + DescriptionQuick sorts 1000 items and times how long it takes. + +Explanation + + + +program qsort; +uses crt,dos; +const +max = 1000; +type +list = array[1..max] of integer; +var +data : list; +i : integer; +h,m,s,hun : word; +procedure quicksort(var a : list; Lo,Hi: integer); +procedure sort(l,r : integer); +var +i,j,x,y : integer; +begin +i := l; j := r; x := a[( l+r ) div 2]; +repeat +while a[i] < x do i := i+1; +while x < a[j] do j := j-1; +if i < j then +begin +y := a[i]; a[i] := a[j]; a[j] := y; +i := i+1; j := j-1; +end; +until i > j; +if l < j then sort( l , j ); +if i < r then sort( i , r ); +end; +begin {quicksort}; +sort( Lo , Hi ); +end; +begin {qsort}; +write('Now generating 1000 random numbers...'); +randomize; +for i := 1 to max do data[i] := random(30000); +writeln; +writeln('Now sorting random numbers...'); +gettime(h,m,s,hun); +writeln('Start time is : ',h,' : ',m,' : ',s,' : ',hun); +quicksort( data, 1, max ); +writeln; +{for i := 1 to max do write(data[i] ); } +gettime(h,m,s,hun); +writeln('Finish time is : ',h,' : ',m,' : ',s,' : ',hun); +end. + + + +Explanation : Quick Sort +Start at "begin {qsort}" ( last paragraph) +Generate 1000 random numbers and say so. +Get the current time and display it. +Jump to "quicksort" procedure with the variables (data - the array), (1 - the +start position), and (max - the last position). +At quicksort data changes to the variable a, 1 to Lo and max to Hi. (so data,1, +max ..... goto .....a, Lo, Hi ) +Now jump down to begin "{quicksort}" (This is the start of the main procedure) +This calls the procedure "sort" with the variables Lo and Hi. +Which then become l and r (left and right) (Don't ask me why!!! I think it's to +tell the different procedure's own variables appart from each other, a bit +confusing really) +So we now have l=1 and r=1000...(the first and last positions of the array) +Now we need to keep l and r for later so we create i and j to use within the +procedure and assign them the same values as l and r. +x takes on the value of the item in the middle of the array ie. with a list of +7,4,6... x would be 4. ( this sets the pivot value around which the program will +sort left and right. ie. +Ok so now we should have the hi, lo and middle values to sort. +Now we're on the two while loops... +The first says increment i until the value contained in position i is greater +than the value pointed at by x (the middle value). +Simerly the second lowers the value of j until the value of position j is lower +than the value of the middle item. +If the items pointed at have not crossed the middle value... Ie both pointers i +and j are still on the side of the array they started on then swap the values +contained in those two positions. And move both i and j pointers one towards the +middle. +End of the if bit. +We have to repeat the testing and swapping section until everything greater than +the middle value is on the right of it and everything lower than the middle +value is on the left of it. This means that if the value of the middle position +has not got an equal amount of higher and lower numbers in the array the value +of the middle position will change addresses in the array, but it will still be +the pivot or middle value... +I think this needs an example... +0,1,2,3,7,4,5,6,8,9 - array a. +x = 1+10 div 2 = 5.....So the value of "the middle value" is +7.....a[5]=7...........Hope you're getting this. +now we test l against x...(0-7 no change necessary) (1-7..no change) (2-7..no +change) (3-7..no change) (7-7..stop) i now equils 5..( ie a[5]=7 ) +and now r against x..(7-9..no change) (7-8..nochange) (7-6..stop) j now equils +8..( ie a[8]=6 ) +Swap 7 and 6 over (using a temporary value y) leaving 0,1,2,3,6,4,5,7,8,9....x +now equils 8.. +The middle value will always be 7 this pass. (ie x=8 ... a[8]=7 .. a[x]=7 ) +As you can see now all higher numbers are to the right of 7 and all lower +nimbers to the left of 7. +Here we can say 7 is in the sorted place and can be left alone now. +We seperate the left side of the list and call the sort procedure again passing +in the parameters to say that the array only exists between positions 1 and 7 ie +l=1 and r=7... +This will continue findind a middle value and putting it in the right place +until all values have been the middle value ie lists of size one. it will go +down the left hand side of the array splitting it like it was a binary tree. +Once all Left hand options have been sorted the right hand options will be delt +with. +Furthur example... +list a is 5,3,9,4,2 ... x=3 , l=1 , r=5....swap 9 and 2.. +5,3,2,4,9 ... x=5 , l=1 , r=5....split off left +5,3,2,4 ... x=2 , l=1 , r=4....swap 5 and 2.. +So far ... 2,3,5,4,9 ... processed -9,3 +2,3,5,4... x=2 , l=1 , r=4....split off left +2... x=1 , l=1 , r=1.... split off right +So far ... 2,3,5,4,9 ... processed -9,3,2 +5,4 ... x=3 , l=3 , r=4....swap 5 and 4 +4,5 ... x=4 , l=3 , r=4...split off left +So far ... 2,3,4,5,9 ... processed -9,3,2,5 +4 ... x=4 , l=4 , r=4....stop +So far ... 2,3,4,5,9 ... processed -9,3,2,5,4 +or +positions... 1,2,3 +sort 1,2,3 +Pos 2 sorted +sort 1 +pos 1 sorted +sort 3 +pos 3 sorted +I think the key to this is realising that the array is split into unequil halves +each pass around... recursivly the lefthand side is called with one set of +parameters and then the righthand side is called with the opposing set of +parameters. +If you need any furthur help with this don't hesitate in contacting me... I'll +try to help. diff --git a/raices.pas b/raices.pas new file mode 100644 index 0000000..8db0076 --- /dev/null +++ b/raices.pas @@ -0,0 +1,22 @@ +PROGRAM NumeroPi; + +VAR + a, b, c, xre, xim: real; + +BEGIN + xim := -1; + xre := -1; + writeln( ' 2' ); + writeln( ' aX + bX + c = 0' ); + write( 'Ingrese a: ' ); + readln( a ); + write( 'Ingrese b: ' ); + readln( b ); + write( 'Ingrese c: ' ); + readln( c ); + IF ( sqr( b ) - 4 * a * c ) < 0 THEN xim := abs( sqr( b ) - 4 * a * c ) / ( 2 * a ) + ELSE xre := ( sqr( b ) - 4 * a * c ) / ( 2 * a ); + IF xre <> -1 THEN writeln( 'X1 = ' , ( ( -b ) / ( 2 * a ) ) + xre : 3 : 2 , ' X2 = ' , ( ( -b ) / ( 2* a ) ) - xre : 3 : 2 ) + ELSE IF xim <> -1 THEN writeln( 'X1 = ' , ( -b ) / ( 2 * a ) : 3 : 2 , ' + ' , xim : 3 : 2 , 'i X2 = ' , ( -b ) / ( 2* a ) : 3 : 2 , ' - ' , xim : 3 : 2 , 'i' ) + ELSE writeln( 'Hubo un error.' ); +END. diff --git a/selection.pas b/selection.pas new file mode 100644 index 0000000..67fe6ff --- /dev/null +++ b/selection.pas @@ -0,0 +1,48 @@ +program selection_demo(input,output); + const + MAX = 10; + var + a : array[1..MAX] of integer; + i, n : integer; + + procedure swap(var a,b : integer); + var + temp : integer; + begin + temp := a; + a := b; + b := temp; + end; + + procedure selection_sort; + var + i : integer; + max : integer; + top : integer; + begin + for top := n downto 2 do + begin + max := top; + for i := 1 to top-1 do + if a[i] > a[max] then + max := i; + swap(a[top],a[max]) + end + end; + + begin { main } + writeln('How many number would you like to sort (max=',MAX:2,') ?'); + readln(n); + + writeln('Enter in ',n:1,' numbers:'); + for i := 1 to n do + read(a[i]); + + selection_sort; + + for i := 1 to n do + write(a[i]:1,' '); + writeln + + end. + diff --git a/selection_sort.txt b/selection_sort.txt new file mode 100644 index 0000000..65f3aa0 --- /dev/null +++ b/selection_sort.txt @@ -0,0 +1,34 @@ +Selection Sort +Selection Sort is an elementary sorting algorithm that is designed to minimize +the number of exchanges that are performed. +It works by making N-1 passes over the unsorted portion of the array, each time +selecting the largest value. This value is then moved into its final sorted +position with a single exchange. +Here is a procedure that implements Selection Sort, assuming a global array a[ ] +with n elements, and a procedure called swap( ). + + + +procedure selection_sort; + var + i : integer; + top : integer; + max : integer; + begin + for top := n downto 2 do + begin + max := top; + for i := 1 to top-1 do + if a[i] > a[max] then + max := i; + swap(a[top],a[max]) + end + end; + + + DOWNLOAD selection.pas (complete program) +Selection Sort requires about N2/2 comparisons and about N exchanges, and is +quite insensitive to the original ordering of the input data. + + + diff --git a/strings.pas b/strings.pas new file mode 100644 index 0000000..8d4ba1c --- /dev/null +++ b/strings.pas @@ -0,0 +1,12 @@ +PROGRAM cadenas; + +VAR cadena: string[40]; + i: integer; + +BEGIN + cadena := 'Luca'; + writeln(' Cadena: ', cadena, ' Caracteres: ', length(cadena)); + for i := length(cadena) downto 1 DO write(cadena[i]); +{ write(cadena[0]); + write('lucaman'); } +END. diff --git a/template.pas b/template.pas new file mode 100644 index 0000000..ed46955 --- /dev/null +++ b/template.pas @@ -0,0 +1,9 @@ +program nombre_programa; + +const + +var + +begin + +end. diff --git a/valid_shellsort.txt b/valid_shellsort.txt new file mode 100644 index 0000000..12e9298 --- /dev/null +++ b/valid_shellsort.txt @@ -0,0 +1,47 @@ +/* Weiss used 1-n as the indices of the array rather than 0 to n-1 Also, he +modified the Ada version quite a bit so that it would "work" in C. In the Ada +version he used "and then" to force what is called short- circuit evaluation. +All C comparisons use short-circuit evaluation so the algorithm did NOT need to +be changed. */ + +#include + +void shellsort(int a[], int n) +{ + int i, j, increment; + int temp; + + for (increment = n/2; increment > 0; increment /= 2) + { + for (i = increment; i < n; i++) /*changed this line*/ + { + temp = a[i]; + j = i; + while ((j>=increment)&&(temp