Skip to content

Instantly share code, notes, and snippets.

@Samu31Nd
Last active March 15, 2023 17:54
Show Gist options
  • Select an option

  • Save Samu31Nd/567d6bee863ca7f514ee324374634698 to your computer and use it in GitHub Desktop.

Select an option

Save Samu31Nd/567d6bee863ca7f514ee324374634698 to your computer and use it in GitHub Desktop.
Practica 1 - Complejidad de algoritmos
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void askUserData(int*);
int *createDinamicArray(int);
void fillArrWRandVar(int*,int);
void showArr(int*,int);
double insertSort(int*,int);
void showMsg(int);
void showTime(double);
void freeMemory(int*);
int main(int argc, char const *argv[])
{
srand(time(NULL));
int n, *arrElements;
askUserData(&n);
arrElements = createDinamicArray(n);
fillArrWRandVar(arrElements,n);
showArr(arrElements,n);
double time_spent = insertSort(arrElements,n);
showArr(arrElements,n);
showTime(time_spent);
freeMemory(arrElements);
return 0;
}
void askUserData(int *n){
printf("How many elements?: ");
scanf("%d",n);
}
int *createDinamicArray(int n){
int *array;
array = (int*)malloc(sizeof(int)*n);
if(array == NULL){
showMsg(0);
exit(-1);
}
return array;
}
void fillArrWRandVar(int *arr,int n){
for (int i = 0; i < n; i++)
arr[i] = rand()%100;
}
void showArr(int *arr, int n){
int i, showN = n;
if(n > 100) showN = 25;
for (i = 0; i < showN; i++) {
printf("Num. [%d]: %d\n", i+1, arr[i]);
}
printf("\n\n");
}
double insertSort(int *arr,int n){
int j,i,key;
double time_spent = 0.0;
clock_t t0 = clock();
for (j = 1; j < n; j++){
i =j;
key = arr[j];
while (i > 0 && arr[i-1] > key){
arr[i] = arr[i-1];
i--;
}
arr[i] = key;
}
clock_t t1 = clock();
time_spent+=(double)(t1-t0) / CLOCKS_PER_SEC;
return time_spent;
}
void showTime(double time_spent){
printf("The computing time it takes to the insertion algorithm is [%f]s\n",time_spent);
}
void showMsg(int n){
char *errors[] = {
"Memory Error allocating the array...",
"The memory has been sucessfully liberated"
};
printf("Msg: [%s]",errors[n]);
}
void freeMemory(int *arr){
free(arr);
showMsg(1);
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
void askUserData(int*);
int **createDinamicArray(int);
void fillArrWRandVar(int**,int);
void showMatrix(int**,int,char);
double sumMatrix(int**,int**,int**,int);
int askShowData();
void showMsg(int);
void showTime(double);
void freeMemory(int**,int);
int main(int argc, char const *argv[])
{
srand(time(NULL));
int n, **A, **B,**C;
askUserData(&n);
A = createDinamicArray(n);
B = createDinamicArray(n);
C = createDinamicArray(n);
fillArrWRandVar(A,n);
fillArrWRandVar(B,n);
double time_spent = sumMatrix(A,B,C,n);
int opcion = askShowData();
if(opcion){
showMatrix(A,n,'A');
showMatrix(B,n,'B');
showMatrix(C,n,'C');
}
showTime(time_spent);
freeMemory(A,n); freeMemory(B,n); freeMemory(C,n);
return 0;
}
void askUserData(int *n){
printf("Insert the size of the matrixes: ");
scanf("%d",n);
}
int **createDinamicArray(int n){
int **array;
array = (int**)malloc(sizeof(int*)*n);
for (int i = 0; i < n; i++){
array[i] = (int*)malloc(sizeof(int)*n);
}
if(array == NULL || *array == NULL){
showMsg(0);
exit(-1);
}
return array;
}
void fillArrWRandVar(int **arr,int n){
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
arr[i][j] = (rand()%199)-99;
}
}
void showMatrix(int **arr, int n, char c){
int i, showN = n;
if(n > 30) showN = 20;
printf("\tMatrix [%c]:\n",c);
for (i = 0; i < showN; i++) {
for (int j = 0; j < showN; j++){
printf("[%d][%d]: %d\t", i+1,j+1, arr[i][j]);
}
printf("\n");
}
printf("\n\n");
}
void showTime(double time_spent){
printf("The computing time it takes to addition of the matrixes is [%f]s\n",time_spent);
}
void showMsg(int n){
char *errors[] = {
"Memory Error allocating the array...",
"Memory sucessfully liberated..."
};
printf("Msg: [%s]\n",errors[n]);
}
double sumMatrix(int **A, int **B, int **C, int n){
double time_spent = 0.0;
clock_t t0 = clock();
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
C[i][j] = A[i][j] + B[i][j];
}
clock_t t1 = clock();
time_spent+=(double)(t1-t0) / CLOCKS_PER_SEC;
return time_spent;
}
void freeMemory(int **mtx, int n){
for (int i = 0; i < n; i++)
free(mtx[i]);
free(mtx);
showMsg(1);
}
int askShowData(){
int opcion = 0;
printf("Show matrixes? [Yes:1 | No:0]: ");
scanf("%d",&opcion);
return opcion;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment