Commit 94adbc8d authored by jonas.stirnema's avatar jonas.stirnema
Browse files

Added new jmaths and added tableauxUni

parent 34d6c0f9
No preview for this file type
......@@ -25,5 +25,6 @@ uint64_t GCD(uint64_t a, uint64_t b);
uint64_t LCM(uint64_t a, uint64_t b);
bool isPrime(uint64_t n);
uint64_t fact(uint64_t n);
uint64_t fibo(uint64_t n);
#endif
\ No newline at end of file
......@@ -81,5 +81,16 @@ uint64_t fact(uint64_t n)
return r;
}
uint64_t fibo(uint64_t n)
{
if(n > 1)
{
printf("%ld", (fibo(n -1) + fibo(n -2)));
}
else
{
return n;
}
}
......@@ -56,6 +56,14 @@ int main()
timeT = ((double)t) / CLOCKS_PER_SEC;
printf("COMPUTED FACTORIAL IN %lfs\n", timeT);
printf("FACT(%ld) = %ld \n", a, r);
t = clock();
r = fibo(b);
t = clock() - t;
timeT = ((double)t) / CLOCKS_PER_SEC;
printf("COMPUTED FIBO IN %lfs\n", timeT);
printf("FACT(%ld) = %ld \n", a, r);
return 0;
}
\ No newline at end of file
CC := gcc
CC_FLAGS := -Wall -Wextra
BIN := bin
SRC := src
INCLUDE := include
LIB := lib
LIBRARIES := -lm #Library flags like -lm -lncurses
EXECUTABLE := main
all: $(BIN)/$(EXECUTABLE)
run: clean all
clear
@echo "🚀 Executing..."
./$(BIN)/$(EXECUTABLE)
$(BIN)/$(EXECUTABLE): $(SRC)/*.c
@echo "🚧 Building..."
$(CC) $(CC_FLAGS) -I $(INCLUDE) -L $(LIB) $^ -o $@ $(LIBRARIES)
clean:
@echo "🧹 Clearing..."
-rm $(BIN)/*
/*
* HEADER JRANDOM
* Author : Jonas S.
* Date : 12/10/2021
! DESCRIPTION
*/
#ifndef _JRANDOM_H_
#define _JRANDOM_H_
// DEFINES
// PROTOTYPE FUNCTIONS
uint64_t getRandomNumber(uint64_t highLimit, uint64_t lowLimit);
#endif
\ No newline at end of file
/*
* HEADER TABLEAUX_UNI
* Author : Jonas S.
* Date : 12/10/2021
! DESCRIPTION
*/
#ifndef _JTABLEAUX_H_
#define _JTABLEAUX_H_
// DEFINES
// PROTOTYPE FUNCTIONS
void showTab(uint64_t *tab, uint64_t tabSize);
void fillTabRandom(uint64_t *tab, uint64_t tabSize, uint64_t min, uint64_t max);
void fillTabWithZeroes(uint64_t *tab, uint64_t tabSize);
uint64_t getTabSmallestValue(uint64_t *tab, uint64_t tabSize);
uint64_t getTabBiggestValue(uint64_t *tab, uint64_t tabSize);
uint64_t getIndexOfTabElement(uint64_t *tab, uint64_t tabSize, uint64_t element);
void sortTabLTH(uint64_t *tab, uint64_t tabSize);
void countOccurence(uint64_t *tab, uint64_t tabSize, uint64_t *occurences, uint64_t sizeOfTabOcc);
double getMeanOfTab(uint64_t *tab, uint64_t tabSize);
double getVarianceOfTab(uint64_t *tab, uint64_t tabSize, double mean);
uint64_t getMedianOfTab(uint64_t *tab, uint64_t tabSize);
void showVerticalHistoOfTab(uint64_t *tab, uint64_t tabSize);
void showHorizonalHistoOfTab(uint64_t *occurences, uint64_t tabSize);
#endif
\ No newline at end of file
# * PROJ
# * Author: Jonas S.
# * Date: 11/07/21
# ! OBJECTIVE
# ? CREATE A BASIC RUNNING PROJECT
# ? CREATE SIMPLE C OR HEADER FILE WITH HEADERS
# ? CREATE MAKEFILE
# ! NEED COLORAMA AND OS
import colorama
from colorama import Fore, Back
#HARD CODED STUFF
NAME = "Jonas S."
colorama.init()
# ! FILE CREATION
def createFile(fileName, content):
with open(f"{fileName}", 'w', encoding = 'utf-8') as f:
f.write(content)
def createHeaderFile(fileName):
from datetime import datetime
import os
from os import mkdir
now = datetime.now() # current date and time
content = (
"/*\n"
f"\t* HEADER {fileName.upper()}\n"
f"\t* Author : {NAME}\n"
f"\t* Date : {now.strftime('%d/%m/%Y')}\n"
"\t! DESCRIPTION\n"
"*/\n\n"
f"#ifndef _{fileName.upper()}_H_\n"
f"#define _{fileName.upper()}_H_\n\n"
"// DEFINES\n\n"
"// PROTOTYPE FUNCTIONS\n\n"
"#endif"
)
if not os.path.exists('include'):
mkdir('include')
createFile(f"include/{fileName}.h", content)
def createCodeFile(fileName):
from datetime import datetime
from datetime import datetime
import os
from os import mkdir
now = datetime.now() # current date and time
content = (
"/*\n"
f"\t* CODE {fileName.upper()}\n"
f"\t* Author : {NAME}\n"
f"\t* Date : {now.strftime('%d/%m/%Y')}\n"
"\t! DESCRIPTION\n"
"*/\n\n"
)
# add basic printf if main
if fileName == "main":
content += "#include <stdio.h>\n\nint main()\n{\n\tprintf(\"ISSOU\\n\");\n\treturn 0;\n}"
if not os.path.exists('src'):
mkdir('src')
createFile(f"src/{fileName}.c", content)
def createMakefile():
content = (
"CC := gcc\n"
"CC_FLAGS := -Wall -Wextra\n\n"
"BIN := bin\n"
"SRC := src\n"
"INCLUDE := include\n"
"LIB := lib\n"
"LIBRARIES := #Library flags like -lm -lncurses\n"
"EXECUTABLE := main\n\n"
"all: $(BIN)/$(EXECUTABLE)\n\n"
"run: clean all\n"
" clear\n"
" @echo \"🚀 Executing...\"\n"
" ./$(BIN)/$(EXECUTABLE)\n\n"
"$(BIN)/$(EXECUTABLE): $(SRC)/*.c\n"
" @echo \"🚧 Building...\"\n"
" $(CC) $(CC_FLAGS) -I $(INCLUDE) -L $(LIB) $^ -o $@ $(LIBRARIES)\n\n"
"clean:\n"
" @echo \"🧹 Clearing...\"\n"
" -rm $(BIN)/*\n\n"
)
createFile("Makefile", content)
def createDirStructure():
import os
from os import mkdir
if not os.path.exists('bin'):
mkdir('bin')
if not os.path.exists('build'):
mkdir('build')
if not os.path.exists('include'):
mkdir('include')
if not os.path.exists('src'):
mkdir('src')
# ! MENU CREATION
def createCompleteProject():
# Base structure (SRC, BUILD, INCLUDE, BIN)
createDirStructure()
print(f"{Fore.GREEN}+ BASIC STRUCTURE CREATED{Fore.RESET}")
# Makefile
createMakefile()
print(f"{Fore.GREEN}+ MAKEFILE CREATED{Fore.RESET}")
# Main
createCodeFile("main")
print(f"{Fore.GREEN}+ MAIN.C FILE CREATE{Fore.RESET}")
def createCodeHeader(name):
# Code
createCodeFile(name)
print(f"{Fore.GREEN}+ CODE FILE CREATED{Fore.RESET}")
# Header
createHeaderFile(name)
print(f"{Fore.GREEN}+ HEADER FILE CREATED{Fore.RESET}")
# ! MENU
def menu():
choice = input(
"1 - COMPLETE PROJECT \n"
"2 - CODE + HEADER \n"
"3 - CODE ONLY \n"
"4 - HEADER ONLY \n"
"5 - MAKEFILE ONLY \n"
"\n"
)
# PROJECT
if choice == '1':
print(f"{Fore.BLUE}FULL PROJECT IS BEING CREATED.{Fore.RESET}")
createCompleteProject()
# CODE + HEADER
elif choice == '2':
print(f"{Fore.BLUE}A CODE AND HEADER FILE ARE BEING CREATED.{Fore.RESET}")
createCodeHeader(input("WHAT FILE NAME DO YOU WANT : "))
# CODE
elif choice == '3':
print(f"{Fore.BLUE}A CODE FILE IS BEING CREATED.{Fore.RESET}")
createCodeFile(input("WHAT FILE NAME DO YOU WANT : "))
print(f"{Fore.GREEN}+ CODE FILE CREATED{Fore.RESET}")
# HEADER
elif choice == '4':
print(f"{Fore.BLUE}A HEADER FILE IS BEING CREATED.{Fore.RESET}")
createHeaderFile(input("WHAT FILE NAME DO YOU WANT : "))
print(f"{Fore.GREEN}+ CODE FILE CREATED{Fore.RESET}")
# MAKEFILE
elif choice == '5':
print(f"{Fore.BLUE}A HEADER FILE IS BEING CREATED.")
createMakefile()
print(f"{Fore.GREEN}+ MAKEFILE CREATED{Fore.RESET}")
if __name__ == "__main__":
menu()
/*
* CODE JRANDOM
* Author : Jonas S.
* Date : 12/10/2021
! RANDOM LIB
*/
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include "../include/jrandom.h"
/**
* @brief Returns a random number
*
* @param highLimit
* @param lowLimit
* @return uint64_t
*/
uint64_t getRandomNumber(uint64_t highLimit, uint64_t lowLimit)
{
return (rand() % (highLimit - lowLimit + 1) + lowLimit);
}
\ No newline at end of file
/*
* CODE TABLEAUX_UNI
* Author : Jonas S.
* Date : 12/10/2021
! Gestion de base de tableaux
! https://malaspinas.academy/prog_seq_c_tp/tableaux_unidimensionnels/index.html
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <math.h>
#include "../include/jtableaux.h"
#include "../include/jrandom.h"
/**
* @brief Print an array in the terminal
*
* @param tab
* @param tabSize
*/
void showTab(uint64_t *tab, uint64_t tabSize)
{
for(uint64_t i = 0; i < tabSize; i++)
{
printf(" %ld,", tab[i]);
}
printf("\n");
}
/**
* @brief Fill a tab with random values
* from a min to a max value
* @param tab
* @param tabSize
* @param min
* @param max
*/
void fillTabRandom(uint64_t *tab, uint64_t tabSize, uint64_t min, uint64_t max)
{
for(uint64_t i = 0; i < tabSize; i++)
{
tab[i] = getRandomNumber(max, min);
}
}
/**
* @brief Get smallest value's index of an array
*
* @param tab
* @param tabSize
* @return uint64_t
*/
uint64_t getTabSmallestValue(uint64_t *tab, uint64_t tabSize)
{
uint64_t value = tab[0];
uint64_t index = 0;
for (uint64_t i = 1; i < tabSize; i++)
{
if (value > tab[i])
{
value = tab[i];
index = i;
}
}
return index;
}
/**
* @brief returns biggest value's index of an array
*
* @param tab
* @param tabSize
* @return uint64_t
*/
uint64_t getTabBiggestValue(uint64_t *tab, uint64_t tabSize)
{
uint64_t value = 0;
uint64_t index = 0;
for (uint64_t i = 1; i < tabSize; i++)
{
if (value < tab[i])
{
value = tab[i];
index = i;
}
}
return index;
}
/**
* @brief Sort an array Low to High Selection
*
* @param tab
* @param tabSize
*/
void sortTabLTH(uint64_t *tab, uint64_t tabSize)
{
uint64_t min_i;
uint64_t tmp_v;
for (uint64_t i = 0; i < tabSize; i++)
{
tmp_v = tab[i];
min_i = getTabSmallestValue(tab+i, tabSize-i) ;
tab[i] = tab[min_i + i];
tab[min_i + i] = tmp_v;
}
}
/**
* @brief Returns index of specified element in an array
*
* @param tab
* @param tabSize
* @param element
* @return uint64_t
*/
uint64_t getIndexOfTabElement(uint64_t *tab, uint64_t tabSize, uint64_t element)
{
for(uint64_t i = 0; i < tabSize; i++)
{
if(tab[i] == element){return i;}
}
return -1;
}
/**
* @brief Returns average value of an array
*
* @param tab
* @param tabSize
* @return double
*/
double getMeanOfTab(uint64_t *tab, uint64_t tabSize)
{
uint64_t sum = 0;
for(uint64_t i = 0; i < tabSize; i++)
{
sum += tab[i];
}
return (double)sum / tabSize;
}
/**
* @brief Returns variance of an array
*
* @param tab
* @param tabSize
* @param mean
* @return double
*/
double getVarianceOfTab(uint64_t *tab, uint64_t tabSize, double mean)
{
double var = 0;
for (uint64_t i = 0; i < tabSize; i++)
{
var += pow( (tab[i] - mean), 2);
}
return ( var / tabSize );
}
/**
* @brief Returns median of an array
*
* @param tab
* @param tabSize
* @return uint64_t
*/
uint64_t getMedianOfTab(uint64_t *tab, uint64_t tabSize)
{
if( tabSize % 2 == 0 ) // pair
{
return tab[(tabSize - 1) / 2];
}
return tab[(tabSize - 1) / 2] + tab[tabSize / 2] / 2;
}
/**
* @brief Fill an array with occurences of numbers in inital array
*
* @param tab
* @param tabSize
* @param occurences
* @param sizeOfTabOcc
*/
void countOccurence(uint64_t *tab, uint64_t tabSize, uint64_t *occurences, uint64_t sizeOfTabOcc)
{
fillTabWithZeroes(occurences, sizeOfTabOcc);
for(uint64_t i = 0; i < tabSize; i++)
{
occurences[tab[i]]++;
}
}
/**
* @brief Print a vertical histogram in terminal
*
* @param occurences
* @param tabSize
*/
void showVerticalHistoOfTab(uint64_t *occurences, uint64_t tabSize)
{
uint64_t topValue = occurences[getTabBiggestValue(occurences, tabSize)];
// PRINT ACTUAL BARS
for(uint64_t i = 0; i < tabSize; i++)
{
printf("%02ld | ", i);
for(uint64_t j = 0; j < occurences[i]; j++)
{
printf("* ");
}
printf("\n");
}
// PRINT X AXIS
printf(" └");
for(uint64_t i = 1; i < topValue + 1; i++)
{
printf("---");
}
printf("\n ");
for(uint64_t i = 1; i < topValue + 1; i++)
{
printf(" %02ld", i);
}
printf("\n");
}
/**
* @brief Print an horizontal histogram in terminal
*
* @param occurences
* @param tabSize
*/
void showHorizonalHistoOfTab(uint64_t *occurences, uint64_t tabSize)
{
uint64_t topValue = occurences[getTabBiggestValue(occurences, tabSize)];
// PRINT ACTUAL BARS
for(uint64_t i = topValue; i > 0; i--) // From topValue to 1
{
printf("%02ld | ", i);
for(uint64_t j = 0; j < tabSize; j++) // go through whole tab
{
if( occurences[j] >= i) // High enough to be printed
{
printf("** ");
}
else{printf(" ");}
}
printf("\n");
}
// PRINT X AXIS
printf(" └");
for(uint64_t i = 1; i < tabSize + 1; i++)
{
printf("---");
}
printf("\n ");
for(uint64_t i = 1; i < tabSize + 1; i++)
{
printf(" %02ld", i);
}
printf("\n");
}
/**
* @brief Fill an array with zeroes
*
* @param tab
* @param tabSize
*/
void fillTabWithZeroes(uint64_t *tab, uint64_t tabSize)
{
for(uint64_t i = 0; i < tabSize; i++)
{
tab[i] = 0;
}
}