John Matthews

Rancher
+ Follow
since Jul 04, 2018
Was writing 6502 (8 bit) assembler fixed point arithmetic code (to draw circles) back in the early 80s, on an Acorn Atom if that means anything to anyone. Usually write embedded C, with the odd excursion into Java and C++.
Melksham, UK
Cows and Likes
Cows
Total received
11
In last 30 days
0
Total given
0
Likes
Total received
61
Received in last 30 days
3
Total given
16
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by John Matthews

Hanna Roberts wrote:I was just trying to make the numbers match up because i had added a zero to the end of 100.0 when i wrote the rand() line should have been , so it wasn't a huge deal i just wanted to make sure people weren't confused by that

But they would have been confused if they had read the references to 1000 in other posts, and there was no 1000 in your original post because you had edited it.
2 weeks ago
Hi Jesse - Hanna can correct me if I'm wrong, but based on this and previous threads they are learning how to program in C. I don't think the randomness of the numbers generated by rand() is an issue.
2 weeks ago
The 1000 would make sense if you divided the result by 10, to give values 1.0, 1.1, 1.2, 1.3 etc.
2 weeks ago
Yes - I was going to mention that The '%' operator needs integer operands eg. 1000 not 1000.0. Stephan was pointing out that the value of 1000(.0) is probably wrong.
2 weeks ago
Hi Hanna - is it ok if the results are doubles, but with 0 fractional part eg. 1.0, 2.0, 3.0, .. 100.0? Or do you want non-integer values as well?
2 weeks ago
...although I suspect sqrt(2.0) is just being optimised to a constant value. So yes, add -lm after the .c filename on the command line.
1 month ago

Campbell Ritchie wrote:JM: I tried myself last night, and kept getting errors about, 𠇍istance.c:(.text+0x87): undefined reference to `sqrt'”. I could however get sqrt(2.0) to compile and run. ny idea what I have done wrong? Should I reinstall gcc?

I had to lookup how to link with the math library - "-lm" on the end of the command line (after the .c). But then that doesn't explain how it compiled with the 2.0 arg. Pass.
1 month ago
Hi Denis

The mistakes are just 'typos'; missing semicolons on the ends of lines, missing commas, that sort of thing. The compiler will tell you were the mistakes are. There's nothing fundamentally 'wrong' with your code; I fixed the typos (identified by the compiler) and it worked.

Make sure you enable all warnings on the compiler command line - use -Wall if you are compiling with gcc.

John
1 month ago
Hi Jesse - can you show us your code? Use the 'Code' tags to make it look nice (select the text and hit the 'Code' button).
2 months ago
One more thing - I would use
just so you can shorten 'struct Test' to 'Test'.
2 months ago
Yes, that would work. Although...

You don't need to cast the malloc() return value (unless your code is C++); malloc() returns a void*, and they don't need to be cast to other pointer types.

Rather than do sizeof(struct Test), the code would be more robust if you used sizeof(*test), the size of whatever test is pointing at. That makes the code independent of the type.
2 months ago
...but to dynamically create a structure you need to use malloc() (which will replace aTest).
2 months ago
test is still a pointer to a structure, but it also defines one of those structures, aTest, and makes test point to it.
2 months ago
...or you could do something like:
and leave the rest unchanged.
2 months ago