|
Message Closed
modified 15-May-23 19:06pm.
|
|
|
|
|
Try using nullptr instead of NULL. NULL is defined as a macro, often the equivalent of #define NULL ((void *)0) . That was fine for C, but C++ has stricter rules about converting to/from NULL pointers, so you should use nullptr, particularly when you want to pass a null pointer to a function.
Keep Calm and Carry On
|
|
|
|
|
Hope no one screams at me but if they do I guess its okay this question is simple its ridiculous
but here goes my total number in decimal is 1,621,270,208 the number I am trying to figure out the percent is from is 1,604,667,016
I did this calculation on the web and it came out to 98% the two number the first being total is a int the second unalloc is also a int. the variable percent is a double or float
I thought mistaking that percent = unalloc / total woudgive me 0000.98 in percent but I realize that since the quotient is zero percent is 000.000 doing percent = unalloc % total would give the reminder in percent and thus a value of 1604665335.000 I have searched the web I know this is real simple to some of you wonder if you could help
thanks in advance
|
|
|
|
|
Presumably you're doing something like
int total = 100;
int unalloc = 96;
double percent = unalloc/total;
So the problem here is that since unaloc and total are both int , the calculation is done in integer math, and then promoted to double. What you need to do is to promote either operand to the division to double (or both):
double percent = (double)unalloc/total:
Keep Calm and Carry On
|
|
|
|
|
that was it thank you so so so much
|
|
|
|
|
hey k5054 my comment does not relate to the currently running thread. I just wanted to let you know I keep getting back to some of your replies in topic I have posted quite a while ago. I thought it might be a good idea to let you know I`m haunting those posts.
|
|
|
|
|
Hey lookee here ... a joke icon!
That's what I needed the other day. Oh well.
|
|
|
|
|
double total = 1621270208;
double percentFrom = 1604667016;
double percent = percentFrom / total * 100;
|
|
|
|
|
|
Message Closed
modified 15-May-23 19:07pm.
|
|
|
|
|
If you pass a list of random types (numbers, strings, objects), then your function needs some method of determining what each reference is. That is why printf (and its derivatives) requires a format string to identify the type of each parameter.
|
|
|
|
|
Message Closed
modified 15-May-23 19:07pm.
|
|
|
|
|
I'm not sure exactly where you're stuck. But lets start with the example you gave in your original post:
void PrintFloats (int n, ...)
{
int i;
double val;
printf ("Printing floats:");
va_list vl;
va_start(vl,n);
for (i=0;i
Assuming that you're stuck here, then you might complete this something like
for(i = 0; i < n; ++i) {
val = va_arg(vl, float);
printf("%f ", val);
}
va_end(vl);
putchar('\n');
In this case I'm assuming that the int n parameter tells the function how many float values to expect. Other options to tell a variadic function how many arguments to expect are to use some sort of format string, like printf() does, or you can use some sort of sentinel value for your function e.g.
PrintFloats(1.1, 2.2, -15.2, nan("")); In this case we have assumed that a NaN won't be part of the input string, so is a fair choice for a sentinel.
Keep Calm and Carry On
|
|
|
|
|
Message Closed
modified 15-May-23 19:07pm.
|
|
|
|
|
It's pretty straight forward, really. When you use the va_arg macro, the second argument is the received objects type. So, to receive a pointer to a type, you use va_arg(args, Obj*) where Obj is some object type - e.g. class, int, double, struct, etc. Here's a short working example:
#include <cstdarg>
#include <iostream>
struct S {
int data;
S(int d) : data(d) {}
};
void f(size_t n, ...)
{
va_list args;
va_start(args, n);
for(size_t i = 0; i < n; ++i) {
S* ptr = va_arg(args, S*);
std::cout << ptr->data << '\n';
}
}
int main()
{
S item1(1);
S item2(2);
S item3(3);
f(3, &item1, &item2, &item3);
}
Keep Calm and Carry On
|
|
|
|
|
As I said, you need some control to identify hoe many parameters are being passed, and what type they are. There is no other way of knowing when you get to the end of the list.
|
|
|
|
|
I wrote this linked list code. Could someone confirm I`m doing it the way I`m supposed to.
class SomeNode
{
public:
int data;
SomeNode * next;
};
SomeNode * BuildList()
{
SomeNode * Tail;
Tail = (SomeNode*)malloc(sizeof(SomeNode));
Tail->next = NULL;
Tail->data = 100;
SomeNode * Temp;
bool firstrun = true;
for(int i =0; i < 5; i++)
{
if(firstrun)
{
Temp = AddTo(&Tail,i);
firstrun = false;
}
else
{
Temp = AddTo(&Temp,i);
}
}
return Temp;
}
SomeNode * AddTo(SomeNode ** Source, int somedata)
{
SomeNode * NewSN;
NewSN = (SomeNode*)malloc(sizeof(SomeNode));
NewSN->data = somedata;
NewSN->next = (*Source);
return NewSN;
}
|
|
|
|
|
- Since your code is C++ then why do you use malloc rather than new?
- How and where do you free your allocated in heap memory?
|
|
|
|
|
Thanks for your feedback Victor.
Since these are your only observations I take it that overall my code is within 'allowed' range.
malloc: Using it doesn`t seem to break anything so why not.
free: the code is just a display of how things would work in principle.
modified 18-Mar-22 7:20am.
|
|
|
|
|
One thing it is not good using malloc in C++ apps is because the class ctor is not called.
I see you are trying then to "solve" this problem using this code:
<pre lang="C++"> SomeNode * Tail;
Tail = (SomeNode*)malloc(sizeof(SomeNode));
Tail->next = NULL;
Tail->data = 100;
But you wiil need to do it in every place you will create the class instance and, moreover, after some updates in the class members you will have to look for all the occurrences of such a malloc and update the class member initializations accordingly!
|
|
|
|
|
Quote: One thing it is not good using malloc in C++ apps is because the class ctor is not called.
well I`m not really chasing the perfect solution, to be honest I haven`t even got to use the feature that much until now, my projects were simple enough to go about without using new/malloc
Again huge thanks for helping me sort this out.
modified 18-Mar-22 16:36pm.
|
|
|
|
|
I have an update. This is a function for inserting nodes in the middle of the list. Is this a good approach? I did a quick test and it looks like it`s doing what it should.
void InsertTo(SomeNode ** FrontTip, int NewNodeData, int InsertInFrontOfNodeCount, int TotalNodeCount)
{
SomeNode * Temp;
SomeNode * NewNode;
SomeNode * Previous;
NewNode = (SomeNode*)malloc(sizeof(SomeNode));
NewNode->data = NewNodeData;
bool firstrun = true;
for(int i =0; i < TotalNodeCount;i++)
{
if(i == InsertInFrontOfNodeCount)
{
NewNode->next = Temp;
Previous->next = NewNode;
}
else
{
if(firstrun)
{
Temp = (*FrontTip)->next;
firstrun = false;
}
else
{
Previous = Temp;
Temp = Temp->next;
}
}
}
}
|
|
|
|
|
Just a few more things on top of what Victor said:
1. Typically the chaining structure comes before the payload (the data part). That allows you to have different sizes of objects in your list without having to modify the list management code.
2. Again in the typical case, the function that creates or extends the list (BuildList function in your example) receives a structure containing the list head and tail. That way you can easily add new elements at either end and this is the most common operation.
3. In general you should be very, very, very sure that you need to use a linked list. Linked lists perform poorly because they don't make good use of cache. For a longer discussion about this see Bjarne Stroustrup: Why you should avoid Linked Lists - YouTube[^] and also Are lists evil? -- Bjarne Stroustrup : Standard C++[^].
Mircea
|
|
|
|
|
CalinNegru wrote: Could someone confirm I`m doing it the way I`m supposed to. Maybe you meant to phrase it differently, but what is the "supposed to" way to which you refer? There are multiple ways to code a linked list, and only you know what goal(s) you are trying to meet.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
It`s just a first time linked list implementation experience, I want to know if I`m not doing foolish things.
Quote: There are multiple ways to code
I knew there is more than one approach when writing a linked list, but is there `that` many ways to do it?
|
|
|
|