Skip to content
Snippets Groups Projects
Commit 3b19d09e authored by [Loïc] [Marchand]'s avatar [Loïc] [Marchand]
Browse files

31/03/17 09:50

parent 19c20488
1 merge request!6Master
File deleted
import java.util.Random;
public class RechercheTab{
private static Random rand = new Random();
private static final int MAX_VALEUR = 150000;
public static int[] generer_tableau(int n){
int[] tableau = new int[n];
for(int i = 0;i < n;i++){
tableau[i] = rand.nextInt(MAX_VALEUR);
}
return tableau;
}
public static void afficher_tableau(int[] tableau){
for(int value : tableau){
System.out.println( + value );
}
System.out.println();
}
public static boolean rechercher(int[] tableau,int valeur){
for(int value : tableau){
if(value == valeur){
return true;
}
}
return false;
}
public static void inserer(int[] tableau,int pos,int valeur){
for(int i = pos;i < tableau.length - 1;i++){
tableau[i+1] = tableau[i];
}
tableau[pos] = valeur;
}
public static void enleverPos(int[] tableau,int pos){
for(int i = pos;i < tableau.length - 1;i++){
tableau[i] = tableau[i+1];
}
}
public static void enleverValeur(int[] tableau,int val){
for(int i = 0;i < tableau.length;i++){
if(val == tableau[i]){
enleverPos(tableau,i);
return;
}
}
}
public static void main(String[] argv){
int[] tableau;
int n, valeur;
boolean trouve;
if(argv.length < 1){
System.out.println(argv[0] +" opérande manquant");
System.out.println("Recherche une valeur aléatoire dans un tableau d'entier aléatoire de taille n");
System.out.println("Affiche le tableau et renvoie 1 si la valeur est trouvée, 0 sinon");
System.exit(1);
}
n = Integer.parseInt(argv[0]);
valeur = rand.nextInt(MAX_VALEUR);
tableau = generer_tableau(n);
//afficher_tableau(tableau);
trouve = rechercher(tableau,valeur);
System.out.println("Valeur " + valeur + " trouve : " + trouve);
inserer(tableau,1,valeur);
inserer(tableau,tableau.length-2,valeur);
int pos = rand.nextInt(n);
inserer(tableau,pos,valeur);
inserer(tableau,tableau.length-2,valeur);
enleverPos(tableau,1);
enleverValeur(tableau,valeur);
pos = rand.nextInt(n);
enleverPos(tableau,pos);
enleverPos(tableau,tableau.length - 2);
System.out.println("Valeur " + valeur + " insérée à la position " + pos);
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment