miércoles, 14 de diciembre de 2011
Matriz dimension N, traza
#include <stdio.h>
#define DIM 100
void LeerMatriz(float A[DIM][DIM])
{
int i,j, n;
printf("Introduce la dimension de tu matriz: \n");
scanf ("%d", &n);
for (i=0; i<n; i++)
{
printf ("Dame los %d elementos la fila %d: \n", n, i+1);
for (j=0; j<n; j++)
scanf ("%f", &A[i][j]);
}
}
void EscribirMatriz(float A[DIM][DIM])
{
int i,j, n;
printf("La matriz introducida es: \n");
for(i=0;i<n;i++)
{
printf("\nFila %d: ", i+1);
for (j=0;j<n;j++)
printf("\t%f", A[i][j]);
}
}
float TrazaMatriz(float A[DIM][DIM])
{
int i,j, fil, col;
float suma=0;
fil=col;
for(i=0;i<fil;i++)
suma+=A[i][i];
printf("\n\nLa traza (suma elementos diagonal principal) de tu matriz es: \n");
printf("%f\n\n", suma);
}
int main()
{
float A[DIM][DIM];
LeerMatriz(A);
EscribirMatriz(A);
TrazaMatriz(A);
return 0;
}
letra mas usada
#include <stdio.h>
#include <string.h>
int main()
{
char texto[20];
int i,max=0,contadores[256];
for(i=0;i <= 255;i++)
contadores[i]=0;
printf("Escribe una palabra: ");
scanf("%s",&texto);
for (i=0;i < strlen(texto);i++)
contadores[texto[i]]++;
for(i=0;i <= 255;i++)
if(contadores[i] > max)
max=contadores[i];
for(i=0;i <= 255;i++)
if(contadores[i]==max) printf("\nLa letra mas usada es la %c",i);
getchar;
return 0;
}
DICE NUMERO DE LETRAS Y ESCRIBE DEL REVES
#include <stdio.h>
int main ()
{
char c, palabra[21];
int i;
printf("Teclea una palabra de menos de 20 letras:\n");
scanf("%s", palabra);
i=0;
while (palabra[i++]!='\0');
printf("%s tiene %d letras.\n", palabra, i-1);
printf("%s escrita al reves es: ", palabra);
while (i>0) printf("%c", palabra[--i]);
printf("\n");
return 0;
}
GENERAR MATRIZ
#include<stdio.h>
#include<conio.h>
void llenar (int M[20][20], int f, int c)
{
int k=1;
for (int i=1;i<=f;i++)
{
if (i%2!=0)
{
for (int j=1;j<=c;j++)
{
M[i][j]=k; k++;
}
}
else
{
for (int j=c;j>=1;j--)
{
M[i][j]=k; k++;
}
}
}
}
void mostrar (int M[20][20], int f, int c)
{
for (int i=1;i<=f;i++)
{
printf("\n");
for (int j=1;j<=c;j++)
{
printf("[%d] ",M[i][j]);
}
}
}
int main ()
{
int f, c;
int M[20][20];
printf("Inserte filas de M: "); scanf("%d",&f);
printf("Inserte columnas de M: "); scanf("%d",&c);
llenar(M, f, c);
mostrar(M, f, c);
getch();
return 0;
}
#include<conio.h>
void llenar (int M[20][20], int f, int c)
{
int k=1;
for (int i=1;i<=f;i++)
{
if (i%2!=0)
{
for (int j=1;j<=c;j++)
{
M[i][j]=k; k++;
}
}
else
{
for (int j=c;j>=1;j--)
{
M[i][j]=k; k++;
}
}
}
}
void mostrar (int M[20][20], int f, int c)
{
for (int i=1;i<=f;i++)
{
printf("\n");
for (int j=1;j<=c;j++)
{
printf("[%d] ",M[i][j]);
}
}
}
int main ()
{
int f, c;
int M[20][20];
printf("Inserte filas de M: "); scanf("%d",&f);
printf("Inserte columnas de M: "); scanf("%d",&c);
llenar(M, f, c);
mostrar(M, f, c);
getch();
return 0;
}
cadenas
fichero de cabecera para estos es
#include<string.h>
y cadenas
#include<ctype.h>
LONGITUD DE CADENA
int strlen(char v[])
{
int n=0, i=0;
while(v[i]!='\0')
{
n++;
i++;
}
return 0;
}
forma mas abreviada de lo anterior
int strlen(char v[])
{
int n=0;
while (v[n++]!='\0');
return n;
}
COPIAR CADENAS
void strcpy(char v[],char w[])
{
int n=0;
while(w[n]!='\0')
{
v[n]=w[n];
n++;
}
v[n]='\0';
}
COMPARAR CADENAS LEXICOGRAFICAMENTE
int strcmp(char v[],char w[])
{
int lon;
lon=(strlen(v)<strlen(w))?strlen(v):strlen(w);
for(i=0;i<=lon;i++)
{
if (v[i]>w[i]) return 1;
else if(v[i]<w[i])return -1;
return 0;
}
#include<string.h>
y cadenas
#include<ctype.h>
LONGITUD DE CADENA
int strlen(char v[])
{
int n=0, i=0;
while(v[i]!='\0')
{
n++;
i++;
}
return 0;
}
forma mas abreviada de lo anterior
int strlen(char v[])
{
int n=0;
while (v[n++]!='\0');
return n;
}
COPIAR CADENAS
void strcpy(char v[],char w[])
{
int n=0;
while(w[n]!='\0')
{
v[n]=w[n];
n++;
}
v[n]='\0';
}
COMPARAR CADENAS LEXICOGRAFICAMENTE
int strcmp(char v[],char w[])
{
int lon;
lon=(strlen(v)<strlen(w))?strlen(v):strlen(w);
for(i=0;i<=lon;i++)
{
if (v[i]>w[i]) return 1;
else if(v[i]<w[i])return -1;
return 0;
}
martes, 13 de diciembre de 2011
Matriz, traspuesta y su producto.
/*Calcula la traspuesta de una matriz y luego el producto de esa matriz y su traspuesta*/
#include <iostream>
#define DIM 3 //Aqui decides la dimension, no lo se hacer como lo intentabas
void matriz_traspuesta(float A[DIM][DIM], float At[DIM][DIM]);
void producto_matriz(float A[DIM][DIM], float At[DIM][DIM]);
using namespace std;
int main()
{
int i, j;
float A[DIM][DIM], At[DIM][DIM];
cout << "Introduce la matriz:\n";
for(i=0;i<DIM;i++)
{
cout << "Introduce la fila "<<i+1<<": ";
for (j=0;j<DIM;j++)
{
cin >> A[i][j];
}
}
for(i=0;i<DIM;i++) //inicializo At con todos los elementos 0 para que lo pueda coger la funcion
{
for (j=0;j<DIM;j++)
{
At[i][j]=0;
}
}
matriz_traspuesta(A, At);
producto_matriz(A, At);
#define DIM 3 //Aqui decides la dimension, no lo se hacer como lo intentabas
void matriz_traspuesta(float A[DIM][DIM], float At[DIM][DIM]);
void producto_matriz(float A[DIM][DIM], float At[DIM][DIM]);
using namespace std;
int main()
{
int i, j;
float A[DIM][DIM], At[DIM][DIM];
cout << "Introduce la matriz:\n";
for(i=0;i<DIM;i++)
{
cout << "Introduce la fila "<<i+1<<": ";
for (j=0;j<DIM;j++)
{
cin >> A[i][j];
}
}
for(i=0;i<DIM;i++) //inicializo At con todos los elementos 0 para que lo pueda coger la funcion
{
for (j=0;j<DIM;j++)
{
At[i][j]=0;
}
}
matriz_traspuesta(A, At);
producto_matriz(A, At);
return 0;
}
}
void matriz_traspuesta(float A[DIM][DIM],float At[DIM][DIM]) //he quitado B, pues solo la usamos en la ultima funcion
{
int i,j;
cout<<"La matriz traspuesta es: \n"; //para copiar los elementos de la matriz y escribirlos en pantalla
for (i=0;i<DIM;i++) //hay que hacer lo mismo que cuando la escribimos, un bucle for
{
for(j=0;j<DIM;j++)
{
At[i][j]=A[j][i];
cout<<At[i][j]<<"\t"; // el \t es para que los elementos esten separados
}
cout<<"\n"; //para que salte de linea cada fila
}
}
{
int i,j;
cout<<"La matriz traspuesta es: \n"; //para copiar los elementos de la matriz y escribirlos en pantalla
for (i=0;i<DIM;i++) //hay que hacer lo mismo que cuando la escribimos, un bucle for
{
for(j=0;j<DIM;j++)
{
At[i][j]=A[j][i];
cout<<At[i][j]<<"\t"; // el \t es para que los elementos esten separados
}
cout<<"\n"; //para que salte de linea cada fila
}
}
void producto_matriz(float A[DIM][DIM], float At[DIM][DIM])
{
int i, j, k, elemento=0, B[DIM][DIM]; //este bucle es el mas dificil y lioso de hacer
for(i=0; i<DIM; i++)
{
for(j=0; j<DIM; j++)
{
for(k=0; k<DIM; k++)
{
elemento=elemento+(A[i][k])*(At[k][j]);
}
B[i][j]=elemento;
elemento=0;
}
}
cout<<"el producto de las matrices anteriores es\n";
for(i=0; i<DIM; i++)
{
for(j=0; j<DIM; j++)
{
cout<<B[i][j]<<"\t";
}
cout<<endl;
}
}
{
int i, j, k, elemento=0, B[DIM][DIM]; //este bucle es el mas dificil y lioso de hacer
for(i=0; i<DIM; i++)
{
for(j=0; j<DIM; j++)
{
for(k=0; k<DIM; k++)
{
elemento=elemento+(A[i][k])*(At[k][j]);
}
B[i][j]=elemento;
elemento=0;
}
}
cout<<"el producto de las matrices anteriores es\n";
for(i=0; i<DIM; i++)
{
for(j=0; j<DIM; j++)
{
cout<<B[i][j]<<"\t";
}
cout<<endl;
}
}
EXAMEN PRÁCTICO MARTES
// Los valores dentro del vector están entre 1 y 15, por eso en el rand se suma 1
#define LANZAMIENTOS 100
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include<math.h>
#define DIM 100
using namespace std;
int main()
{
float productoescalar;
int N, m;
int i[DIM], k[DIM];
float modulo1, modulo2;
modulo1=0;
modulo2=0;
cout << "Introduce la dimension de ambos vectores: "; cin >> N;
srand (time (NULL) );
for(m=0;m<N;m++) i[m]=(rand()%15)+1;
cout << endl << endl<< "Primer vector: ("; cout << " ";
for(m=0;m<N;m++) cout << i[m] << " ";
cout << ")" << endl;
for(m=0;m<N;m++) k[m]=(rand()%15)+1;
cout << endl << endl << "Segundo vector: ("; cout << " ";
for(m=0;m<N;m++) cout<< k[m] <<" ";
cout<< ")" << endl;
cout << endl << endl << "La dimension de estos dos vectores es " << N;
cout << endl << endl;
productoescalar=0;
cout << endl << endl << "Producto escalar de los vectores= ";
for(m=0;m<N;m++)
{
productoescalar=i[m]*k[m]+productoescalar;
}
cout << productoescalar << endl << endl;
for(m=0;m<N;m++)
{
modulo1=modulo1+i[m]*i[m];
}
for(m=0;m<N;m++)
{
modulo2=modulo2+k[m]*k[m];
}
if(modulo1==modulo2)
{
cout << "Tienen el mismo modulo.";
}
else
{
cout << "No tienen el mismo modulo: ";
cout << endl << modulo1;
cout << endl << endl << modulo2;
cout << endl << endl;
}
return 0;
}
#define LANZAMIENTOS 100
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include<math.h>
#define DIM 100
using namespace std;
int main()
{
float productoescalar;
int N, m;
int i[DIM], k[DIM];
float modulo1, modulo2;
modulo1=0;
modulo2=0;
cout << "Introduce la dimension de ambos vectores: "; cin >> N;
srand (time (NULL) );
for(m=0;m<N;m++) i[m]=(rand()%15)+1;
cout << endl << endl<< "Primer vector: ("; cout << " ";
for(m=0;m<N;m++) cout << i[m] << " ";
cout << ")" << endl;
for(m=0;m<N;m++) k[m]=(rand()%15)+1;
cout << endl << endl << "Segundo vector: ("; cout << " ";
for(m=0;m<N;m++) cout<< k[m] <<" ";
cout<< ")" << endl;
cout << endl << endl << "La dimension de estos dos vectores es " << N;
cout << endl << endl;
productoescalar=0;
cout << endl << endl << "Producto escalar de los vectores= ";
for(m=0;m<N;m++)
{
productoescalar=i[m]*k[m]+productoescalar;
}
cout << productoescalar << endl << endl;
for(m=0;m<N;m++)
{
modulo1=modulo1+i[m]*i[m];
}
for(m=0;m<N;m++)
{
modulo2=modulo2+k[m]*k[m];
}
if(modulo1==modulo2)
{
cout << "Tienen el mismo modulo.";
}
else
{
cout << "No tienen el mismo modulo: ";
cout << endl << modulo1;
cout << endl << endl << modulo2;
cout << endl << endl;
}
return 0;
}
Matriz aleatoria
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#define col 22
#define fil 22
#include<time.h>
#include<stdio.h>
#define col 22
#define fil 22
int main(){
int i,j,x,y;
srand(time(NULL));
int m[col][fil];
printf("introduce el numero de filas de la matriz1");
scanf("%d",&x);
printf("introduce el numero de columnas de la matriz1");
scanf("%d",&y);
int i,j,x,y;
srand(time(NULL));
int m[col][fil];
printf("introduce el numero de filas de la matriz1");
scanf("%d",&x);
printf("introduce el numero de columnas de la matriz1");
scanf("%d",&y);
for(i=0;i<x;i++){
for(j=0;j<y;j++){
m[i][j]= 1+rand()%15;
printf(" %2.d ",m[i][j]);
}
printf("\n");
}
return 0;
}
for(j=0;j<y;j++){
m[i][j]= 1+rand()%15;
printf(" %2.d ",m[i][j]);
}
printf("\n");
}
return 0;
}
Vectores aleatorios
#include <iostream>
#include <stdlib.h>
#include <time.h>
#define DIM 15
using namespace std;
int main()
{
int v[DIM], w[DIM];
int i, j;
cout<<"Dime la dimension de los vectores: ";
cin>>j;
srand(time(NULL));
for(i=0;i<j;i++)
v[i]=rand()%15;
cout<<"El primer vector es: ( ";
for(i=0;i<j;i++)
cout<<v[i]<<" ";
cout<<")"<<endl;
for(i=0;i<j;i++)
w[i]=rand()%15;
cout<<"El segundo vector es: ( ";
for(i=0;i<j;i++)
cout<<w[i]<<" ";
cout<<")"<<endl;
return 0; }
#include <stdlib.h>
#include <time.h>
#define DIM 15
using namespace std;
int main()
{
int v[DIM], w[DIM];
int i, j;
cout<<"Dime la dimension de los vectores: ";
cin>>j;
srand(time(NULL));
for(i=0;i<j;i++)
v[i]=rand()%15;
cout<<"El primer vector es: ( ";
for(i=0;i<j;i++)
cout<<v[i]<<" ";
cout<<")"<<endl;
for(i=0;i<j;i++)
w[i]=rand()%15;
cout<<"El segundo vector es: ( ";
for(i=0;i<j;i++)
cout<<w[i]<<" ";
cout<<")"<<endl;
return 0; }
MEDIA DE NÚMEROS Y DESVIACIÓN RESPECTO A ELLA (ARRAYS)
#include <stdio.h>
int main()
{
int n, cont;
float media,d,suma=0;
float lista [100];
/*leer el valor de n*/
printf("¿Cuantos numeros para calcular la media?");
scanf("%d", &n);
printf("\n");
/*leer los números y calcular su suma*/
for (cont=0; cont<n; ++cont)
{
printf("i=%d x= ", cont+1);
scanf("%f", &lista[cont]);
suma+=lista[cont];
}
/*Calcular la media y escribir la respuesta*/
media=suma/n;
printf("La media es %5.2f\n\n", media);
/*Calcular y escribir las desviaciones respecto a la media */
for(cont=0; cont<n; ++cont)
{
d=lista[cont]-media;
printf("i= %d x= %5.2f d= %5.2f\n", cont+1,lista[cont],d);
}
}
int main()
{
int n, cont;
float media,d,suma=0;
float lista [100];
/*leer el valor de n*/
printf("¿Cuantos numeros para calcular la media?");
scanf("%d", &n);
printf("\n");
/*leer los números y calcular su suma*/
for (cont=0; cont<n; ++cont)
{
printf("i=%d x= ", cont+1);
scanf("%f", &lista[cont]);
suma+=lista[cont];
}
/*Calcular la media y escribir la respuesta*/
media=suma/n;
printf("La media es %5.2f\n\n", media);
/*Calcular y escribir las desviaciones respecto a la media */
for(cont=0; cont<n; ++cont)
{
d=lista[cont]-media;
printf("i= %d x= %5.2f d= %5.2f\n", cont+1,lista[cont],d);
}
}
Ejemplo factorial: Recursivo VS Iterativo
RECURSIVO:
#include <iostream>
using namespace std;
double fact(int n)
{
if(n==1) return 1;
return(n*fact(n-1));
}
int main()
{
int x, y;
cout<<"Dame el numero del que quieres obtener el factorial: ";
cin>> x;
y= fact(x);
cout<<"El factorial de " << x << " es: " <<y;
return 0;
}
#include <iostream>
using namespace std;
double fact(int n)
{
if(n==1) return 1;
return(n*fact(n-1));
}
int main()
{
int x, y;
cout<<"Dame el numero del que quieres obtener el factorial: ";
cin>> x;
y= fact(x);
cout<<"El factorial de " << x << " es: " <<y;
return 0;
}
ITERATIVO:
#include <iostream>
using namespace std;
double fact(int n)
{
int i;
double fact=1;
for (i=1; i<=n;i++){
fact = fact*i;
}
return fact;
}
int main()
{
int x, y;
cout<<"Dame el numero del que quieres obtener el factorial: ";
cin>> x;
y= fact(x);
cout<<"El factorial de " << x << " es: " <<y;
return 0;
}
lunes, 12 de diciembre de 2011
PRODUCTO DE DOS MATRICES
#include <stdlib.h>
#include <stdio.h>
# define DIM 3
#include <stdio.h>
# define DIM 3
int main()
{
int i,j,k;
float A[DIM][DIM], B[DIM][DIM], C[DIM][DIM];
{
int i,j,k;
float A[DIM][DIM], B[DIM][DIM], C[DIM][DIM];
for(k=1;k<=2;k++) // Entrada de datos
{
printf("Dame los datos de la matriz %d: n",k);
for(i=0;i<DIM;i++)
{
printf("Dame los datos de la fila %d: n",i+1);
for(j=0;j<DIM;j++)
{
if(k==1) scanf("%f", &A[i][j]);
else scanf("%f", &B[i][j]);
}
}
}
for(i=0;i<DIM;i++) // Producto de las matrices
for(j=0;j<DIM;j++)
{
C[i][j]=0;
for(k=0;k<DIM;k++)
C[i][j] += A[i][k]*B[k][j];
}
printf("La matriz resultado es: n"); // Salida de datos
for(i=0;i<DIM;i++)
{
for(j=0;j<DIM;j++)
printf("%f ",C[i][j]);
printf("n");
}
system("PAUSE");
return 0;
}
{
printf("Dame los datos de la matriz %d: n",k);
for(i=0;i<DIM;i++)
{
printf("Dame los datos de la fila %d: n",i+1);
for(j=0;j<DIM;j++)
{
if(k==1) scanf("%f", &A[i][j]);
else scanf("%f", &B[i][j]);
}
}
}
for(i=0;i<DIM;i++) // Producto de las matrices
for(j=0;j<DIM;j++)
{
C[i][j]=0;
for(k=0;k<DIM;k++)
C[i][j] += A[i][k]*B[k][j];
}
printf("La matriz resultado es: n"); // Salida de datos
for(i=0;i<DIM;i++)
{
for(j=0;j<DIM;j++)
printf("%f ",C[i][j]);
printf("n");
}
system("PAUSE");
return 0;
}
SUMA DE DOS MATRICES
#include <stdlib.h>
#include <stdio.h>
# define DIM 3
#include <stdio.h>
# define DIM 3
int main()
{
int i,j,k;
float A[DIM][DIM], B[DIM][DIM], C[DIM][DIM];
{
int i,j,k;
float A[DIM][DIM], B[DIM][DIM], C[DIM][DIM];
for(k=1;k<=2;k++) // Entrada de datos
{
printf("Dame los datos de la matriz %d: n",k);
for(i=0;i<DIM;i++)
{
printf("Dame los datos de la fila %d: n",i+1);
for(j=0;j<DIM;j++)
{
if(k==1) scanf("%f", &A[i][j]);
else scanf("%f", &B[i][j]);
}
}
}
for(i=0;i<DIM;i++) // Suma de las matrices
for(j=0;j<DIM;j++)
C[i][j]=A[i][j]+B[i][j];
printf("La matriz resultado es: n"); // Salida de datos
for(i=0;i<DIM;i++)
{
for(j=0;j<DIM;j++)
printf("%f ",C[i][j]);
printf("n");
}
system("PAUSE");
return 0;
}
// Un poco distinto.
#include <stdio.h>
#include <stdlib.h>
#define DIM 3
void leer_matriz (float A [DIM][DIM]);
void escribir_matriz (float A [DIM][DIM]);
void sumar_matriz (float A [DIM][DIM], float B [DIM][DIM], float C [DIM][DIM]);
int main ( )
{
float A [DIM][DIM], B [DIM][DIM], C [DIM][DIM];
leer_matriz (A);
leer_matriz (B);
sumar_matriz (A,B,C);
escribir_matriz (C);
return 0;
}
void leer_matriz (float A[DIM][DIM])
{
int i,j;
printf ("Datos de tu matriz :\n");
for (i=0; i<DIM; i++)
{
printf ("Datos de la fila %d: ", i+1);
for (j=0; j<DIM; j++)
scanf ("%f", &A[i][j]);
printf ("\n");
}
}
void escribir_matriz (float A [DIM][DIM])
{
int i,j;
printf ("Datos de tu matriz: \n");
for (i=0; i<DIM; i++)
{
printf ("Datos de la fila %d: ", i+1);
for (j=0; j<DIM; j++)
printf ("%f ", A[i][j]);
printf ("\n");
}
}
void sumar_matriz(float A [DIM][DIM], float B [DIM][DIM], float C [DIM][DIM])
{
int i, j;
for (i=0; i<DIM; i++)
{
for (j=0; j<DIM; j++)
{
C[i][j]= A[i][j]+B[i][j];
}
}
}
{
printf("Dame los datos de la matriz %d: n",k);
for(i=0;i<DIM;i++)
{
printf("Dame los datos de la fila %d: n",i+1);
for(j=0;j<DIM;j++)
{
if(k==1) scanf("%f", &A[i][j]);
else scanf("%f", &B[i][j]);
}
}
}
for(i=0;i<DIM;i++) // Suma de las matrices
for(j=0;j<DIM;j++)
C[i][j]=A[i][j]+B[i][j];
printf("La matriz resultado es: n"); // Salida de datos
for(i=0;i<DIM;i++)
{
for(j=0;j<DIM;j++)
printf("%f ",C[i][j]);
printf("n");
}
system("PAUSE");
return 0;
}
// Un poco distinto.
#include <stdio.h>
#include <stdlib.h>
#define DIM 3
void leer_matriz (float A [DIM][DIM]);
void escribir_matriz (float A [DIM][DIM]);
void sumar_matriz (float A [DIM][DIM], float B [DIM][DIM], float C [DIM][DIM]);
int main ( )
{
float A [DIM][DIM], B [DIM][DIM], C [DIM][DIM];
leer_matriz (A);
leer_matriz (B);
sumar_matriz (A,B,C);
escribir_matriz (C);
return 0;
}
void leer_matriz (float A[DIM][DIM])
{
int i,j;
printf ("Datos de tu matriz :\n");
for (i=0; i<DIM; i++)
{
printf ("Datos de la fila %d: ", i+1);
for (j=0; j<DIM; j++)
scanf ("%f", &A[i][j]);
printf ("\n");
}
}
void escribir_matriz (float A [DIM][DIM])
{
int i,j;
printf ("Datos de tu matriz: \n");
for (i=0; i<DIM; i++)
{
printf ("Datos de la fila %d: ", i+1);
for (j=0; j<DIM; j++)
printf ("%f ", A[i][j]);
printf ("\n");
}
}
void sumar_matriz(float A [DIM][DIM], float B [DIM][DIM], float C [DIM][DIM])
{
int i, j;
for (i=0; i<DIM; i++)
{
for (j=0; j<DIM; j++)
{
C[i][j]= A[i][j]+B[i][j];
}
}
}
Traza y determinante de una matriz 3x3
/*Este programita calcula la traza y el determinante de una matriz de dimencion 3,
mediante dos funciones*/
#include <iostream>
using namespace std;
void traza (float A[3][3]);
void determinante (float A[3][3]);
int main()
{
float A[3][3];
int i,j;
cout << "Introduce la matriz: ";
for(i=0;i<3;i++)
{
cout << "Introduce la fila "<<i+1<<": ";
for (j=0;j<3;j++)
{
cin >> A[i][j];
}
}
traza (A);
determinante(A);
return 0;
}
void traza(float A[3][3])
{
int t;
t=A[1][1]+A[2][2]+A[0][0];
cout << "La traza es: "<< t<< endl;
}
void determinante(float A[3][3])
{
int det, pos, neg;
pos=(A[0][0]*A[1][1]*A[2][2])+(A[1][0]*A[2][1]*A[0][2])+(A[0][1]*A[1][2]*A[2][0]);
neg=(A[0][2]*A[1][1]*A[2][0])+(A[1][2]*A[2][1]*A[0][0])+(A[0][1]*A[1][0]*A[2][2]);
det=pos-neg;
cout << "El determinante es: "<<det<<endl;
}
mediante dos funciones*/
#include <iostream>
using namespace std;
void traza (float A[3][3]);
void determinante (float A[3][3]);
int main()
{
float A[3][3];
int i,j;
cout << "Introduce la matriz: ";
for(i=0;i<3;i++)
{
cout << "Introduce la fila "<<i+1<<": ";
for (j=0;j<3;j++)
{
cin >> A[i][j];
}
}
traza (A);
determinante(A);
return 0;
}
void traza(float A[3][3])
{
int t;
t=A[1][1]+A[2][2]+A[0][0];
cout << "La traza es: "<< t<< endl;
}
void determinante(float A[3][3])
{
int det, pos, neg;
pos=(A[0][0]*A[1][1]*A[2][2])+(A[1][0]*A[2][1]*A[0][2])+(A[0][1]*A[1][2]*A[2][0]);
neg=(A[0][2]*A[1][1]*A[2][0])+(A[1][2]*A[2][1]*A[0][0])+(A[0][1]*A[1][0]*A[2][2]);
det=pos-neg;
cout << "El determinante es: "<<det<<endl;
}
Producto por un escalar, escalar y planar de dos vectores.
/* Este programita calcula el producto de un vector por un escalar,
el producto escalar y el planar de dos vectores, u y v mediante funciones.
NOTA: usa estructuras */
# include <iostream>
using namespace std;
typedef struct
{
float x;
float y;
}
vector;
float producto_escalar(vector,vector);
float producto_planar(vector,vector);
vector escalar(vector,float);
int main()
{
vector v, u,c;
int a,b,elegir;
float e;
cout << "Introduce la componente x de v: ";
cin >> v.x;
cout << "Introduce la componente y de v: ";
cin >> v.y;
cout << "Introduce la componente x de u: ";
cin >> u.x;
cout << "Introduce la componente y de u: ";
cin >> u.y;
cout << "Introduce un escalar: ";
cin >> e;
cout<<"Elige una operacion 1(producto escalar), 2(producto vectorial, 3(escalar): ";
cin >> elegir;
switch (elegir){
case 1: a=producto_escalar(u,v);
cout << "El producto escalar es: "<<a<<endl;
break;
case 2: b=producto_planar(u,v);
cout << "El producto planar es: "<<b<<endl;
break;
case 3: c=escalar(u,e);
cout << "El producto de u "<<u.x<<" "<<u.y<<" por "<<e<<" es: "<<c.x<<" "<<c.y<<endl;
break;
}
return 0;
}
vector escalar(vector u,float e)
{
vector c;
c.x=u.x*e;
c.y=u.y*e;
return c;
}
float producto_escalar(vector u, vector v)
{
int a;
a=u.x*v.x+u.y*v.y;
return a;
}
float producto_planar(vector u, vector v)
{
int b;
b=u.x*v.y-u.y*v.x;
return b;
}
el producto escalar y el planar de dos vectores, u y v mediante funciones.
NOTA: usa estructuras */
# include <iostream>
using namespace std;
typedef struct
{
float x;
float y;
}
vector;
float producto_escalar(vector,vector);
float producto_planar(vector,vector);
vector escalar(vector,float);
int main()
{
vector v, u,c;
int a,b,elegir;
float e;
cout << "Introduce la componente x de v: ";
cin >> v.x;
cout << "Introduce la componente y de v: ";
cin >> v.y;
cout << "Introduce la componente x de u: ";
cin >> u.x;
cout << "Introduce la componente y de u: ";
cin >> u.y;
cout << "Introduce un escalar: ";
cin >> e;
cout<<"Elige una operacion 1(producto escalar), 2(producto vectorial, 3(escalar): ";
cin >> elegir;
switch (elegir){
case 1: a=producto_escalar(u,v);
cout << "El producto escalar es: "<<a<<endl;
break;
case 2: b=producto_planar(u,v);
cout << "El producto planar es: "<<b<<endl;
break;
case 3: c=escalar(u,e);
cout << "El producto de u "<<u.x<<" "<<u.y<<" por "<<e<<" es: "<<c.x<<" "<<c.y<<endl;
break;
}
return 0;
}
vector escalar(vector u,float e)
{
vector c;
c.x=u.x*e;
c.y=u.y*e;
return c;
}
float producto_escalar(vector u, vector v)
{
int a;
a=u.x*v.x+u.y*v.y;
return a;
}
float producto_planar(vector u, vector v)
{
int b;
b=u.x*v.y-u.y*v.x;
return b;
}
Maximo, minimo y media de una secuencia de reales.
/* Calcular el máximo y el mínimo y la media de una secuencia de N>0 reales. */
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int n,i;
double x, max, min, suma=0;
do{
cout << "Introduce la longitud que tiene la secuencia: ";
cin >> n;
}while (n<=0);
cout << "Introduce la secuencia: ";
cin >> x;
max = x;
min = x;
for (i=2;i<=n;i++){
cin >> x;
suma=suma+n;
if (x > max){
max = x;
}else{
if (x < min){
min = x;
}
}
}
cout << "El maximo es: " << max << endl;
cout << "El minimo es: " << min << endl;
cout << "La media de la secuencia es: "<< suma/n << endl;
cout << endl << endl;
return 0;
}
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int n,i;
double x, max, min, suma=0;
do{
cout << "Introduce la longitud que tiene la secuencia: ";
cin >> n;
}while (n<=0);
cout << "Introduce la secuencia: ";
cin >> x;
max = x;
min = x;
for (i=2;i<=n;i++){
cin >> x;
suma=suma+n;
if (x > max){
max = x;
}else{
if (x < min){
min = x;
}
}
}
cout << "El maximo es: " << max << endl;
cout << "El minimo es: " << min << endl;
cout << "La media de la secuencia es: "<< suma/n << endl;
cout << endl << endl;
return 0;
}
MENÚ DE OPERACIONES / C++
#include <iostream>
using namespace std;
int main()
{
int x, y, r;
char operador;
/* Presentación en pantalla de las opciones */
cout << endl << endl << "\t\t\tAritmetica de Enteros" << endl << endl;
cout << "\tPuedo realizar las siguientes operaciones con enteros:"
<< endl << endl;
cout << "\t\t+ Suma" << endl << endl;
cout << "\t\t- Diferencia" << endl << endl;
cout << "\t\t* Producto" << endl << endl;
cout << "\t\t/ Cociente de la division entera" << endl << endl;
cout << "\t\t% Resto de la division entera" << endl << endl;
cout << endl << "\tIntroduce el simbolo de la operacion: ";
/* Lectura del teclado de la opción (operador) escogida */
cin >> operador;
cout << endl;
cout << " Dame dos numeros a operar: ";
cin >> x >> y;
switch(operador)
{
case '+': r = x+y;
break;
case '-': r = x-y;
break;
case '*': r = x*y;
break;
case '/': if (y!=0) r = x/y;
case '%': if (y!=0) r = x%y;
}
if (y==0 && (operador== '/' || operador== '%'))
cout << " La operacion NO se puede hacer.\n";
else
cout << " El resultado es: " << x << operador << y << "=" << r << endl;
cout << endl << endl;
return 0;
}
using namespace std;
int main()
{
int x, y, r;
char operador;
/* Presentación en pantalla de las opciones */
cout << endl << endl << "\t\t\tAritmetica de Enteros" << endl << endl;
cout << "\tPuedo realizar las siguientes operaciones con enteros:"
<< endl << endl;
cout << "\t\t+ Suma" << endl << endl;
cout << "\t\t- Diferencia" << endl << endl;
cout << "\t\t* Producto" << endl << endl;
cout << "\t\t/ Cociente de la division entera" << endl << endl;
cout << "\t\t% Resto de la division entera" << endl << endl;
cout << endl << "\tIntroduce el simbolo de la operacion: ";
/* Lectura del teclado de la opción (operador) escogida */
cin >> operador;
cout << endl;
cout << " Dame dos numeros a operar: ";
cin >> x >> y;
switch(operador)
{
case '+': r = x+y;
break;
case '-': r = x-y;
break;
case '*': r = x*y;
break;
case '/': if (y!=0) r = x/y;
case '%': if (y!=0) r = x%y;
}
if (y==0 && (operador== '/' || operador== '%'))
cout << " La operacion NO se puede hacer.\n";
else
cout << " El resultado es: " << x << operador << y << "=" << r << endl;
cout << endl << endl;
return 0;
}
Cuadrado de un numero
#include <stdio.h>
float cuadrado(float);
int main()
{
float a;
float b;
printf("Dame un numero:");
scanf("%f",&a);
b=cuadrado(a);
printf("El cuadrado del numero vale %f\n",b);
return 0;
}
float cuadrado(float a)
{
float b;
b=a*a;
return b;
}
*(Añado también en C++ por si alguien lo prefiere)
#include <iostream>
using namespace std;
float cuadrado (float x);
int main(int argc, char *argv[]) {
float x,y;
cout << "Dame un numero; ";
cin >> x;
y = cuadrado(x);
cout << "El cuadrado es : "<<y;
return 0;
}
float cuadrado (float z)
{
float y;
y = z*z;
return y;
}
float cuadrado(float);
int main()
{
float a;
float b;
printf("Dame un numero:");
scanf("%f",&a);
b=cuadrado(a);
printf("El cuadrado del numero vale %f\n",b);
return 0;
}
float cuadrado(float a)
{
float b;
b=a*a;
return b;
}
*(Añado también en C++ por si alguien lo prefiere)
#include <iostream>
using namespace std;
float cuadrado (float x);
int main(int argc, char *argv[]) {
float x,y;
cout << "Dame un numero; ";
cin >> x;
y = cuadrado(x);
cout << "El cuadrado es : "<<y;
return 0;
}
float cuadrado (float z)
{
float y;
y = z*z;
return y;
}
Producto escalar en C++
#include <iostream>
using namespace std;
#define DIM 3
float leer_vector(float w[DIM])
{
int i;
cout<<"Dame las "<<DIM<<" coordenadas del vector :\n";
for(i=0;i<DIM;i++)
cin>>w[i];
return 0;
}
float escalar(float w[DIM], float t[DIM])
{
float esc=0;
int i;
for(i=0;i<DIM;i++)
esc=esc+w[i]*t[i];
return esc;
}
int main()
{
float esc, u[DIM], v[DIM];
leer_vector(u);
leer_vector(v);
esc=escalar(u,v);
cout<<"El productor escalar es: "<<esc<<endl;
return 0;
}
using namespace std;
#define DIM 3
float leer_vector(float w[DIM])
{
int i;
cout<<"Dame las "<<DIM<<" coordenadas del vector :\n";
for(i=0;i<DIM;i++)
cin>>w[i];
return 0;
}
float escalar(float w[DIM], float t[DIM])
{
float esc=0;
int i;
for(i=0;i<DIM;i++)
esc=esc+w[i]*t[i];
return esc;
}
int main()
{
float esc, u[DIM], v[DIM];
leer_vector(u);
leer_vector(v);
esc=escalar(u,v);
cout<<"El productor escalar es: "<<esc<<endl;
return 0;
}
Leer y escribir matriz en C
#include <stdio.h>
#define DIM 3
using namespace std;
void leer_matriz(float A[DIM][DIM])
{ int i,j;
printf("Datos de tu matriz: ");
for(i=0;i<DIM;i++)
{ printf("\nDatos de la fila %d:", i+1);
for (j=0;j<DIM;j++)
scanf("%f", &A[i][j]);
}
}
void escribir_matriz(float A[DIM][DIM])
{ int i,j;
printf("Datos de tu matriz: \n");
for(i=0;i<DIM;i++)
{ printf("\nDatos de la fila %d:", i+1);
for (j=0;j<DIM;j++)
printf("\t%f", A[i][j]);
}
}
int main()
{
float A[DIM][DIM];
leer_matriz(A);
escribir_matriz(A);
return 0;
}
#define DIM 3
using namespace std;
void leer_matriz(float A[DIM][DIM])
{ int i,j;
printf("Datos de tu matriz: ");
for(i=0;i<DIM;i++)
{ printf("\nDatos de la fila %d:", i+1);
for (j=0;j<DIM;j++)
scanf("%f", &A[i][j]);
}
}
void escribir_matriz(float A[DIM][DIM])
{ int i,j;
printf("Datos de tu matriz: \n");
for(i=0;i<DIM;i++)
{ printf("\nDatos de la fila %d:", i+1);
for (j=0;j<DIM;j++)
printf("\t%f", A[i][j]);
}
}
int main()
{
float A[DIM][DIM];
leer_matriz(A);
escribir_matriz(A);
return 0;
}
Suscribirse a:
Entradas (Atom)