viernes, 8 de mayo de 2015

Practica 6

Ejercicio 2

(Muñoz Organes Alejandro)


Generar en forma aleatoria una matriz de 4x4. Desplegar la matriz y sumar la diagonal principal y la diagonal inversa. Imprimir las sumas e indicar cual de las sumas es mayor.

Pseudocodigo:

Inicio función generar_datos(int k[4][4])
       srand(time(0))
       for(from i = 0 to 4 step i=i+1)
             for(from j = 0 to 4 step j=j+1)
                    k[i][j]=rand()%100+1
fin funcion

Inicio función desplegar_tabla(int k[4][4])
       for(from i = 0 to 4 step i=i+1)
       {
             for(from j = 0 to 4 step j=j+1)
                    write k[i][j], "  |"
       }
fin funcion

Inicio function int suma_Dp(int k[4][4])
       int sum=0,j=0
       write "Suma diagonal principal: "
       for(from i = 0 to 4 step i=i+1)
       {
             sum+=k[i][j]
             write k[i][j] "  "
             j=j+1
       }
       write "= ", sum
       return sum
fin funcion

Inicio function int suma_Di(int k[4][4])
       int sum=0
       int j=3
       write "Suma diagonal inversa: "
       for(from i = 0 to 4 step i=i-1)
       {
             sum+=k[i][j]
             write k[i][j], "  "
             j=j-1
       }
       write "= ", sum
       return sum
fin funcion

Inicio programa
       int k[4][4]
    generar_datos(k)
    desplegar_tabla(k)
    int Dp = suma_Dp(k)
    int Di = suma_Di(k)
    if(Dp>Di)write "La suma de la diagonal principal es mayor"
    else write "La suma de la diagonal inversa es mayor"
    end if
fin

Codigo:
#include <iostream.h>
#include <conio.h>
#include <iomanip>
#include <stdlib.h>
#include <time.h>
/*
Practica No.6 Problema No.2 Fecha:08/05/2015
Nombre: Muñoz Organes Alejandro No.Control: 14211337
*/
void generar_datos(int k[4][4])
{
srand(time(0));
for(int i=0 ; i<4; i++)
for(int j=0 ; j<4; j++)
k[i][j]=rand()%100+1;
}

void desplegar_tabla(int k[4][4])
{
for(int i=0 ; i<4; i++)
{
for(int j=0 ; j<4; j++)
cout<<"\t"<<setw(3)<<k[i][j]<<"  |";
cout<<endl<<endl;
}
}

int suma_Dp(int k[4][4])
{
int sum=0,j=0;
cout<<"\nSuma diagonal principal: ";
for(int i=0 ; i<4; i++)
{
sum+=k[i][j];
cout<<k[i][j]<<"  ";
j++;
}
cout<<"= "<<sum<<endl;
return sum;
}

int suma_Di(int k[4][4])
{
int sum=0;
int j=3;
cout<<"\nSuma diagonal inversa: ";
for(int i=0 ; i<4; i++)
{
sum+=k[i][j];
cout<<k[i][j]<<"  ";
j--;
}
cout<<"= "<<sum<<endl;
return sum;
}

int main()
{
int k[4][4];
 generar_datos(k);
 desplegar_tabla(k);
 int Dp = suma_Dp(k);
 int Di = suma_Di(k);
 if(Dp>Di) cout<<"\nLa suma de la diagonal principal es mayor";
 else cout<<"\nLa suma de la diagonal inversa es mayor";
 getch();
 return 0;
}


Ejercicio 3
(Olvera Vázquez Scarlett)

3.-Generar una matriz cuadrada de 10 x 10 con números aleatorios entre 1 Y 100, manejando el siguiente criterio las filas se llenarán con números acordes a ella. Si son filas impares (1,3,5...9) con números impares y si son filas pares (0,2,4,..8) con números pares. Desplegar la matriz.

Pseudocodigo

Inicio funcion generarnum(int N[10][10])
srand(time(0));
int y
for (int i=0 to  i<10 step i++)
{
for (int j=0 to j<10 step j++)
{
if (i%2==0) then
do
{
y=rand()%100 +1
}
while(y%2!=0)
else
do
{
y=rand()%100 +1
}
while(y%2==0)
}
N[i][j]=y
 }
Fin funcion

Inicio funcion desplegarnum(int N[10][10])
for(int i=0 ; i<10; i++)
{
for(int j=0 ; j<10; j++)
Write N[i][j]
}
Fin funcion

Inicio programa
int valores[10][10]
generarnum(valores)
desplegarnum(valores)
fin

Código del Programa

#include <conio.h>
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
/* Practica No.6 Problema No.3 Fecha:07/04/2015
Nombre: Olvera Vazquez Scarlett No.Control: 14211908*/

void generarnum(int N[10][10])
{
srand(time(0));
int y;
for (int i=0; i<10; i++)
{
for (int j=0; j<10; j++)
{
if (i%2==0)
{
do
{
y=rand()%100 +1;
}
while(y%2!=0);
}
else
{
do
{
y=rand()%100 +1;
}
while(y%2==0);
}
N[i][j]=y;
}
}
}
void desplegarnum(int N[10][10])
{
for(int i=0 ; i<10; i++)
{
for(int j=0 ; j<10; j++)
cout<<N[i][j]<<"\t";
cout<<endl;
}
}
int main()
{
int valores[10][10];
generarnum(valores);
desplegarnum(valores);
getch();
return 0;
}



Ejercicio 4

(Utrera Cámara Jesús Manuel)
Pseudocodigo

  1. Inicio funcion generar_elementos (int d [4] [4])
  2. {
  3. Srand(time(0))
  4. Int temp, restart = 0;
  5. For (int f = 0 to 4 step f = f + 1)
  6. {
  7. For (int c = 0 to 4 step c = c + 1)
  8. {
  9. Do
  10. {
  11. Temp=rand()%50+1
  12. For (int x = f   to 0 step f = f - 1)
  13. {
  14. For (int y = 4 to 0 y = y – 1)
  15. {
  16. If (temp = d [x] [y])
  17. Restart = 1
  18. }
  19. Else restart = 0
  20. }
  21. If (restart = 1)
  22. }
  23. }
  24. While (restart = 1)
  25. d [f] [c] = temp
  26. }
  27. }
  28. }
  29. Fin funcion
  30. Inicio funcion  desplegar_matriz  (int d [4] [4] )
  31. {
  32. For (int f = 0 to 4 step f = f + 1)
  33. {
  34. For (int c = 0 to 4 step c = c + 1)
  35. Write ,  d [f] [c] , “ | “
  36. }
  37. }
  38. Fin funcion
  39. Inicio programa
  40. Int d [4][4]
  41. generar_elementos(d)
  42. desplegar_matriz(d)
  43. fin programa

Codigo

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip.h>

/*Pratica 6  Problema 4  Fecha 14/Mayo/15
Nombre: Utrera Camara Jesus Manuel   No.Control: 14211345 */

void generar_elementos(int d[4][4])
{
srand(time(0));
int temp,restart = 0;
for(int f=0 ; f<4; f++)
{
for(int c=0 ; c<4; c++)
{
do
{
temp=rand()%50+1;
for(int x=f ; x>=0; x--)
{
for (int y = 4;y >=0;y--)
{
if (temp == d[x][y])
{
restart = 1;
break;
}
else restart = 0;
}
if (restart == 1)break;
}
}
while(restart==1);
d[f][c]=temp;
}
}
}

void desplegar_matriz(int d[4][4])
{
for(int f=0 ; f<4; f++)
{
for(int c=0 ; c<4; c++)
cout<<"\t"<<setw(3)<<d[f][c]<<"  |";
cout<<endl<<endl;
}
}

int main ()
{
int d[4][4];
generar_elementos(d);
desplegar_matriz(d);
getch ();
return 0;
}


No hay comentarios.:

Publicar un comentario