/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Fevrier 2005 */ import java.io.*; public class ProduitScalaire { static BufferedReader flux = new BufferedReader(new InputStreamReader(System.in)); /* Fonction d'affichage de tous les reels contenus */ /* dans un tableau de reels */ public static void affichageTableau(double [] t) { int i; /* Pour toutes les valeurs d'indice 0 a t.length-1 inclus */ for ( i = 0 ; i < t.length ; i++ ) System.out.println(t[i]); } /* Fonction d'initialisation de tous les reels */ /* contenus dans un tableau de reels */ /* avec une valeur tiree au sort entre 0 et max */ public static void initialisationTableau(double [] t,double max) { int i; /* Pour toutes les valeurs d'indice 0 a t.length-1 inclus */ for ( i = 0 ; i < t.length ; i++ ) { /* Initialisation de t[i] avec un reel tire */ /* au hasard entre 0.0 et max */ t[i] = max*Math.random(); } } public static double produitScalaire(double [] ta,double [] tb) { double ps = 0.0; for ( int i = 0 ; i < ta.length ; i++ ) ps = ps + ta[i]*tb[i]; return(ps); } /* Fonction principale */ public static void main(String [] args) throws IOException { /* Definition et allocation de 2 tableaux de 3 reels */ double [] t1 = new double[3]; double [] t2 = new double[3]; /* Appel a la fonction d'initialisation */ initialisationTableau(t1,1.0); initialisationTableau(t2,1.0); System.out.println("Le tableau 1 contient les valeurs suivantes:"); /* Appel a la fonction d'affichage sur le premier tableau */ affichageTableau(t1); System.out.println(); System.out.println("Le tableau 2 contient les valeurs suivantes:"); /* Appel a la fonction d'affichage sur le deuxieme tableau */ affichageTableau(t2); System.out.println(); /* Calcul et affichage du produit scalaire */ System.out.println("Le produit scalaire est "+produitScalaire(t1,t2)); } }