-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
139 changed files
with
181,936 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ang sin (ang) cos (ang) | ||
------ ----------- ----------- | ||
20.00 0.34202013 0.93969262 | ||
25.00 0.42261826 0.90630779 | ||
30.00 0.50000001 0.86602540 | ||
35.00 0.57357643 0.81915205 | ||
40.00 0.64278759 0.76604446 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// Tomás Oliveira e Silva, AED, October 2021 | ||
// | ||
// This program contains a flawed binary search implementation. | ||
// Use the gdb program to execute the binary_search() function | ||
// one line at a line and to examine the value of all relevant | ||
// variables to find where it goes wrong. (Do not put calls to | ||
// the printf function to discover what is wrong. In the context | ||
// of the present exercise that would be cheating.) | ||
// | ||
|
||
#include <stdio.h> | ||
|
||
int binary_search(int *a, int n, int d) { | ||
int lo = 0; | ||
int hi = n - 1; | ||
while (hi >= lo) { | ||
int middle = (lo + hi) / 2; | ||
if (a[middle] == d) | ||
return middle; // found it | ||
if (a[middle] < d) | ||
lo = middle + 1; | ||
else | ||
hi = middle - 1; | ||
} | ||
return -1; // not found | ||
} | ||
|
||
int main(void) { | ||
int a[8] = {1, 3, 8, 11, 18, 19, 23, 27}; | ||
int i, d; | ||
|
||
for (d = 0; d <= 30; d++) { | ||
i = binary_search(a, 8, d); | ||
if (i < 0) | ||
printf("%d not found\n", d); | ||
else | ||
printf("the index of %d is %d\n", d, i); | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <ctype.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define INPUT_SIZE 200 | ||
|
||
void lowerAll(char *str) { | ||
for (size_t i = 0; str[i] != '\0'; i++) | ||
str[i] = tolower(str[i]); | ||
} | ||
|
||
int readLine(char *str, const size_t size) { | ||
if (fgets(str, size, stdin) == NULL) | ||
return -1; | ||
|
||
str[strcspn(str, "\n")] = '\0'; | ||
|
||
return 0; | ||
} | ||
|
||
int main(void) { | ||
char str1[INPUT_SIZE]; | ||
char str2[INPUT_SIZE * 2]; | ||
|
||
printf("Insert first string: "); | ||
if (readLine(str1, INPUT_SIZE) != 0) | ||
return 1; | ||
|
||
printf("Insert second string: "); | ||
if (readLine(str2, INPUT_SIZE) != 0) | ||
return 1; | ||
|
||
unsigned int numAlpha = 0; | ||
for (int i = 0; str1[i] != '\0'; i++) { | ||
if (isalpha(str1[i])) | ||
numAlpha++; | ||
} | ||
printf("Caracteres do alfabeto = %d\n", numAlpha); | ||
|
||
unsigned int numUpperCase = 0; | ||
for (int i = 0; str2[i] != '\0'; i++) { | ||
if (isupper(str2[i])) | ||
numUpperCase++; | ||
} | ||
printf("Caracteres maisculos = %d\n", numUpperCase); | ||
|
||
lowerAll(str1); | ||
printf("First string minusculas = %s\n", str1); | ||
lowerAll(str2); | ||
printf("Second string minusculas = %s\n", str2); | ||
|
||
int cmpRes = strcmp(str1, str2); | ||
|
||
if (cmpRes == 0) { | ||
printf("As strings são iguais\n"); | ||
} else if (cmpRes > 0) { | ||
puts(str2); | ||
puts(str1); | ||
} else { | ||
puts(str1); | ||
puts(str2); | ||
} | ||
|
||
char str3[INPUT_SIZE]; | ||
|
||
strcpy(str3, str2); | ||
printf("Cópia da segunda string: %s\n", str3); | ||
|
||
strcat(str2, str3); | ||
printf("Strings concatenadas: %s\n", str2); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <ctype.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
void Permute(int *a, int *b, int *c) { | ||
const int temp = *c; | ||
*c = *b; | ||
*b = *a; | ||
*a = temp; | ||
} | ||
|
||
int main(void) { | ||
int a = 1, b = 2, c = 3; | ||
|
||
printf("Start:\n"); | ||
printf("a: %d\n", a); | ||
printf("b: %d\n", b); | ||
printf("c: %d\n", c); | ||
|
||
for (int i = 0; i < 5; i++) { | ||
printf("Iteration %d\n", i); | ||
|
||
Permute(&a, &b, &c); | ||
|
||
printf("a: %d\n", a); | ||
printf("b: %d\n", b); | ||
printf("c: %d\n", c); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
// Display the contents of array a with n elements | ||
// Pre-Conditions: a != NULL and n > 0 | ||
// Example of produced output: Array = [ 1.00, 2.00, 3.00 ] | ||
void DisplayArray(double *a, size_t n) { | ||
printf("Array = ["); | ||
|
||
for (size_t i = 0; i < n; i++) | ||
printf(" %.2lf", a[i]); | ||
|
||
printf(" ]\n"); | ||
} | ||
|
||
// Read the number of elements, allocate the array and read its elements | ||
// Condition: number of elements > 0 | ||
// Pre-Condition: size_p != NULL | ||
// Return NULL if memory allocation fails | ||
// Set *size_p to ZERO if memory allocation fails | ||
double *ReadArray(size_t *size_p) { | ||
printf("Número de elementos: "); | ||
|
||
if (scanf(" %lu", size_p) != 1) { | ||
*size_p = 0; | ||
return NULL; | ||
} | ||
|
||
double *result = malloc(*size_p * sizeof(double)); | ||
|
||
if (result == NULL) { | ||
*size_p = 0; | ||
return NULL; | ||
} | ||
|
||
size_t read = 0; | ||
while (read != *size_p) { | ||
printf("%lu: ", read); | ||
|
||
if (scanf(" %lf", &result[read]) != 1) | ||
printf("Please insert again\n"); | ||
else | ||
read++; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// Allocate and return a new array with (size_1 + size_2) elements | ||
// which stores the elements of array_1 followed by the elements of array_2 | ||
// Pre-Conditions: array_1 != NULL and array_2 != NULL | ||
// Pre-Conditions: size_1 > 0 and size_2 > 0 | ||
// Return NULL if memory allocation fails | ||
double *Append(double *array_1, size_t size_1, double *array_2, size_t size_2) { | ||
double *result = malloc(sizeof(double) * (size_1 + size_2)); | ||
|
||
if (result == NULL) | ||
return NULL; | ||
|
||
memcpy(result, array_1, size_1 * sizeof(double)); | ||
memcpy(result + size_1, array_2, size_2 * sizeof(double)); | ||
|
||
return result; | ||
} | ||
|
||
// Test example: | ||
// | ||
// | ||
// Array = [ 1.00, 2.00, 3.00 ] | ||
// Array = [ 4.00, 5.00, 6.00, 7.00 ] | ||
// Array = [ 1.00, 2.00, 3.00, 4.00, 5.00, 6.00, 7.00 ] | ||
// | ||
// | ||
|
||
int main(void) { | ||
size_t array_size = 0; | ||
double *array = ReadArray(&array_size); | ||
|
||
if (array == NULL) | ||
return 1; | ||
|
||
DisplayArray(array, array_size); | ||
|
||
size_t array2_size = 0; | ||
double *array2 = ReadArray(&array2_size); | ||
|
||
if (array2 == NULL) | ||
return 1; | ||
|
||
double *append = Append(array, array_size, array2, array2_size); | ||
|
||
if (append == NULL) | ||
return 1; | ||
|
||
DisplayArray(append, array_size + array2_size); | ||
|
||
free(append); | ||
free(array2); | ||
free(array); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include <math.h> | ||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
// The coefficients of a degree n polynomial | ||
// are stored in an array p of size (n + 1) | ||
// p[0] is the coefficient of the largest degree term | ||
// p[n] is the coefficient of the zero-degree term | ||
// Display a polynomial | ||
// Pre-Conditions: coef != NULL and degree >= 0 | ||
// Example of produced output: | ||
// Pol(x) = 1.000000 * x^2 + 4.000000 * x^1 + 1.000000 | ||
void DisplayPol(double *coef, size_t degree) { | ||
printf("Pol(x) = "); | ||
|
||
for (size_t i = 0; i < degree + 1; i++) { | ||
if (i != 0) | ||
printf(" + "); | ||
|
||
printf("%lf", coef[i]); | ||
|
||
if (i != degree) | ||
printf(" * x^%lu", degree - i); | ||
} | ||
|
||
putchar('\n'); | ||
} | ||
|
||
// Compute the value of a polynomial using Horner’s method: | ||
// Pre-Conditions: coef != NULL and degree >= 0 | ||
double ComputePol(double *coef, size_t degree, double x) { | ||
double a = coef[0]; | ||
|
||
for (size_t i = 1; i < degree + 1; i++) { | ||
a = coef[i] + x * a; | ||
} | ||
|
||
return a; | ||
} | ||
|
||
// Test example: | ||
// Pol(x) = 1.000000 * x^2 + 4.000000 * x^1 + 1.000000 | ||
// Pol(2.000000) = 13.000000 | ||
// Compute the real roots, if any, of a second-degree polynomial | ||
// Pre-Conditions: coef != NULL and degree == 2 and coef[0] != 0 | ||
// Pre-Conditions: root_1 != NULL and root_2 != NULL | ||
// Return values: 0 -> no real roots | ||
// 1 -> 1 real root with multiplicity 2 | ||
// 2 -> 2 distinct real roots | ||
// The computed root values are returned via the root_1 and root_2 | ||
// pointer arguments | ||
// Assign 0.0 to the *root_1 and *root_2 if there are no real roots | ||
unsigned int GetRealRoots(double *coef, size_t degree, double *root_1, | ||
double *root_2) { | ||
if (degree != 2) { | ||
*root_1 = 0.0; | ||
*root_2 = 0.0; | ||
return 0; | ||
} | ||
|
||
const double a = coef[0], b = coef[1], c = coef[2]; | ||
const double inner_root = b * b - 4 * a * c; | ||
|
||
if (inner_root < 0.0) { | ||
*root_1 = 0.0; | ||
*root_2 = 0.0; | ||
return 0; | ||
} | ||
|
||
*root_1 = (-b + sqrt(inner_root)) / (2 * a); | ||
*root_2 = (-b - sqrt(inner_root)) / (2 * a); | ||
|
||
if (*root_1 == *root_2) | ||
return 1; | ||
|
||
return 2; | ||
} | ||
|
||
int main(void) { | ||
double coefs[] = {2, 1, -6}; | ||
const size_t degree = (sizeof(coefs) / sizeof(double)) - 1; | ||
|
||
DisplayPol(coefs, degree); | ||
|
||
const double res = ComputePol(coefs, degree, 1.0); | ||
|
||
printf("P(1) = %lf\n", res); | ||
|
||
double root_1 = 0.0, root_2 = 0.0; | ||
|
||
const unsigned int roots = GetRealRoots(coefs, degree, &root_1, &root_2); | ||
|
||
printf("Roots: %u\n", roots); | ||
printf("First root: %lf\n", root_1); | ||
printf("Second root: %lf\n", root_2); | ||
} |
Oops, something went wrong.