Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Experimento 7
Experimento 7
4. struct Matriz { int filas,columnas; Int *elemento; }; void Definir(struct Matriz m); struct Matriz Sumar(struct Matriz m1, struct Matriz m2); struct Matriz Restar(struct Matriz m1, struct Matriz m2); Double Determinante(struct Matriz m); struct Matriz Inversa (struct Matriz m);
VECTOR. H
#ifndef Vector_H #define Vector_H #include <stdio.h> #include <math.h> #include <stdlib.h> struct Vector { int t; int *elemento; }; void definir(struct Vector v, int tt) { v.t = tt; v.elemento = (int *)malloc(sizeof(int)*tt) ; for (int i=0;i<tt;i++) { v.elemento[i] =0; } } int tam(struct Vector v)
{ return (v.t); } void llenar(struct Vector v) { int temp; for (int i = 0; i<tam(v); i++) { printf("Ingrese el elemento numero %d \n", i+1); scanf("%d", &temp); v.elemento[i] = temp; } } void borrar (struct Vector v) { v.t = 0; free (v.elemento); v.elemento = NULL; } double magnitud(struct Vector v) { double res =0; for (int i=0;i<tam(v);i++) { res += v.elemento[i]*v.elemento[i]; } return (sqrt(res)); } void copia(struct Vector fuente, struct Vector destino) { for (int i=0;i<tam(fuente);i++) { destino.elemento[i] = fuente.elemento[i]; } } int ppunto (struct Vector v1, struct Vector v2)
{ struct Vector temporal; int res=0; if (tam(v1)>tam(v2)) { definir(temporal,tam(v1)); copia(v2,temporal); for (int i=0;i<tam(v1);i++) res += v1.elemento[i] * temporal.elemento[i]; } else { definir(temporal,tam(v2)); copia(v1,temporal); for (int i=0;i<tam(v2);i++) res += v2.elemento[i] * temporal.elemento[i]; } return res; } Vector suma(Vector &v1, Vector &v2) { Vector res; if (tam(v1)>tam(v2)) { definir(res,tam(v1)); copia(v1,res); for (int i=0;i<tam(v2);i++) res.elemento[i] += v2.elemento[i]; } else { definir(res,tam(v2)); copia(v2,res); for (int i=0;i<tam(v1);i++) res.elemento[i] += v1.elemento[i]; } return res; } struct Vector resta(struct Vector v1, struct Vector v2)
{ struct Vector res; if (tam(v1)>tam(v2)) { definir(res,tam(v1)); copia(v1,res); for (int i=0;i<tam(v2);i++) res.elemento[i] -= v2.elemento[i]; } else { definir(res,tam(v2)); copia(v1,res); for (int i=0;i<tam(v2);i++) res.elemento[i] -= v2.elemento[i]; } return res; } struct Vector operator+(stuct Vector v1, struct Vector v2) { return suma(v1,v2); } struct Vector operator-(struct Vector v1, struct Vector v2) { return resta(v1,v2); } void mostrar(struct Vector v) { for (int i=0;i<tam(v);i++) if (i != (tam(v) -1) ) printf("%d (e%d) %c ", v.elemento[i], i+1, v.elemento[i]<0?'-':'+'); else printf("%d (e%d) \n", v.elemento[i], i+1); } #endif
EJERCITADOR.C
#include "vector.h" #include <stdio.h> void main() { struct Vector v1, v2; int t1; //tamao del vector printf("Ingrese el tamao del vector 1 \n"); scanf("%d", &t1); definir(v1,t1); llenar(v1); printf("Ingrese el tamao del vector 2 \n"); scanf("%d", &t1); definir(v2,t1); llenar(v2); printf("La magnitud del vector 1 es = %lf \n", magnitud(v1)); printf("La magnitud del vector 2 es = %lf \n", magnitud(v2)); printf("El producto punto entre los dos vectores es : %d \n", ppunto(v1,v2)); printf("La suma v1 + v2 = "); mostrar(v1+v2); printf("La resta v1-v2 = "); mostrar(v1-v2); }