b) -1
c) 3
printf("x = %f, y = %f, z = %f\n", x, y, z);
b) Både p1 och p2 pekar på variabeln y.
double max3(double d1, double d2, double d3) { if (d1 >= d2 && d1 >= d3) return d1; else if (d2 >= d3) return d2; else return d3; }
double arraymax(double a[], int n) { double max = a[0]; for (int i = 1; i < n; ++i) if (a[i] > max) max = a[i]; return max; }
int main(void) { printf("max3(7.0, 7.1, -13.3) = %f\n", max3(7.0, 7.1, -13.3)); double a[] = { 3.9, 2.9, -10.1, 9.1, 9.2, 9.3, 9.2, 9.1, -1000 }; printf("arraymax(a, sizeof(a)/sizeof(a[0])) = %f\n", arraymax(a, sizeof(a)/sizeof(a[0]))); return 0; }
struct Monster { char namn[10 + 1]; double tuffhet; int antal_tander; };
struct Monster monstret = { "Uschlo", 12.5, 300 };
void visa_monster(struct Monster m) { printf("Namn: %s\n", m.namn); printf("Tuffhet: %.2f\n", m.tuffhet); printf("Tänder: %d\n", m.antal_tander); }
void las_monster(struct Monster *mp) { printf("Namn: "); scanf("%s", mp->namn); printf("Tuffhet: "); scanf("%lf", &mp->tuffhet); printf("Tänder: "); scanf("%d", &mp->antal_tander); }
int lever(struct Monster m) { return m.tuffhet >= 0; }
void attackera(struct Monster *aggressorp, struct Monster *offerp) { if (rand() % 2 == 0) offerp->tuffhet -= aggressorp->antal_tander; }
int main(void) { struct Monster monster1, monster2; srand((unsigned int)time((time_t*)NULL)); las_monster(&monster1); las_monster(&monster2); attackera(&monster1, &monster2); attackera(&monster2, &monster1); if (lever(monster1)) visa_monster(monster1); if (lever(monster2)) visa_monster(monster2); return EXIT_SUCCESS; }
#include <stdlib.h> #include <stdio.h> // Definition av "struct Monster" och funktionen "las_monster" int main(void) { FILE *f; int antal_monster; f = fopen("monster.txt", "w"); if (f == NULL) { printf("Kunde inte skriva filen 'monster.txt'.\n"); exit(EXIT_FAILURE); } printf("Hur många monster vill du mata in? "); scanf("%d", &antal_monster); for (int i = 0; i < antal_monster; ++i) { struct Monster m; las_monster(&m); fprintf(f, "%s %f %d\n", m.namn, m.tuffhet, m.antal_tander); } fclose(f); return EXIT_SUCCESS; }
#include <stdlib.h> #include <stdio.h> #include <time.h> #define MAX_ANTAL_MONSTER 1000 // Definition av "struct Monster", "visa_monster", "lever" och "attackera" int main(void) { FILE *f; struct Monster monster[MAX_ANTAL_MONSTER]; int antal_monster = 0; struct Monster m; srand((unsigned int)time((time_t*)NULL)); f = fopen("monster.txt", "r"); if (f == NULL) { printf("Kunde inte läsa filen 'monster.txt'.\n"); exit(EXIT_FAILURE); } while (fscanf(f, "%s %lf %d", m.namn, &m.tuffhet, &m.antal_tander) == 3) { if (antal_monster >= MAX_ANTAL_MONSTER) { printf("Varning: För många monster. Resten ignoreras.\n"); break; } monster[antal_monster++] = m; } fclose(f); while (antal_monster > 1) { int aggressor = rand() % antal_monster; int offer; do offer = rand() % antal_monster; while (offer == aggressor); attackera(&monster[aggressor], &monster[offer]); if (!lever(monster[offer])) { printf("Glufs! %s dödade %s.\n", monster[aggressor].namn, monster[offer].namn); monster[offer] = monster[antal_monster - 1]; --antal_monster; } } visa_monster(monster[0]); return EXIT_SUCCESS; }