2025-02-25 11:50:08 +01:00

168 lines
3.5 KiB
C

#include <iso646.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Variables
const int size=8;
int *alloc_tab(){
int *tab=NULL;
tab=malloc(sizeof(int)*size);
tab[0]=1;
tab[1]=2;
tab[2]=4;
tab[3]=8;
tab[4]=3;
tab[5]=7;
tab[6]=6;
tab[7]=9;
return tab;
}
int generaterandoms(int min, int max, int count) {
// Taking current time as seed
unsigned int seed = time(0);
for (int i = 0; i < count; i++) {
// Generate a random number in the range [min, max]
int rd_num = rand_r(&seed) % (max - min + 1) + min;
return rd_num;
}
}
void display_tab(int tab[]) {
int i;
for (i=0; i<size; i++) {
printf("%d ",tab[i]);
}
printf("\n");
}
bool dict_sort(int tab[], int cible) {
// Définition et Assignation des variables
int pointer = 1;
int intervale= size;
int pivot;
bool trouve=false;
while (pointer<=intervale && trouve==false) {
pivot = (pointer+intervale)/2;
printf("Pointer: %d, Intervalle: %d, Pivot: %d \n ",pointer , intervale, pivot);
if (tab[pivot]==cible) {
trouve=true;
}
else {
if (cible<tab[pivot]) {
intervale=pivot-1;
}
else {
pointer=pivot+1;
}
}
}
if (trouve==true) {
printf("Found !\n");
}
else {
printf("Not found !\n");
}
return trouve;
}
void insert(int tab[], int position, int element) {
int i = position;
while (i>0 && tab[i]>element) {
tab[i+1]=tab[i];
i=i-1;
}
tab[i+1]=element;
display_tab(tab);
}
void sortinsert(int tab[]) {
int i;
for (i=2; i<=size; i++) {
insert(tab, i-1, tab[i]);
}
display_tab(tab);
}
void permutation(int tab[], int premier, int deuxieme) {
int temp;
temp=tab[premier];
tab[premier]=tab[deuxieme];
tab[deuxieme]=temp;
display_tab(tab);
}
void bubblesort(int tab[]) {
for (int i=size-1; i>1; i--) {
for (int j=0; j<i-1; j++) {
if (tab[j]>tab[j+1]) {
permutation(tab, j, j+1);
}
}
}
display_tab(tab);
}
void hiddennumber() {
int number= generaterandoms(0,64,1);
int selection;
int tries=1;
bool found=false;
printf("I choosed a number between 0 and 64, good luck ! \n");
while(found==false) {
printf("Make a guess ! \n");
scanf("%d",&selection);
if (selection==number) {
found=true;
printf("Hidden number found ! \n It was : %d \n", number);
printf("Found in %d tries \n", tries);
break;
}
else {
printf("WRONG ! \n");
tries++;
if (selection>number) {
printf("Smaller \n");
}
else if (selection<number) {
printf("Larger \n");
}
}
}
char again[1];
printf("Play again ? (o/n) ");
scanf("%s", &again);
if (again[0]=='o') {
hiddennumber();
}
else if (again[0]=='n') {
printf("Until next time ! \n");
}
}
void xmastree() {
int selection;
printf("Tree height ?: ");
scanf("%d",&selection);
for (int i=0; ; i++) {}
}
int main(void) {
int* tab=alloc_tab();
int tab2[4]={1,2,3,4};
//dict_sort(tab, 5);
//sortinsert(tab);
//permutation(tab, 1, 2);
//bubblesort(tab);
hiddennumber();
// printRandoms(2, 100, 10);
free(tab);
return 0;
}