Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • 20041679/prog-1-lab-assigments
1 result
Show changes
Commits on Source (6)
Showing
with 586 additions and 0 deletions
.nfs*
a.out
#include <stdio.h>
void minmax(int array[], int len, int *min, int *max) {
int i = 0;
*min = array[i];
*max = array[i];
for (i = 1; i < len; i++) {
if (array[i] < *min) { *min = array[i]; }
if (array[i] > *max) { *max = array[i]; }
}
}
#include <stdio.h>
int max(int array[], int len, int *n_max_occurr) {
int max;
*n_max_occurr = 0;
int i = 0;
max = array[i];
(*n_max_occurr)++;
for (i = 1; i < len; i++) {
if (array[i] == max) { (*n_max_occurr)++; }
else if (array[i] > max) {
max = array[i];
(*n_max_occurr) = 1;
}
}
return max;
}
#include <stdio.h>
int maxcount(int array[], int len) {
int i = 0;
int max_i = i;
for (i = 1; i < len; i++) {
if (array[i] > array[max_i]) { max_i = i; }
}
int ret = array[max_i];
array[max_i] = -1;
return ret;
}
#include <stdio.h>
void input_int_array(int arr[], size_t len) {
for (size_t i = 0; i < len; ++i) {
scanf("%d", arr[i]);
}
}
#include <stdio.h>
void print_int_array(int arr[], size_t len) {
for (size_t i = 0; i < len; ++i) {
printf("%d", arr[i]);
if (i < len - 1) { printf(", "); }
}
puts("");
}
#include <assert.h>
void add_int_array(int A[], int dimA, int B[], int dimB, int C[], int dimC) {
assert(dimA == dimB);
assert(dimB == dimC);
for (int i = 0; i < dimA; ++i) {
C[i] = A[i] + B[i];
}
}
int sum_int_array(int arr[], int len) {
int ret = 0;
for (int i = 0; i < len; i++) {
ret += arr[i];
}
return ret;
}
int f(int A[], int dimA, int B[], int dimB) {
int sumi = 0;
for (int i = 0; i < dimA; ++i) { sumi += A[i] * sum_int_array(B, dimB); }
return sumi;
}
#include <stdio.h>
#define MARK -1
#define LEN(x) (sizeof(x) / sizeof(*x))
void copy(int to[], int from[], int n, int *ret) {
int i = 0;
while (from[i] != MARK && i < n - 1) {
to[i] = from[i];
++i;
}
to[i] = MARK;
*ret = from[i] == MARK ? 1 : -1;
}
void cmp(int a1[], int a2[], int *ret) {
int i = 0;
while (a1[i] != MARK && a2[i] != MARK && a1[i] == a2[i]) { ++i; }
*ret = a1[i] == a2[i] ? 1 : -1;
}
int count_greater(int a1[], int a2[]) {
int i = 0;
int ret = 0;
while (a1[i] != MARK && a2[i] != MARK) {
if (a1[i] > a2[i]) { ++ret; }
++i;
}
while (a2[i] != MARK) {
++ret;
++i;
}
return ret;
}
int copy_even_positions(int to[], int from[], int n) {
int i = 0;
while (from[i * 2] && from[(i * 2) + 1] != MARK && i < n - 1) {
to[i] = from[i * 2];
++i;
}
to[i] = MARK;
return i;
}
int sum(int a1[], int a2[], int n, int sum[]) {
int i = 0;
while (a1[i] != MARK && a2[i] != MARK && i < n - 1) {
sum[i] = a1[i] + a2[i];
++i;
}
while (a1[i] != MARK && i < n - 1) {
sum[i] = a1[i];
++i;
}
while (a2[i] != MARK && i < n - 1) {
sum[i] = a2[i];
++i;
}
sum[i] = MARK;
return a1[i] == MARK || a2[i] == MARK ? 1 : -1;
}
#include <stdio.h>
#define ROWS(a) (sizeof(a) / sizeof(a[0]))
#define COLS(a) (sizeof(a[0]) / sizeof(a[0][0]))
void input(int rows, int cols, int matrix[][cols]) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("[%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}
int count_eq(int rows, int cols, int matrix[][cols], int key) {
int ret = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (matrix[i][j] == key) {
++ret;
}
}
}
return ret;
}
int sum(
int rows1, int cols1, int a1[][cols1],
int rows2, int cols2, int a2[][cols2],
int rows3, int cols3, int a3[][cols3])
{
int ret;
if (rows1 == rows2 && rows2 == rows3 && cols1 == cols2 && cols2 == cols3) {
int rows = rows1;
int cols = cols1;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
a3[i][j] = a1[i][j] + a2[i][j];
}
}
ret = 0;
} else {
ret = -1;
}
return ret;
}
void print(int rows, int cols, int matrix[][cols]) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%3d ", matrix[i][j]);
}
puts("");
}
}
int multiply(
int rows1, int cols1, int a1[][cols1],
int rows2, int cols2, int a2[][cols2],
int rows3, int cols3, int a3[][cols3])
{
int ret;
if (cols1 == rows2 && rows1 == rows3 && cols2 == cols3) {
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
int sum = 0;
for (int k = 0; k < rows2; ++k) {
sum += a1[i][k] * a2[k][j];
}
a3[i][j] = sum;
}
}
ret = 1;
} else {
ret = -1;
}
return ret;
}
int main(void) {
int a1[2][3];
int a2[3][2];
puts("inserimento A");
input(ROWS(a1), COLS(a1), a1);
puts("inserimento B");
input(ROWS(a2), COLS(a2), a2);
int x;
printf("inserire x: ");
scanf("%d", &x);
printf("instanze di %d in A: %d\n", x, count_eq(ROWS(a1), COLS(a1), a1, x));
int y;
printf("inserire y: ");
scanf("%d", &y);
printf("instanze di %d in B: %d\n", y, count_eq(ROWS(a2), COLS(a2), a2, y));
int s[ROWS(a1)][COLS(a1)];
if (sum(ROWS(a1), COLS(a1), a1, ROWS(a2), COLS(a2), a2, ROWS(s), COLS(s), s) < 0) {
fputs("somma impossibile\n", stderr);
} else {
print(ROWS(s), COLS(s), s);
}
int m[2][2];
if (multiply(ROWS(a1), COLS(a1), a1, ROWS(a2), COLS(a2), a2, ROWS(m), COLS(m), m) < 0) {
fputs("prodotto impossibile\n", stderr);
} else {
print(ROWS(m), COLS(m), m);
}
}
#include <stdio.h>
#include <string.h>
#define BUF_SIZE 4096
#define ARR_SIZE(a) (sizeof(a) / sizeof(*a))
void str_strip_trailing(char *s) {
char *pos = strchr(s, '\n');
if (pos != NULL) { *pos = '\0'; }
}
int main(void) {
char str1[BUF_SIZE];
char str2[BUF_SIZE];
fgets(str1, ARR_SIZE(str1), stdin);
str_strip_trailing(str1);
fgets(str2, ARR_SIZE(str2), stdin);
str_strip_trailing(str2);
if (strcmp(str1, str2) == 0) {
puts("Le due stringhe sono uguali");
} else if (strstr(str1, str2) != NULL) {
puts("La seconda stringa e' contenuta nella prima");
} else if (strstr(str2, str1) != NULL) {
puts("La prima stringa e' contenuta nella seconda");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#define BUF_SIZE 4096
#define ARR_SIZE(a) (sizeof(a) / sizeof(*a))
const char vowels[] = "aeiouAEIOU";
void str_strip_trailing(char *s) {
char *pos = strchr(s, '\n');
if (pos != NULL) { *pos = '\0'; }
}
void change_forbidden_chars(char *s, const char *remove, char new) {
char *match = s;
while ((match = strpbrk(match, remove)) != NULL) {
*match = new;
}
}
int main(void) {
char str1[BUF_SIZE];
char str2[BUF_SIZE];
char str12[BUF_SIZE * 2 - 1];
fgets(str1, ARR_SIZE(str1), stdin);
str_strip_trailing(str1);
fgets(str2, ARR_SIZE(str2), stdin);
str_strip_trailing(str2);
strcpy(str12, str1);
strcat(str12, str2);
change_forbidden_chars(str12, vowels, '*');
puts(str12);
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define BUF_SIZE 100
#define ARR_SIZE(a) (sizeof(a) / sizeof(*a))
const char vowels[] = {'a', 'e', 'i', 'o', 'u' };
void str_strip_trailing(char *s) {
char *pos = strchr(s, '\n');
if (pos != NULL) { *pos = '\0'; }
}
void count_vowels(char *s, unsigned int counts[]) {
while (*s) {
int match = 0;
size_t i;
for (i = 0; i < ARR_SIZE(vowels) && !match; ++i) {
if (vowels[i] == tolower(*s)) { match = 1; }
}
if (match) { ++counts[i - 1]; }
++s;
}
}
unsigned int arr_sum(unsigned int arr[], size_t len) {
unsigned int ret = 0;
for (size_t i = 0; i < len; ++i) { ret += arr[i]; }
return ret;
}
int main(void) {
char str[BUF_SIZE];
fgets(str, ARR_SIZE(str), stdin);
str_strip_trailing(str);
unsigned int vowels_counters[5] = {0};
count_vowels(str, vowels_counters);
unsigned int n_vowels = arr_sum(vowels_counters, ARR_SIZE(vowels_counters));
size_t len = strlen(str);
unsigned int n_consonants = len - n_vowels;
printf("vocali: %u, consonanti: %u\n", n_vowels, n_consonants);
puts("frequenze vocali:");
for (size_t i = 0; i < 5; ++i) {
printf("%c: %lf\n", vowels[i], ((double)vowels_counters[i] / len) * 100);
}
}
File added
#include <stdio.h>
#include <assert.h>
#define MARKER -1
#define ARR_SIZE(a) (sizeof(a) / sizeof(*a))
int populate(int arr[], size_t size) {
int insert;
size_t i = 0;
while (i < size && ((scanf("%d", &insert)), insert != MARKER)) {
arr[i] = insert;
++i;
}
int ret;
if (i < size) {
arr[i] = MARKER;
ret = 1;
}
else { ret = -1; }
return ret;
}
void print(int arr[]) {
size_t i = 0;
if (arr[i] != MARKER) {
while (arr[i + 1] != MARKER) {
printf("%d*", arr[i]);
++i;
}
printf("%d", arr[i]);
}
}
int *find(int where[], int what) {
size_t i = 0;
int found = 0;
while (where[i] != MARKER && !found) {
if (where[i] == what) { found = 1; }
else { ++i; }
}
int *ret;
if (found) { ret = &where[i]; }
else { ret = NULL; }
return ret;
}
void remove_first(int where[], int what) {
assert(what != MARKER);
int *pos = find(where, what);
if (pos != NULL) {
while ((*pos) != MARKER) {
*pos = *(pos + 1);
++pos;
}
}
}
void remove_all(int where[], int what) {
size_t shift_width = 0;
size_t i = 0;
while (where[i + shift_width] != MARKER) {
if (where[i] == what) {
++shift_width;
} else {
++i;
}
where[i] = where[i + shift_width];
}
where[i] = MARKER;
}
int main(void) {
int array[10];
if (populate(array, ARR_SIZE(array))) {
remove_all(array, 5);
print(array);
}
}
1
2
3
4
5
6
7
8
9
-1
1
2
3
3
3
9
8
7
6
-1
#include <stdio.h>
#define ARR_SIZE(arr) (sizeof(arr) / sizeof(*arr))
#define MARK -1
int write_to_file(char *filename, int arr[]) {
int ret = 0;
FILE *file;
if ((file = fopen(filename, "w")) == NULL) {
ret = -1;
} else {
int i = 0;
while (arr[i] != MARK && arr[i + 1] != MARK) {
fprintf(file, "%d-", arr[i]);
++i;
}
fprintf(file, "%d", arr[i]);
fclose(file);
}
return ret;
}
int read_from_file(char *filename, int arr[], int size) {
int ret;
FILE *file;
if ((file = fopen(filename, "r")) == NULL) {
ret = -1;
} else {
int i = 0;
int end = 0;
int buf;
while (i < size && !end) {
fscanf(file, "%d", &buf);
if (buf == MARK) {
end = 1;
} else {
arr[i] = buf;
++i;
}
}
if (end) {
arr[i] = MARK;
ret = i;
} else {
arr[0] = MARK;
ret = -1;
}
fclose(file);
}
return ret;
}
void modify(int arr1[], int arr2[]) {
int i = 0;
while (arr1[i] != MARK && arr2[i] != MARK) {
int a = arr1[i];
int b = arr2[i];
if (a == b) {
arr1[i] = 0;
} else if (a < b) {
arr1[i] = b;
} else {
++arr1[i];
}
++i;
}
}
int main(void) {
int ret = 0;
int arr1[10];
if (read_from_file("in1.txt", arr1, ARR_SIZE(arr1)) < 0) {
ret = 1;
} else {
int arr2[10];
if (read_from_file("in2.txt", arr2, ARR_SIZE(arr2)) < 0) {
ret = 1;
} else {
modify(arr2, arr1);
write_to_file("out.txt", arr2);
}
}
return ret;
}
0-0-0-4-5-10-9-8-9
\ No newline at end of file
aaaaaa