[Info-vax] ChatGPT solved it for me, again...
Arne Vajhøj
arne at vajhoej.dk
Fri Feb 17 08:24:57 EST 2023
On 2/17/2023 6:58 AM, Slo wrote:
> Sorry Arne, I don't trust your coding skills and an ancient language :-)
Pascal is from 1970 and C is from 1972. Big difference?
> Me: Convert this code to C
> ============================
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <time.h>
> int get_wattage(char *sys) {
> return (rand() % 20) * 50 + 200;
> }
> int main() {
> char sys1[256], sys2[256];
> int watt1, watt2;
> srand(time(NULL));
> printf("Utility to check power supply compatibility.\n");
> printf("Enter name of system #1: ");
> fgets(sys1, 256, stdin);
> printf("Enter name of system #2: ");
> fgets(sys2, 256, stdin);
> sys1[strcspn(sys1, "\n")] = '\0';
> sys2[strcspn(sys2, "\n")] = '\0';
> watt1 = get_wattage(sys1);
> watt2 = get_wattage(sys2);
> if (watt1 == watt2) {
> printf("%s and %s can use the same power supply (%d watt)\n", sys1, sys2, watt1);
> } else {
> printf("%s and %s cannot use the same power supply as they use %d and %d watt\n", sys1, sys2, watt1, watt2);
> }
> return 0;
> }
> ============================
> I did not test it.
It seems to work.
The code is not bad.
There are a few things I would do different. I would
never use strcspn for this. But somebody else may have
a different opinion on that topic.
> It also gave these comments:
>
> Note that the pstr type from the original Pascal code has been replaced with C-style strings
> (null-terminated arrays of characters), and the random function has been replaced with the
> rand function from the C standard library. The clock function from the original Pascal code
> has been replaced with the time function from the C standard library to seed the random
> number generator.
All correct.
> The trunc function from Pascal has not been used in the C code, as C
> automatically truncates floating-point values when they are cast to integers.
The explanation is BS. The difference is that C rand return an integer
while VMS Pascal Random returns a floating point.
Arne
More information about the Info-vax
mailing list