I thought those places were real!
Technically, nothing you use in programming exists because it's just text. ;) Even assemblers take a text file of assembly language which is then converted by an assembler into machine code.
I thought those places were real!
Technically, nothing you use in programming exists because it's just text. ;) Even assemblers take a text file of assembly language which is then converted by an assembler into machine code.
I mean overwrite the macro variable with a non macro variable
A macro isn't a variable. The name of a macro is physically replaced with its value in your file prior to compilation of the code. So if you have #define FOO 5
, every use of FOO will be textually replaced with 5.
I need to bring an example of overwritten.
Then you need to explain what you mean by "overwritten" more clearly. Because the way you've described it so far, an example that actually compiles is impossible.
This is the syntax for the configuration files of Apache servers. This kind of syntax is used inside httpd.conf, apache2.conf, .htaccess and the files used to configure virtual hosts.
We don't have a highlight mode for that, it's probably interpreting it as XML. But I absolutely agree that producing different highlighted text than raw text is a bad thing, and when I get a chance I'll see if it's something that can be fixed without herculean efforts.
Let's say i
is 6. You're trying to take the factorial of (2 * 6 + 1)
, or 13. 13! is 6227020800, which overflows even an unsigned 32-bit integer (your range is roughly halved from that by using a signed type). It's not unexpected that you'll start getting funky values very quickly given the rapid growth of factorials.
What is the OP's question ?
The OP's question was clearly "d". Can't you read? ;) Now we need to figure out what the question means and answer it. :rolleyes:
hey deceptikon is it using logn time to evaluate a fibo numbers.
Yes. Or rather, it's O(nlogn) because I'm calculating the whole sequence. But for a single number yes, it's O(logn).
i don't know which GOOGLE you use :p
It's the special google for google-fu masters. But seriously, it's really just a matter of remembering things you've seen before, constructing a likely keyword search, and refining that search based on the results until you find something useful.
and from where i can read the logic which is used in this ?
Well...you could read the book that I linked to. The logarithmic Fibonacci algorithm is an exercise to a section on exponentiation (hint), and the exercise itself does a reasonably good job of explaining how the algorithm works.
It might also be instructive if you reverse engineered my solution to the exercise (computing p' and q' in terms of p and q), but I won't insist on it. ;) Merely tracing through the algorithm and noting the transformations to understand how it works should be sufficient.
this just takes in one value, so i cant enter double digit numbers here!
I assumed that's what you intended. If you want to handle multiple digits then you're getting into the conversion stuff that scanf() does. It's not especially difficult for integer types (floating point types are a pain in the booty), but there are niggling edge cases that need to be considered for a truly robust conversion.
However, the core of the algorithm is this:
int stoi(const char *s)
{
int result = 0;
/* Positive numbers only! */
while (*s && isdigit(*s)) {
result = 10 * result + (*s - '0');
++s;
}
return result;
}
You progressively get a digit, find it's numeric value (by subtracting the character '0', see if you can figure out why this works), add that value to the result, then multiply or shift the result left by a tens place to make room for the next digit. For the string "123", the trace would look like this:
10 * 0 + ('1' - '0') // result == 1
10 * 1 + ('2' - '0') // result == 12
10 * 12 + ('3' - '0') // result == 123
No, please check the link, in the Apache configuration file or in .htaccess files this is the correct syntax.
Our highlighter doesn't magically determine that you're using an Apache configuration or .htaccess file and adjust its logic accordingly. So whether it's valid for Apache or not is irrelevant. Is this valid XML? If not then the highlighter could rightfully be choking on malformed syntax.
Of course I still don't think it should be modifying the content, but that would at least explain why it's happening. ;)
where is union required?
A union is never required, but it can simplify certain tasks such as type punning. An example is from the C standard library implementation in my signature. I use a union for punning between an IEEE double, its 64-bit representation, and a breakdown of the key components (specified by bitfields):
typedef union _real8 {
unsigned long long ivalue;
double fvalue;
struct {
unsigned long long mantissa : 52;
unsigned long long exponent : 11;
unsigned long long sign : 1;
} parts;
} _real8_t;
This saves me from having to use the bitwise operators to break down the value manually such as in this helper function:
/*
@description:
Implementation helper for the fpclassify macro for double/long double.
*/
int _fpclassifyd(double value)
{
_real8_t fpv;
fpv.fvalue = value;
if (fpv.parts.exponent == 0)
return fpv.parts.mantissa == 0 ? FP_ZERO : FP_SUBNORMAL;
else if (fpv.parts.exponent == 0x7FF)
return fpv.parts.mantissa == 0 ? FP_INFINITE : FP_NAN;
return FP_NORMAL;
}
The alternative would be much uglier and harder to infer the intended meaning of the code.
please explain with an example where having union is a better choice than any data structure.
This question is confusing, and I suspect you're using "data structure" to mean C's struct. Keep in mind that "data structure" is used elsewhere in computer science to mean a more general way of storing and accessing data (eg. linked lists, trees, stacks, queues).
I did it for myself, because it was easier to remember and easier to program.
If you're writing code only for yourself then that's fine. If you want other people to read it too then you need to keep them in mind while writing the code. Granted this is a fairly minor thing because an astute reader will be able to decipher your variables' meaning through context, but that still requires extra effort. Unless I'm required to read and understand your code (I'm not, by the way), the amount of effort needed is inversely proportional to the likelihood of me even bothering. Put another way, the harder code is to read, the less likely I'll take time to read it.
Deceptikon, I copied and pasted your code and still would not work. It says error opening file.
That's the entire point of my code: to add error handling that tells you what's wrong. I didn't fix the problem because it's obviously a case of user error in typing the correct file path. But knowing that the error is "file not found", or at the very least "cannot open file" points you in the direction of how to fix the problem. That's why catching and reporting errors is so important. Otherwise you're sitting in the dark with no clue of how to proceed.
i dont get any output for case1 (ie char2shrt()) and case2 (ie int2shrt()).. any ideas on that?
'h' is a modifier to the %d specifier, you need them both: "%hd"
i do get an output for case 3 (ie shrt2int()), but have to work out in binary if its correct or not.
You don't need to fiddle with binary to determine correctness, just look at an ASCII table. Type a character and check the corresponding value in the table to verify. For example, if you type 'a' the result should be 97.
*type* input[40]
means what?
It means input is an array of 40 objects of size "type". If "type" is char
then you have an array of 40 bytes, if "type" is int
then you have an array of 40 integers where each one is 4 bytes (going with your assumption). The size of an array of 40 will always be sizeof(type) * 40
. Note that sizeof(char)
is always 1, and char
corresponds to the system's byte size, so an array of 40 char is equivalent to 40 bytes.
Simultaneous processing can be done with threading, but that's a can of worms that I don't recommend opening unless you absolutely must. A way to fake it in a single threaded application would be to specify a distance threshold between the two keypresses that denotes "at the same time", and any two keypresses inside that distance would be treated as simultaneous.
Obviously this means calculating the time between each player's keypress, which isn't terribly difficult. However, you'll need to rely on a nonstandard timing method because C's standard timing granularity is limited to seconds. You could probably say that "simultaneous" is within 1ms.
What compiler and OS are you using?
When working out these problems, start by understanding what the expected result should be, then trace through your code in a debugger and find the place where the expected result differs from the actual result. That's where your logic error is likely to be.
Obviously this process can be difficult if you're using code you don't understand, which is why it's important to understand the problem fully and how the code goes about solving it. ;)
On a side note, there's a more elegant solution to this problem. It's described as an exercise here and solved/converted to C here:
#include <stdio.h>
long long fib_iter(long long a, long long b, long long p, long long q, int count)
{
if (count <= 0)
return b;
if (count % 2 == 0) {
long long p_next = (p * p) + (q * q);
long long q_next = (2 * p * q) + (q * q);
return fib_iter(a, b, p_next, q_next, count / 2);
}
else {
long long a_next = (b * q) + (a * q) + (a * p);
long long b_next = (b * p) + (a * q);
return fib_iter(a_next, b_next, p, q, count - 1);
}
}
long long fib(int n)
{
return fib_iter(1, 0, 0, 1, n);
}
int main(void)
{
for (int n = 0; n < 10; ++n)
printf("%lld ", fib(n));
putchar('\n');
return 0;
}
FYI: Dani and I typically use Area 51 for testing stuff in production so as to avoid cluttering the publicly viewable forums with such things.
Can you try clearing your browser cache (not your cookies) and try again? :)
Same behavior.
Another issue I'm now suddenly having (and only James, Davey and Sanjay) will be able to test this one, is I need to refresh the page if I try to delete my own post without specifying a reason.
That one I can reproduce.
Odd. It works for me with Chrome on Windows 7 64-bit.
It might be just you, I haven't seen that issue at all. At least not with Chrome, what browser are you using to test with?
You would do well with some error handling, as I suspect the file isn't being opened and span remains uninitialized:
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string filename;
cout << "Enter a file path: ";
getline(cin, filename);
ifstream in(filename.c_str());
if (!in)
perror("Error opening requested file");
else {
double span;
if (!(in >> span))
cerr << "Invalid file format (expected floating point value)\n";
else
cout << "span = " << span << '\n';
}
}
i didn't get anything in that line sorry to say.
Did you run it? Did you try printing fmt to see what its contents are after sprintf() runs? I don't mind explaining things, but it would be better for you to experiment first, because someone won't always be around to explain things to you.
how fgets() is more better than what npcpmplete has said.
It more clearly shows your intention (read a line of string data), avoids the overhead of scanf() because fgets() doesn't need to interpret a format string, and of course it's easier to miss the huge bug of not including a field width with scanf(). Another consideration is if the size of the array that will hold the string isn't a compile time entity, in which case you need to construct the format string at runtime in a very awkward manner:
char *buf; /* Buffer with unknown compile time size */
size_t size; /* Runtime size of buf */
char fmt[BUFSIZ];
sprintf(fmt, "%%%d[^\n]%%*c", size);
/* Now use scanf() after constructing a format string */
if (scanf(fmt, buf) != 1) {
/* Handle an input error */
}
There's nothing really wrong with using scanf(), assuming you use it correctly, but it's kind of like using a wrench as a hammer. It works, but it's not the most obvious tool, and the more obvious tool will probably work better.
First you have this:
#define radius
Then this:
cout<<"\nRadius is " << radius << set(8)<< "PI is" <<PI;
What exactly were you expecting to get printed when radius is replaced with nothing? The end result after preprocessing is this:
cout<<"\nRadius is " << << set(8)<< "PI is" <<PI;
And that's obviously a syntax error. You have the same problem anywhere radius is used to mean anything except nothing.
Instead of using "%s" format directive, why don't you use scanf(" %[^\n]",a[i]); that is "%[^\n]" format directive.
Or else, use "%[^\n]%*c" format directive. It will take away all the things that is present in the input buffer, otherwise the newline or form feed may eat away your subsequent format directive.
Why would this be a better solution for line input than fgets()? Hell, without a field width this suggestion isn't even better than gets(), with the exception that you can add a field width to make it safe while gets() is never safe. ;)
This should NEVER be done.
Technically it's illegal in C++. So if it works, you'd be relying on a compiler extension.
i want to gain full knowlege of programming language..
Um...programming isn't that simple. Not even the creator of a programming language has "full" knowledge of it, because "full" knowledge encompases more than just the trivia of syntax and semantics. Even if you're a book smart pedant who can quote chapter and verse of the language definition, that doesn't guarantee that you're even remotely proficient with using the language.
That's weird. It's almost as if our code highlighter is interpreting tags with no content as empty tags. At least for that particular malformed situation (an extra closing tag that was probably added automatically by a "helpful" editor):
<tag></tag>
stuff
stuff
</tag>
What's up with that?
Obviously Daniweb is so awesome that Microsoft decided to emulate us. :)
if im doing a dll in dev c++ .....
and doing a dll in c++ but using visual studio 2010is both of them gonna be the same??
The two DLLs will be compatible, yes. So you can create a DLL with one compiler and load it with another.
The conio library is nonstandard and thus not supported by all compilers. If you can remove the parts of conio that you use (such as a leading clrscr() or a trailing getch() in your main() function), that's probably the best approach. Otherwise you'll need to look for alternatives or change your desing.
Not fair, deceptikon -- giving me rep and taking it back! Positive rep, even as a test, is a good thing! :P ;)
The test was actually to see if you'd get negative rep. Aren't you glad the effect was as I hoped? ;)
On a serious note, I hope you aren't planning on changing the wayt the box and arrows interract. I happen to like the way they work now.
I'd like to make it more clear visually which arrow the box is affecting, not change the behavior.
Oh, and the comment you made was left in my list of rep even after you removed it. That might be a bug...
Yeah, I'll need to look at that. If it's practical to do so, the summary should match what you would see by browsing the posts.
Hmm that crashes on the very last row if there is a dot.
My example was just that, an example. It's your job to make sure that the concept is properly incorporated into your code, which includes adding error handling. It's pretty obvious that boundary cases aren't covered, and in hindsight I should have added a comment for those who fail to engage their brains before copying and pasting. For example, if you say board[x - 1]
and x
is 0, what do you think will happen?
How are you creating the child process? The answer to this question depends on your OS and compiler because managing processes is highly dependent on the system.
This thread was marked as solved. Did you solve your own problem?
On a side note, is it really so hard to type "input"? I mean, it's got to be less awkward to type and is only two keystrokes longer.
The statement is wrong in the book -- at least the PDF I have.
I'm not aware of an official PDF version, so I'd suspect that it's a bootleg copy and contains copying errors. Unfortunately, I don't have my hardcopy available at the moment, so I can't confirm or deny the typo.
but what to do ? Nobody was replying and i was eager to get a reply.
My first instinct is to say don't put all your eggs in one basket. It's unreasonable to expect that Daniweb will have all of your answers, so while waiting for a reply you should be doing as much research as possible in an attempt to answer your own question.
i just need to ask how can i save the string in C ?
It looks to me like you were asking how to write a suffix array based sort, which isn't exactly trivial. Might I suggest looking at existing programs that do what you want? I have no doubt you can find at least one on google.
atleasy one must reply.
Why? Replying to threads is strictly voluntary. Just because you really want an answer doesn't obligate anyone to provide one.
If you have two non-black spots going in different directions from a cell, then that's an intersection:
bool is_intersection(char board[M][N], int x, int y)
{
if (board[x][y] == '#')
return false;
return
(board[x - 1][y] != '#' || board[x + 1][y] != '#') &&
(board[x][y - 1] != '#' || board[x][y + 1] != '#');
}
@deceptikon : u make it so easy :)
That's the result of making the same error a bajillion times. It kind of stands out now. ;)
A program for
A
BC
DEF
GHIJ
KLMNO
That's kind of weird, but okay:
#include <iostream>
using namespace std;
int main()
{
cout << "A\nBC\nDEF\nGHIJ\nKLMNO\n";
}
p.s. We're not your coding slaves. Posting your homework without even a please or thank you again will find it deleted. Please read our rules.
No this is not a school assignment...just something i thought i'd practice
That doesn't remove the onus of making an honest attempt.
Please shut up if you don't know how to code...
Thank you so much for that comment. I might have actually wasted time helping an ungrateful jerk. I really appreciate your concern for everyone's time. Best of luck with your problem.
if (c == ' ' || c == '\n' || c = '\t')
One of these things is not like the other. Look very closely, I'm sure the book didn't mix up operators.
Please post the compiler errors.
What happens if n is greater than 9?
I respect the OP for coming back with an improved attempt, and in keeping with the spirit of DaniWeb, I'd be more than happy to offer further support with it.
I'm also happy to help, but the OP has failed to ask any kind of specific question about how to improve the code. Both threads strike me more as "look at my awesome code!" rather than "I'm concerned with these parts of my code, how can I improve it and do you have any other suggestions?"
I'll also note that my first suggestion was immediately rejected as "completely unnecessary". That's not exactly indicative of an open mind.
Respectfully, I don't think that's called for.
Respectfully, I do. I get the distinct impression that the OP is getting a big head, and that big head needs to be deflated otherwise his progress will quickly stagnate. The more you blow sunshine up someone's ass, the more they'll have unreasonable expectations.
The only reason I have used words like "jaar", "maand" and "dag", is because it was easier for me to remember and easier to program. For example: using the the word "socialism" for the current year would be a little bit confusing.
I don't think you understand. Unless the reader knows what jaar, maand, and dag mean, they'll have trouble understanding your code without making assumptions based on context or doing a Dutch to English translation. I'm suggesting that you use the English version of the same words: year, month, and day. I'm not suggesting that you name your variables something completely random, and I can't imagine how you inferred that.
Updates should be "Updated". Unfortunately I can't change it now.
You could reply to the existing thread rather than creating a new one... :rolleyes:
Yes, of course I am interested in possible improvements. :-)
I'd start by dropping the Dutch variable and function names, and switching to English. If you want people on an English speaking forum to offer improvements then it's wise to make the code as readable as possible for those people.
I think a lot of people are interested in updates!
On a simple program that any reasonably competent C++ beginner could write? You might find a few people who are interested, but they're all going to be beginners like you who have homework similar to your program's description.
I think you need to adjust your opinion of this program to be more realistic. You're 14 (according to your signature), so it's understandable that you think the world revolves around you and everything that you produce is magic, but I'm willing to bet that most of the people who see this program will think the same thing I did. It's quaint, and props to you for doing it, but it's clearly written by a beginner and I could quickly write a better version with minimal effort. Therefore, I have zero interest in either the original program or updates.
If you have any specific questions about how to improve the program, I'll be happy to offer some opinions. However, I'm not going …