Tudnátok segíteni C programozásban? Eddig ennyire jutottam. Hogyan tudnám ezt javítani?
Feladat: Egy vitorlás hajó által megtett útról egy olyan adathalmaz keletkezett, amelyben minden bejegyzés egy pozícióból és egy időtartamból áll. A pozíció egy kétdimenziós pont, melyen a hajó áthaladt, az időtartam pedig az az idő, ami alatt az előző pozíciótól az aktuális pozícióig elért. A pozíciók között (megfelelő közelítéssel) a hajó egyenesen haladt. A pontok euklideszi térben értelmezettek (laposföldön...), a koordináták méterben vannak megadva (azonban nem feltétlenül egész számok). Az időtartam óra, perc és másodperc tagból áll.
Hozd létre a bejegyzések tárolásához szükséges struktúrákat az alábbi azonosítókkal:
point
x (valós)
y (valós)
time
hour (egész)
minute (egész)
second (egész)
record
position (point)
interval (time)
Írj függvényt in_seconds névvel, mely egy time típussal megadott időintervallumot átvált az eltelt másodpercek számára!
Írd meg a distance nevű függvényt, mely két point típusú pont közötti távolsággal tér vissza!
Írj függvényt average_speed névvel, mely record típusú elemekből álló tömböt kap paraméterként, és visszatér az utazás átlagsebességével m/s mértékegységben (az átlagsebesség a szakaszok összhosszának és az összes eltelt időnek a hányadosa). A tömb első elemének interval tagja irreleváns, hiszen az első pont elérése előtti eseményekre nem vagyunk kíváncsiak.
Kódom:
#include <stdio.h>
struct point {
double x;
double y;
};
struct time {
int hour;
int minute;
int second;
};
struct record {
struct point position;
struct time interval;
};
int in_seconds(struct time t) {
return t.hour * 3600 + t.minute * 60 + t.second;
}
double distance(struct point p1, struct point p2) {
double dx = p2.x - p1.x;
double dy = p2.y - p1.y;
return sqrt(dx * dx + dy * dy);
}
double average_speed(struct record records[], int num_records) {
double total_distance = 0.0;
int total_time_seconds = 0;
for (int i = 1; i < num_records; i++) {
total_distance += distance(records[i - 1].position, records[i].position);
total_time_seconds += in_seconds(records[i].interval);
}
if (total_time_seconds == 0) {
return 0.0;
} else {
return total_distance / total_time_seconds;
}
}
int main() {
struct record records[] = {
{{0.0, 0.0}, {1, 0, 0}},
{{3.0, 4.0}, {0, 30, 0}},
{{6.0, 8.0}, {1, 0, 0}},
{{10.0, 10.0}, {0, 45, 0}}
};
int num_records = sizeof(records) / sizeof(records[0]);
double avg_speed = average_speed(records, num_records);
printf("Az átlagsebesség: %.2f m/s\n", avg_speed);
return 0;
}
fügvény:#include <math.h>
// Függvény az időintervallum másodpercekre átváltásához
int in_seconds(time t) {
return t.hour * 3600 + t.minute * 60 + t.second;
}
// Függvény a két pont közötti távolság kiszámításához
double distance(point p1, point p2) {
return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
}
// Függvény az utazás átlagsebességének kiszámításához
double average_speed(record records[], int size) {
double total_distance = 0.0;
int total_time_seconds = 0;
for (int i = 1; i < size; i++) {
total_distance += distance(records[i].position, records[i - 1].position);
total_time_seconds += in_seconds(records[i].interval);
}
if (total_time_seconds == 0) {
// Ne osszunk nullával
return 0.0;
}
return total_distance / total_time_seconds;
}
hiba: Compiler error
source.c:5:16: error: unknown type name ‘time’; did you mean ‘time_t’?
source.c:10:17: error: unknown type name ‘point’
source.c:10:27: error: unknown type name ‘point’
source.c:15:22: error: unknown type name ‘record’
environment.c:2:25: error: unknown type name ‘time’; did you mean ‘time_t’?
environment.c:7:26: error: unknown type name ‘point’
environment.c:7:35: error: unknown type name ‘point’
environment.c:14:31: error: unknown type name ‘record’
environment.c: In function ‘main’:
environment.c:50:17: error: unknown type name ‘time’; did you mean ‘time_t’?
environment.c:51:18: error: request for member ‘hour’ in something not a structure or union
environment.c:52:18: error: request for member ‘minute’ in something not a structure or union
environment.c:53:18: error: request for member ‘second’ in something not a structure or union
environment.c:54:230: error: implicit declaration of function ‘in_seconds’ [-Wimplicit-function-declaration]
environment.c:54:381: error: implicit declaration of function ‘_correct_in_seconds’ [-Wimplicit-function-declaration]
environment.c:65:9: error: unknown type name ‘point’
environment.c:66:13: warning: braces around scalar initializer
environment.c:66:13: note: (near initialization for ‘test_cases[0][0]’)
environment.c:66:18: error: excess elements in scalar initializer
environment.c:66:18: note: (near initialization for ‘test_cases[0][0]’)
environment.c:66:13: warning: braces around scalar initializer
environment.c:66:13: note: (near initialization for ‘test_cases[0][1]’)
environment.c:66:28: error: excess elements in scalar initializer
environment.c:66:28: note: (near initialization for ‘test_cases[0][1]’)
environment.c:67:13: warning: braces around scalar initializer
environment.c:67:13: note: (near initialization for ‘test_cases[1][0]’)
environment.c:67:18: error: excess elements in scalar initializer
environment.c:67:18: note: (near initialization for ‘test_cases[1][0]’)
environment.c:67:13: warning: braces around scalar initializer
environment.c:67:13: note: (near initialization for ‘test_cases[1][1]’)
environment.c:67:26: error: excess elements in scalar initializer
environment.c:67:26: note: (near initialization for ‘test_cases[1][1]’)
environment.c:68:13: warning: braces around scalar initializer
environment.c:68:13: note: (near initialization for ‘test_cases[2][0]’)
environment.c:68:21: error: excess elements in scalar initializer
environment.c:68:21: note: (near initialization for ‘test_cases[2][0]’)
environment.c:68:13: warning: braces around scalar initializer
environment.c:68:13: note: (near initialization for ‘test_cases[2][1]’)
environment.c:68:34: error: excess elements in scalar initializer
environment.c:68:34: note: (near initialization for ‘test_cases[2][1]’)
environment.c:69:13: warning: braces around scalar initializer
environment.c:69:13: note: (near initialization for ‘test_cases[3][0]’)
environment.c:69:20: error: excess elements in scalar initializer
environment.c:69:20: note: (near initialization for ‘test_cases[3][0]’)
environment.c:69:13: warning: braces around scalar initializer
environment.c:69:13: note: (near initialization for ‘test_cases[3][1]’)
environment.c:69:32: error: excess elements in scalar initializer
environment.c:69:32: note: (near initialization for ‘test_cases[3][1]’)
environment.c:70:25: error: request for member ‘x’ in something not a structure or union
environment.c:71:25: error: request for member ‘y’ in something not a structure or union
environment.c:72:25: error: request for member ‘x’ in something not a structure or union
environment.c:73:25: error: request for member ‘y’ in something not a structure or union
environment.c:77:13: error: unknown type name ‘point’
environment.c:82:26: error: request for member ‘x’ in something not a structure or union
environment.c:82:36: error: request for member ‘y’ in something not a structure or union
environment.c:82:46: error: request for member ‘x’ in something not a structure or union
environment.c:82:56: error: request for member ‘y’ in something not a structure or union
environment.c:85:17: error: unknown type name ‘point’
environment.c:85:34: error: request for member ‘x’ in something not a structure or union
environment.c:85:44: error: request for member ‘y’ in something not a structure or union
environment.c:85:38: error: excess elements in scalar initializer
environment.c:85:38: note: (near initialization for ‘p’)
environment.c:86:17: error: unknown type name ‘point’
environment.c:86:34: error: request for member ‘x’ in something not a structure or union
environment.c:86:44: error: request for member ‘y’ in something not a structure or union
environment.c:86:38: error: excess elements in scalar initializer
environment.c:86:38: note: (near initialization for ‘q’)
environment.c:88:233: error: implicit declaration of function ‘distance’ [-Wimplicit-function-declaration]
environment.c:88:384: error: implicit declaration of function ‘_correct_distance’ [-Wimplicit-function-declaration]
environment.c:100:9: error: unknown type name ‘record’
environment.c:100:9: warning: braces around scalar initializer
environment.c:100:9: note: (near initialization for ‘t[0]’)
environment.c:100:9: warning: braces around scalar initializer
environment.c:100:9: note: (near initialization for ‘t[0]’)
environment.c:100:29: error: excess elements in scalar initializer
environment.c:100:29: note: (near initialization for ‘t[0]’)
environment.c:100:9: warning: braces around scalar initializer
environment.c:100:9: note: (near initialization for ‘t[0]’)
environment.c:100:37: error: excess elements in scalar initializer
environment.c:100:37: note: (near initialization for ‘t[0]’)
environment.c:100:40: error: excess elements in scalar initializer
environment.c:100:40: note: (near initialization for ‘t[0]’)
environment.c:100:33: error: excess elements in scalar initializer
environment.c:100:33: note: (near initialization for ‘t[0]’)
environment.c:100:9: warning: braces around scalar initializer
environment.c:100:9: note: (near initialization for ‘t[1]’)
environment.c:100:9: warning: braces around scalar initializer
environment.c:100:9: note: (near initialization for ‘t[1]’)
environment.c:100:50: error: excess elements in scalar initializer
environment.c:100:50: note: (near initialization for ‘t[1]’)
environment.c:100:9: warning: braces around scalar initializer
environment.c:100:9: note: (near initialization for ‘t[1]’)
environment.c:100:58: error: excess elements in scalar initializer
environment.c:100:58: note: (near initialization for ‘t[1]’)
environment.c:100:61: error: excess elements in scalar initializer
environment.c:100:61: note: (near initialization for ‘t[1]’)
environment.c:100:54: error: excess elements in scalar initializer
environment.c:100:54: note: (near initialization for ‘t[1]’)
environment.c:101:225: error: implicit declaration of function ‘average_speed’ [-Wimplicit-function-declaration]
environment.c:101:376: error: implicit declaration of function ‘_correct_average_speed’ [-Wimplicit-function-declaration]
environment.c:111:9: error: unknown type name ‘record’
environment.c:111:9: warning: braces around scalar initializer
environment.c:111:9: note: (near initialization for ‘t[0]’)
environment.c:111:9: warning: braces around scalar initializer
environment.c:111:9: note: (near initialization for ‘t[0]’)
environment.c:111:30: error: excess elements in scalar initializer
environment.c:111:30: note: (near initialization for ‘t[0]’)
environment.c:111:9: warning: braces around scalar initializer
environment.c:111:9: note: (near initialization for ‘t[0]’)
environment.c:111:43: error: excess elements in scalar initializer
environment.c:111:43: note: (near initialization for ‘t[0]’)
environment.c:111:46: error: excess elements in scalar initializer
environment.c:111:46: note: (near initialization for ‘t[0]’)
environment.c:111:38: error: excess elements in scalar initializer
environment.c:111:38: note: (near initialization for ‘t[0]’)
environment.c:111:9: warning: braces around scalar initializer
environment.c:111:9: note: (near initialization for ‘t[1]’)
environment.c:111:9: warning: braces around scalar initializer
environment.c:111:9: note: (near initialization for ‘t[1]’)
environment.c:111:58: error: excess elements in scalar initializer
environment.c:111:58: note: (near initialization for ‘t[1]’)
environment.c:111:9: warning: braces around scalar initializer
environment.c:111:9: note: (near initialization for ‘t[1]’)
environment.c:111:70: error: excess elements in scalar initializer
environment.c:111:70: note: (near initialization for ‘t[1]’)
environment.c:111:73: error: excess elements in scalar initializer
environment.c:111:73: note: (near initialization for ‘t[1]’)
environment.c:111:66: error: excess elements in scalar initializer
environment.c:111:66: note: (near initialization for ‘t[1]’)
environment.c:111:9: warning: braces around scalar initializer
environment.c:111:9: note: (near initialization for ‘t[2]’)
environment.c:111:9: warning: braces around scalar initializer
environment.c:111:9: note: (near initialization for ‘t[2]’)
environment.c:111:86: error: excess elements in scalar initializer
environment.c:111:86: note: (near initialization for ‘t[2]’)
environment.c:111:9: warning: braces around scalar initializer
environment.c:111:9: note: (near initialization for ‘t[2]’)
environment.c:111:99: error: excess elements in scalar initializer
environment.c:111:99: note: (near initialization for ‘t[2]’)
environment.c:111:103: error: excess elements in scalar initializer
environment.c:111:103: note: (near initialization for ‘t[2]’)
environment.c:111:95: error: excess elements in scalar initializer
environment.c:111:95: note: (near initialization for ‘t[2]’)
environment.c:122:9: error: unknown type name ‘record’
environment.c:125:17: error: request for member ‘position’ in something not a structure or union
environment.c:126:17: error: request for member ‘position’ in something not a structure or union
environment.c:127:17: error: request for member ‘interval’ in something not a structure or union
environment.c:128:17: error: request for member ‘interval’ in something not a structure or union
environment.c:129:17: error: request for member ‘interval’ in something not a structure or union
Már a múltkor is kérdeztük, hogy mi az a source.c és mi az environment.c, nem emlékszem, hogy válaszoltál volna.
Amúgy ha az egyik fájlban deklarálsz egy típust, a másik fájlban hogy akarod használni, ha nem include-olod be?
Huu ehhez tehetség kell hogy valaki ilyen okádék minőségű kódot dobjon össze. Ebben szó szerint több a hiba és a rossz megoldás mint a normálisan megírt, használható dolog...
Nyugtass meg hogy téged valójában nem érdekel a programozás csak apu és anyu javasolta hogy menj informatikusnak mer abban sok a píz'
További kérdések:
Minden jog fenntartva © 2024, www.gyakorikerdesek.hu
GYIK | Szabályzat | Jogi nyilatkozat | Adatvédelem | Cookie beállítások | WebMinute Kft. | Facebook | Kapcsolat: info(kukac)gyakorikerdesek.hu
Ha kifogással szeretne élni valamely tartalommal kapcsolatban, kérjük jelezze e-mailes elérhetőségünkön!