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:
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
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
- Inicio funcion generar_elementos (int d [4] [4])
- {
- Srand(time(0))
- Int temp, restart = 0;
- For (int f = 0 to 4 step f = f + 1)
- {
- For (int c = 0 to 4 step c = c + 1)
- {
- Do
- {
- Temp=rand()%50+1
- For (int x = f to 0 step f = f - 1)
- {
- For (int y = 4 to 0 y = y – 1)
- {
- If (temp = d [x] [y])
- Restart = 1
- }
- Else restart = 0
- }
- If (restart = 1)
- }
- }
- While (restart = 1)
- d [f] [c] = temp
- }
- }
- }
- Fin funcion
- Inicio funcion desplegar_matriz (int d [4] [4] )
- {
- For (int f = 0 to 4 step f = f + 1)
- {
- For (int c = 0 to 4 step c = c + 1)
- Write , d [f] [c] , “ | “
- }
- }
- Fin funcion
- Inicio programa
- Int d [4][4]
- generar_elementos(d)
- desplegar_matriz(d)
- fin programa
#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