diff options
| author | Gaspard Coulet <gaspard.coulet@mines-ales.org> | 2021-04-28 23:05:53 +0200 |
|---|---|---|
| committer | Gaspard Coulet <gaspard.coulet@mines-ales.org> | 2021-04-28 23:05:53 +0200 |
| commit | 9fe033ea88c2f705ec18c232873d056e0c229d72 (patch) | |
| tree | 0647dc8c51610c7336c88c04de2068ea14b21e17 /sem_2/HLIN202/TP/TP11-12/exo10.cpp | |
Initial commit
Diffstat (limited to 'sem_2/HLIN202/TP/TP11-12/exo10.cpp')
| -rw-r--r-- | sem_2/HLIN202/TP/TP11-12/exo10.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/sem_2/HLIN202/TP/TP11-12/exo10.cpp b/sem_2/HLIN202/TP/TP11-12/exo10.cpp new file mode 100644 index 0000000..f53abed --- /dev/null +++ b/sem_2/HLIN202/TP/TP11-12/exo10.cpp @@ -0,0 +1,66 @@ +#include<iostream>
+#include<cmath>
+struct vecteur {
+ int dimension;
+ char nom;
+ double *coords;
+};
+
+vecteur* init ( int taille, char nom) {
+ vecteur * ret = new vecteur;
+ ret->dimension= taille;
+ ret->coords = new double[taille];
+ ret->nom=nom;
+ for ( int i =0; i < taille; i ++) {
+ ret->coords[i]=0;
+ }
+ return ret;
+}
+
+vecteur * oppose ( vecteur * vect) {
+ vecteur * ret = init(vect->dimension, vect->nom);
+ for ( int i =0; i < vect->dimension; i ++) {
+ ret->coords[i]= -vect->coords[i];
+ }
+ return ret;
+}
+
+vecteur * somme ( vecteur * vect1, vecteur * vect2) {
+ if ( vect1->dimension == vect2->dimension) {
+ char nom = 's';
+ vecteur * ret=init ( vect1->dimension, nom);
+ for ( int i =0; i < ret->dimension; i ++) {
+ ret->coords[i]= vect1->coords[i]+ vect2->coords[i];
+}
+return ret;
+}
+return 0;
+}
+
+double scalaire ( vecteur * u, vecteur * v ) {
+ if ( u->dimension != v->dimension) {
+ std::cout<< " erreur " << std::endl;
+ return 0; }
+ double ret=0;
+ for ( int i = 0; i < u->dimension; i ++) {
+ ret += u->coords[i]*v->coords[i];
+ }
+ return ret;
+}
+
+double norme ( vecteur * u ) {
+ return sqrt(scalaire ( u,u));
+}
+
+int main () {
+ vecteur * u = init(2, *"u");
+ vecteur * v = init(2, *"v");
+ v->coords[0]= 0;
+ v->coords[1]= 1;
+ u->coords[0]= 45;
+ u->coords[1]= 1;
+ std::cout<< scalaire(u,v)<<std::endl;
+ std::cout<< " Norme de u : " << norme(u)<<std::endl;
+
+ return 0;
+}
|
