parent
232faedde7
commit
2d3bbd58ef
Binary file not shown.
|
@ -0,0 +1,56 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void display_array(double* a, size_t n) {
|
||||
printf("[");
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
printf("%f", a[i]);
|
||||
if (i < n - 1) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
double* read_array(size_t n) {
|
||||
double* a = malloc(n * sizeof(double));
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
scanf("%lf", &a[i]);
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
double* append_arrays(double* a, size_t n, double* b, size_t m) {
|
||||
double* c = malloc((n + m) * sizeof(double));
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
c[i] = a[i];
|
||||
}
|
||||
for (size_t i = 0; i < m; i++) {
|
||||
c[n + i] = b[i];
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
size_t n, m;
|
||||
printf("A size: ");
|
||||
scanf("%zu", &n);
|
||||
printf("A elements:\n");
|
||||
double* a = read_array(n);
|
||||
printf("B size: ");
|
||||
scanf("%zu", &m);
|
||||
printf("B elements:\n");
|
||||
double* b = read_array(m);
|
||||
printf("A = ");
|
||||
display_array(a, n);
|
||||
printf("B = ");
|
||||
display_array(b, m);
|
||||
double* c = append_arrays(a, n, b, m);
|
||||
printf("C = ");
|
||||
display_array(c, n + m);
|
||||
free(a);
|
||||
free(b);
|
||||
free(c);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
//
|
||||
// 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 - lo) / 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;
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
//
|
||||
// Tomás Oliveira e Silva, AED, October 2021
|
||||
//
|
||||
// explain the program output
|
||||
//
|
||||
// try also compiling the program with the -Wsign-compare compilation flag
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned int i = 1;
|
||||
int j = -1;
|
||||
int k = -2147483648;
|
||||
|
||||
printf("original i = %u\n",i);
|
||||
printf("original j = %d\n",j);
|
||||
printf("original k = %d\n",k);
|
||||
// compare i with j
|
||||
if((int)i > (int)j)
|
||||
printf("i > j is true\n");
|
||||
else
|
||||
printf("i > j is false\n");
|
||||
|
||||
// replace k by its absolute value and print the result
|
||||
if(k > 0) {
|
||||
k = -k;
|
||||
}
|
||||
printf("absolute value of k = %d\n",k);
|
||||
printf("min integer:%d\nmax integer:%d\n", INT_MIN, INT_MAX);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#include <stdio.h>
|
||||
|
||||
void permute(int* a, int* b, int* c) {
|
||||
int temp = *a;
|
||||
*a = *b;
|
||||
*b = *c;
|
||||
*c = temp;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a = 1, b = 2, c = 3;
|
||||
printf("Before: a = %d, b = %d, c = %d\n", a, b, c);
|
||||
permute(&a, &b, &c);
|
||||
printf("After: a = %d, b = %d, c = %d\n", a, b, c);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
|
||||
void display_pol(double* coef, size_t degree) {
|
||||
if (coef == NULL) {
|
||||
printf("NULL\n");
|
||||
return;
|
||||
}
|
||||
for (size_t i = 0; i <= degree; i++) {
|
||||
printf("%lf", coef[i]);
|
||||
if (i < degree) {
|
||||
printf(" * x^%lu + ", degree - i);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
double compute_pol(double* coef, size_t degree, double x) {
|
||||
if (coef == NULL) {
|
||||
return 0;
|
||||
}
|
||||
double result = 0;
|
||||
for (size_t i = 0; i <= degree; i++) {
|
||||
result += coef[i] * pow(x, degree - i);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned int get_real_roots(double* coef, size_t degree, double* root_1, double* root_2) {
|
||||
if (coef == NULL || root_1 == NULL || root_2 == NULL || degree != 2 || coef[0] == 0) {
|
||||
return 0;
|
||||
}
|
||||
double delta = pow(coef[1], 2) - 4 * coef[0] * coef[2];
|
||||
if (delta < 0) {
|
||||
*root_1 = 0;
|
||||
*root_2 = 0;
|
||||
return 0;
|
||||
}
|
||||
*root_1 = (-coef[1] - sqrt(delta)) / (2 * coef[0]);
|
||||
*root_2 = (-coef[1] + sqrt(delta)) / (2 * coef[0]);
|
||||
if (delta == 0) {
|
||||
return 1;
|
||||
}
|
||||
return 2;
|
||||
}
|
||||
|
||||
int main() {
|
||||
double coef[] = {1, 2, 1};
|
||||
size_t degree = 2;
|
||||
double x = 1;
|
||||
double root_1 = 0;
|
||||
double root_2 = 0;
|
||||
printf("Polynomial: ");
|
||||
display_pol(coef, degree);
|
||||
printf("f(%lf) = %lf\n", x, compute_pol(coef, degree, x));
|
||||
printf("Number of real roots: %u\n", get_real_roots(coef, degree, &root_1, &root_2));
|
||||
printf("Roots: %lf, %lf\n", root_1, root_2);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
//
|
||||
// Tomás Oliveira e Silva, AED, October 2021
|
||||
//
|
||||
// This program implements a simple sieve of Eratosthenes.
|
||||
// It does not work as intended. Use the valgrind and
|
||||
// gdb programs to find the programming errors. Correct them.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
const int limit = 1000000;
|
||||
int *sieve = (int *)malloc((size_t)limit);
|
||||
if(sieve == NULL)
|
||||
return 1;
|
||||
for(int i = 0;i <= limit;i++)
|
||||
sieve[i] = 0;
|
||||
sieve[0] = sieve[1] = 1; // 0 and 1 are not prime
|
||||
for(int p = 2;p * p <= limit;p++)
|
||||
if(sieve[p] == 0)
|
||||
for(int i = p * p;i <= limit;i += p)
|
||||
sieve[i] = 1; // i is not prime
|
||||
int c = 0;
|
||||
for(int i = 0;i <= limit;i++)
|
||||
if(sieve[i] == 0)
|
||||
c++;
|
||||
printf("There are %d prime numbers up to %d\n",c,limit);
|
||||
free(sieve);
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
|
||||
int main() {
|
||||
char string1[50], string2[50];
|
||||
printf("String 1: ");
|
||||
scanf("%s", string1);
|
||||
printf("String 2: ");
|
||||
scanf("%s", string2);
|
||||
|
||||
// Count how many letters in string 1
|
||||
int count = 0;
|
||||
for (int i = 0; i<(int)(sizeof(string1)/sizeof(*string1)); i++) {
|
||||
if(isalpha(string1[i])) count++;
|
||||
}
|
||||
printf("Letters in string 1: %d\n", count);
|
||||
|
||||
// Count how many uppercase letters in string 2
|
||||
count = 0;
|
||||
for (int i = 0; i<(int)(sizeof(string2)/sizeof(*string2)); i++) {
|
||||
if(isupper(string2[i])) count++;
|
||||
}
|
||||
printf("Uppercase in string 2: %d\n", count);
|
||||
|
||||
|
||||
// Convert both strings to lowercase
|
||||
for (int i = 0; i<(int)(sizeof(string1)/sizeof(*string1)); i++) {
|
||||
if(isupper(string1[i])) string1[i] = tolower(string1[i]);
|
||||
}
|
||||
for (int i = 0; i<(int)(sizeof(string2)/sizeof(*string2)); i++) {
|
||||
if(isupper(string2[i])) string2[i] = tolower(string2[i]);
|
||||
}
|
||||
printf("String 1 to lower: %s\nString 2 to lower: %s\n", string1, string2);
|
||||
|
||||
if (strcmp(string1, string2) < 0) {
|
||||
printf("Strings are different\nOrdered alphabetically: %s, %s", string1, string2);
|
||||
} else if (strcmp(string1, string2) > 0) {
|
||||
printf("Strings are different\nOrdered alphabetically: %s, %s", string2, string1);
|
||||
} else {
|
||||
printf("Strings are equal");
|
||||
}
|
||||
|
||||
// create a copy of string2 with name string3
|
||||
char string3[50];
|
||||
strcpy(string3, string2);
|
||||
|
||||
// concatenate string2 and string3
|
||||
strcat(string2, string3);
|
||||
printf("\nConcatenated string2 and string3: %s", string2);
|
||||
|
||||
return 0;
|
||||
}
|
Binary file not shown.
Loading…
Reference in New Issue