Last active
October 31, 2017 10:00
-
-
Save aziis98/42f5ea6894c6459bc10b1076c7a4f615 to your computer and use it in GitHub Desktop.
Soluzioni degli esercizi della Bodei del 31 Ottobre 2017
This file contains hidden or 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
| #include <stdio.h> | |
| #define SIZE 10 | |
| int sameSign(float value, float target) { | |
| return (target > 0 && value > 0) || (target < 0 && value < 0); | |
| } | |
| float media(int vet[], int size) { | |
| float acc = 0.0; | |
| int n = 0; | |
| int last = vet[size - 1]; | |
| for (int i = 0; i < size; i++) { | |
| if (sameSign(vet[i], last)) { | |
| n++; | |
| acc += vet[i]; | |
| } | |
| } | |
| return acc / n; | |
| } | |
| int main() { | |
| int vet[SIZE]; | |
| for (int i = 0; i < SIZE; i++) { | |
| scanf("%d", &vet[i]); | |
| } | |
| printf("%.2f", media(vet, SIZE)); | |
| } |
This file contains hidden or 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
| #include <stdio.h> | |
| int mcd(int a, int b) { | |
| if (a == 0) | |
| return b; | |
| if (b == 0) | |
| return a; | |
| else | |
| return mcd(b, a % b); | |
| } | |
| int mcm(int a, int b) { | |
| return a * b / mcd(a, b); | |
| } | |
| int main() { | |
| int n, m; | |
| scanf("%d %d", &n, &m); | |
| printf("%d\n%d\n", mcd(n, m), mcm(n, m)); | |
| } |
This file contains hidden or 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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| float aprox_sqrt(float z) { | |
| if (z <= 0.0) { | |
| return 1.0; | |
| } | |
| else { | |
| float rec = aprox_sqrt(z - 1.0); | |
| return 0.5 * (rec + z / rec); | |
| } | |
| } | |
| int main() { | |
| int n; | |
| scanf("%d", &n); | |
| printf("%.4f", aprox_sqrt(n)); | |
| } |
This file contains hidden or 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
| #include <stdio.h> | |
| void multipli(int n, int m) { | |
| for (int i = m; i <= n; i += m) { | |
| printf("%d\n", i); | |
| } | |
| } | |
| int main() { | |
| int n, m; | |
| scanf("%d %d", &n, &m); | |
| multipli(n, m); | |
| } |
This file contains hidden or 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
| #include <stdio.h> | |
| #include <math.h> | |
| float sum_pow(int n, float x) { | |
| float acc = 0.0; | |
| for (int i = 0; i <= n; i++) { | |
| acc += pow(x, i); | |
| } | |
| return acc; | |
| } | |
| int main() { | |
| int n; | |
| float x; | |
| scanf("%d %f", &n, &x); | |
| printf("%.2f", sum_pow(n, x)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment