« first day (255 days earlier)      last day (2600 days later) » 

2:30 PM
hey guys I am solving some online programming problems in C, my solution and their solution is differing in few points as follows:

My output Desired output
0.612245 0.612245
7.777777 7.777778
89.999954 90.000000
43.877560 43.877551
13.852420 13.852422

How can I get exact their output?
 
cheat
floating point should never be compared for equality
only approximate equality
so it's a mistake in their checker
 
nwp
or you are using float when you should be using double
 
ummm...
let me check
 
@nwp still shouldn't be directly checking for equality
generally speaking you almost always need to use an epsilon
 
double is giving weird output
 
nwp
2:34 PM
I would expect the desired output to be strings and the requirement to be exact to 6 digits after the comma, which would be fair.
 
ok this is my simple program with float:

#include <stdio.h>

int main()
{
float p;
scanf("%f",&p);
printf("%f",(p/((1-p)*(1-p))));
return 0;
}

It gets accepted with input 0.5 as its output is 2.000000. But it fails with rest of the inputs as I showed in above list.

How do I convert it to double? Which format specifier?
%g ?
 
14 messages moved from Lounge<C++>
 
Ell
read the rules next time :( @mahesha999
 
Any help?
#include <stdio.h>

int main()
{
double p;
scanf("%g",&p);
printf("%g",(p/((1-p)*(1-p))));
return 0;
}

gives output 5.2221e-315
 
nwp
@Mahesha999 wrong language
 
2:41 PM
@Mahesha999 consider that a) 1 is not 1.0 also that's C not C++
nor is 1.0 the same as 1.0f
also consider that printf always promotes any floating point value given to it to double
 
ummm trying it with different combinations...but still no luck...
 
@Mahesha999 try using C++ not C also typing your constants correctly
 
@Mgetz in this case it doesn't matter though
 
nwp
@Mahesha999 Don't try different combinations. Look up which specifier is correct for scanf and doubles, then use the return value and a debugger to see if scanf succeeded.
 
@ratchetfreak you say that... but I've had it matter
 
2:53 PM
So is char *argv[] an array of pointers to chars, or a pointer to an array of chars?
 
@nwp which specifier is correct... any more explanation... ?
 
@Mgetz (1-p) where p is a double or float will always have the 1 converted to the same type of p and then the subtraction will be executed
 
@Mahesha999 Maybe time to hit documentation rather than increase the repeat pressure
My kid still has to learn this.
 
@JoshuaBarnett In the context of parameter lists, it's actually none of them
 
2:55 PM
@ratchetfreak yes, at runtime this can produce different output depending on CPU
 
@milleniumbug head asplodes
 
IOW in int main(int argc, char* argv[]) { char* aaa[] = { nullptr, nullptr }; } aaa is of type char*[2] (array of pointers), and argv is of type char**
377
Q: How do I use arrays in C++?

fredoverflowC++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector<T> since C++98 and std::array<T, n> since C++11), so the need for arrays does not arise quite as often as it does in C. However, when you read l...

 
3:20 PM
How do I specify a function parameter as a reference to an array of pointers?
 
@JoshuaBarnett foo(std::array<int*, N>& refOfArrOfPointers);
 
nwp
@JoshuaBarnett in case you like pain
 
cpp is pain
 
nwp
actually that is C, the C++ version looks much more reasonable
 
just give in to the stockholm syndrome
 
nwp
3:32 PM
except references are not C... you know what I mean
 
4:05 PM
Anyone know if I can do something like this?

enum OptionKey {
UID,
START,
LIST
};

struct OptionValue {
std::string name
int parameters;
};

std::map<OptionKey, OptionValue> options = {
OptionKey.UID, OptionValue = {
.name = 'uid';
.parameters = 1
},
OptionKey.START, OptionValue = {
.name = 'start';
.parameters = 1
},
OptionKey.LIST, OptionValue = {
.name = 'list';
.parameters = 0
}
};
 
nwp
@JoshuaBarnett in C yes, in C++ no
 
4:46 PM
How do I store a type in a variable in C++ so I can use the information for parsing?

e.g. I want to declare that a parameter should be of type x (int, char, std::string).

Then use that information to try parsing a char[] as that type?
 
templates
they allow you to make an interface like int x = parse<int>(your_sequence_of_bytes);
 
@milleniumbug so how do I define a template as member of a struct?
 
what is your actual problem
smells like XY
 
@milleniumbug I'm trying to declare in my program all the possible options that can be passed as argv.

So --list may have no parameters but its still a unique option.

--uid may have one parameter of type int and name uid.
I can then use this information to parse argv.
 
5:02 PM
implementing getopt?
 
@ratchetfreak should I be using getopt?
 
getopt doesn't parse the arguments just puts a pointer to the argument string
 
5:20 PM
@JoshuaBarnett there is like 54634563 libs for argument option parsing, or you can do it manually
 
 
1 hour later…
6:20 PM
In the following code, why is the output 8 instead of 7 ? Because of the statement "case 5:", the new value of i = 7. and why will the compiler execute "case 6:" and increment i by 1 ?
 
3 messages moved from Lounge<C++>
 
nwp
1 message moved to Trash can
 
@ShrijanAryal there is no break; so the control flow "falls through" to the next case
 
but the case is 6. because of the previous case, i has been re-updated to 7. So why will case 6 be executed when i = 7 ?
 
because the switch(i) is only considered once
the i is evaluated, the case is chosen, and the flow runs from that case, until it reaches the break;
 
 
3 hours later…
9:03 PM
Hi guys, say I have two classes, where each one as data member has a pointer to the other one like
class A {
  private: B* b;
};

class B {
  private: A *a;
};
I usually have problem with separate these two classes in two separate files
how can I do that?
 
forward declarations of incomplete types
 
example?
 
class A; class B;
 
but how do I handle the inclusion?
which header include which?
 
none of them
 
9:08 PM
I'm confused
so I have one file like
class A;
class B;

class A {
  ... bla bla
};
and another one with
class A;
class B;

class B {
  ... bla bla
};
 
pretty much
 

« first day (255 days earlier)      last day (2600 days later) »