|
Calin Cali wrote: Usually I don`t try to compile wild guesses.
Well, you should because:
1. Might take less time than waiting for an answer.
2. Compiler is never cranky or in a bad mood. At most it will issue an error message.
3. One wild guess leads to another and soon you end up with a nice brilliant idea
Mircea
|
|
|
|
|
Quote: Well, you should because:
For me it feels like trying to compile a quote from Ceaikovski or Twain.
|
|
|
|
|
Mircea is quite correct, you should always try and build small samples. Apart from anything else, the error reports help you to learn. The compiler, linker and debugger excellent tools that aid in development. Had you started in the days when we had to submit a deck of punched cards and wait 24 hours for the compiler output to tell us one character was mistyped, you would appreciate how easy things are these days.
|
|
|
|
|
It`s interesting to hear where things started. I had my first computer experience in the 80`s on a computer with keyboard and a dedicated green and white screen (the screen was displaying only two colors). My first programming experience was on a computer that you had to connect to a TV set. It was a computer with 16 colors graphics.
|
|
|
|
|
Mine was the LEO* III in the 60s, second row first three pictures at Leo Computers Society. Leo 3 photos[^]. Most input was punched paper tape, and some punched cards, no mass storage, only magnetic tape.
*The Lyons Electronic Office, initially developed between Lyons Catering and English Electric, both companies long gone.
|
|
|
|
|
Working in the field since the beginning I imagine you had a computer from every generation.
|
|
|
|
|
Not quite, I started in the transistor age, so things were getting quite sophisticated.
|
|
|
|
|
You're creating a pointer to a free function (one defined outside a class) and then storing it, and invoking it, from within an object. But it's also possible to create a pointer to class member data or a member function. See here[^]. You may need to read several articles about this to gain a good understanding, because I can't point to one that is really good on its own. Search on "C++ pointer to member" and read articles that discuss the type Class::* (a pointer to a class member) and operators .* and ->* . These make the following possible:
int Class::* pm = &Class::m; int (Class::* pf)(int) = &Class::f; Class c, *k;
c.*pm = 1; n = c.*pf(0); k->.pm = 2; n = (k->*pf)(0);
|
|
|
|
|
I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet the code I have so far doesn't work:
void dectobin(int value, char* output)
{
int i;
output[5] = '\0';
for (i = 4; i >= 0; --i, value >>= 1)
{
output[i] = (value & 1) + '0';
}
}
Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic.
Any help would be much appreciated!
modified 23-Sep-22 12:22pm.
|
|
|
|
|
You don’t say exactly why it doesn’t work so I’ll just guess: the code works only with numbers up to 2^5=32. If you tried it with 192 it would have failed.
As a general advice, for small algorithms like that, it helps to “play the computer”: take a piece of paper and go through each step as you would be the computer. That gives you a better understanding of how the algorithm works and helps you find eventual bugs.
Mircea
|
|
|
|
|
Your code is correct as far as the conversion goes. The problem is that it can not handle any binary value greater than 31. You need to calculate the number of digits in the converted value first, and create an array big enough to store the string.
|
|
|
|
|
Thanks Richard MacCutchan I will Try this.
|
|
|
|
|
As noted in other answers, your code handles at most four bits.
Moreover, your function is assuming a (at least) 5-bytes buffer is provided by the caller. That's a flawn: the caller should provide the size of the buffer and the called should check if the size is big enough to produce the requested output.
Try
#include <stdio.h>
#include <stddef.h>
#include <stdbool.h>
bool uint2binstr(unsigned value, char output[], size_t output_size)
{
unsigned v = value;
unsigned bits = 0;
while ( v )
{
v >>= 1;
++bits;
}
if ( ! bits ) ++bits;
if ( output_size > bits )
{
output[bits] = '\0';
while (bits)
{
--bits;
output[bits] = (value & 1) + '0';
value >>= 1;
}
return true;
}
return false;
}
enum { N = 33 };
int main()
{
char out[N];
unsigned a[] = { 0, 1, 128, 192, 65535, 65536, -1 };
for (size_t n=0; n<sizeof(a)/sizeof(a[0]); ++n)
{
if ( uint2binstr(a[n], out, N) )
{
printf("success: '%u' decimal is '%s' binary\n", a[n], out);
}
else
{
printf("failed to represent '%u' in binary\n", a[n]);
}
}
return 0;
}
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Well explained. Thank You!
|
|
|
|
|
You are welcome.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
You have updated your question, but it is not clear what has changed since yesterday when we gave you the answer.
|
|
|
|
|
Hello everyone, new to programming and algorithms, I have an obligation to respond to a project.
That of creating a grid in which there is a prey/predator system. The beginning is to initialize the structures of the 3 elements. We are instructed to create a (dynamic) list to which only the prey and predators entered by the user are added.
I totally don't understand how.
I had the idea of creating a static list and leaving blanks when nothing is there. But our tutor told us that it's heavier and it's better to use a dynamic list rather than a static one.
Thanks for your help.
|
|
|
|
|
The question is not clear. What do you mean by "prey/predator system", what are "the 3 elements"? I know what both of those terms mean, but have no idea what sort of system you are supposed to design. I suggest you go back to your tutor and get a clearer description of what you are supposed to create.
|
|
|
|
|
If the size of the grid is static (m x n ), you can just hard-code it:
enum GridOccupant {EMPTY, PREY, PREDATOR};
GridOccupant Grid[m][n] = {EMPTY}; If the grid is dynamic, the question is whether you're allowed to use things from the Standard Template Library (STL), like std::vector [^], or whether you're supposed to allocate the memory yourself.
When the user specifies the location of Prey or Predator , you create one and add it to PreyAnimals or PredatorAnimals . Again, the question is whether you're allowed to use the STL. Each Animal (the base class for Prey and Predator , if this helps your design) keeps track of its location in the Grid to make it easy to find.
You might discover that you don't need Grid at all, only its dimensions (m x n ).
|
|
|
|
|
Apologies if the description is long; I just want to make the question clear(er).
I have a WIN32 project in which there are some applications and some DLLs that may or may not use a framework. The framework itself is statically linked to apps and DLLs and it has an important data structure that should be unique. Currently, when a DLL is loaded it has no way of knowing if the process in which it was loaded uses the framework and if the data structure has been created or not.
If DLL uses the framework and the EXE doesn't, the DLL can initialize the data structure and all is fine.
If the EXE uses the framework and the DLL doesn't, the EXE will initialize the structure and all is fine again.
The problem appears when both the DLL and the EXE use the framework and only the EXE should initialize the structure and the DLL should just somehow find the address and use the data structure.
I know I could use a shared memory area but this is not inter-process communication so I'd rather use something simpler. I even thought of setting an environment variable with the address of the structure but somehow the solution seems cheesy.
Do you have any suggestion? Is there any standard/better way of doing this?
Mircea
|
|
|
|
|
Couldn't the executable simply call a function of the DLL, passing the address of the data?
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Thanks for the suggestion but the DLL-EXE API is set in stone. With over 300 DLL modules changing the API is not easy/feasible. I have to find a different solution. I'll probably go with the environment variable; I'll just have to hold my nose while coding
Mircea
|
|
|
|
|
Rather than use the environment variable you could use the Registry.
|
|
|
|
|
Interesting idea! However I would have to take care of cleaning the registry when the app finishes. The environment variable just "evaporates" on exit. Do you see any technical advantage to using the registry?
Mircea
|
|
|
|
|
Delete a registry key or value is very simple.
And (almost) no one else (except your app) would like to change it.
|
|
|
|