Randolph Freelander
Member
In addition to fixing the comparison, dude probably also wants an "else if"
In addition to fixing the comparison, dude probably also wants an "else if"
Wherever you can, use integers. When you start using floats/doubles for calculations, you have to be very aware that youll want to be testing within (very) small ranges for equality tests due to fp precision errors.I have to make a program that allows the user to select Metric or Us, then to do the calculations.
It keeps getting an error after the else statement.
for(int n = 0; n < 5; n++)
{
currentIteration = (pow(-1, n) * pow(x, 2*n+1)) / fact(2*n+1);
sum += currentIteration;
}
Program keeps crashing again, though it boots up at least now.
It is stating "Run-Time Check Failure #3 - The variable 'metric' is being used without being initialized."
So I guess I need to use int somewhere with metric and us?
Does someone have a decent example, since I'm considering now to just rewrite the program completely?
Program keeps crashing again, though it boots up at least now.
It is stating "Run-Time Check Failure #3 - The variable 'metric' is being used without being initialized."
So I guess I need to use int somewhere with metric and us?
Does someone have a decent example, since I'm considering now to just rewrite the program completely?
double vi;
double vf;
double d;
double f;
double metric; // this guy here
double us; // and this guy here
double x;
double y;
double unit;
Just need some verification/help here, I'm having the toughest time formatting this math expression correctly in C++.
So, I have to write the taylor series formula of sine for the first 5 iterations. So, this is what I've got:
Code:for(int n = 0; n < 5; n++) { currentIteration = (pow(-1, n) * pow(x, 2*n+1)) / fact(2*n+1); sum += currentIteration; }
Just need some verification/help here, I'm having the toughest time formatting this math expression correctly in C++.
So, I have to write the taylor series formula of sine for the first 5 iterations. So, this is what I've got:
EDIT: Read pow(-1, n) as -1 to the power of n, and fact(2*n+1) as the factorial of 2*n+1.Code:for(int n = 0; n < 5; n++) { currentIteration = (pow(-1, n) * pow(x, 2*n+1)) / fact(2*n+1); sum += currentIteration; }
This will limit the accuracy of the sine of pi/2 to the first 5 iterations of the series.
Now, theoretically, entering in pi/2 should give you a value near sin(pi/2), which should be about 1.000003206.
However, this is giving me some fucking weird random number instead of a value near or around 1.
Help GAF! I seriously do not know what the hell to do here, it's diving me insane!
void Main()
{
var currentIteration = 0.0;
double x = Math.PI / 2.0;
double sum = 0;
for(int n = 0; n < 5; n++)
{
currentIteration = (pow(-1, n) * pow(x, 2*n+1)) / fact(2*n+1);
sum += currentIteration;
}
sum.Dump();
}
public double pow(double a, double b)
{
return Math.Pow(a, b);
}
public int fact(int n)
{
var r = 1;
for (int i = 1; i <= n; i++)
{
r *= i;
}
return r;
}
#include <iostream>
#include <cmath>
using namespace std;
long fact(int);
double sin(double);
double cos(double);
long fact(int n)
{
long product = 1;
while(n > 1)
product *= n--;
return product;
}
double sin(double x)
{
double currentIteration, sum;
for(int n = 0; n < 5; n++)
{
double currentIteration = pow(-1, n)*pow(x, 2*n+1) / fact(2*n+1);
sum += currentIteration;
}
return sum;
}
int main()
{
double number, sum = 0, sineOfnumber;
cout << "Enter a number, then I will calculate the sine of this number" << endl;
cin >> number;
sineOfnumber = sin(number);
cout << "The sine of " << number << " is: " << sineOfnumber << endl;
return 0;
}
Circle 1
Circle red true 1
Rectangle 10 20
Rectangle blue true 1 5
Scanner s = new Scanner(new File("mydata.txt"));
ArrayList<String> Objects = new ArrayList<String>();
while (s.hasNext()){
Objects.add(s.next());
}
s.close();
I have to create a program where it reads off from a text file that contains:
Code:Circle 1 Circle red true 1 Rectangle 10 20 Rectangle blue true 1 5
The first string is the object type (Circle or Rectangle) then it either follows stating the radius or the color of the object type, then if it is filled.
ex,
Rectangle [object type], blue [color], true [is filled], 1 [height], 5 [width].
Since this text has many different variable types, how would I go around adding this into an ArrayList?
I have this:
but I know it is wrong since they aren't all Strings.Code:Scanner s = new Scanner(new File("mydata.txt")); ArrayList<String> Objects = new ArrayList<String>(); while (s.hasNext()){ Objects.add(s.next()); } s.close();
Any suggestions would be appreciated!![]()
Well I solved my previous issue, but now I wonder, why is type casting even a thing? My program worked once I just declared every single variable and function to double. Type casting just seems like a non issue if you just save all your data types to the same. Maybe this issue arises once you HAVE to use different data types? Also when variable allocation size starts becoming an issue?
type error
five bananas
I have to create a program where it reads off from a text file that contains:
Code:Circle 1 Circle red true 1 Rectangle 10 20 Rectangle blue true 1 5
The first string is the object type (Circle or Rectangle) then it either follows stating the radius or the color of the object type, then if it is filled.
ex,
Rectangle [object type], blue [color], true [is filled], 1 [height], 5 [width].
Since this text has many different variable types, how would I go around adding this into an ArrayList?
I have this:
but I know it is wrong since they aren't all Strings.Code:Scanner s = new Scanner(new File("mydata.txt")); ArrayList<String> Objects = new ArrayList<String>(); while (s.hasNext()){ Objects.add(s.next()); } s.close();
Any suggestions would be appreciated!![]()
Well I solved my previous issue, but now I wonder, why is type casting even a thing? My program worked once I just declared every single variable and function to double. Type casting just seems like a non issue if you just save all your data types to the same. Maybe this issue arises once you HAVE to use different data types? Also when variable allocation size starts becoming an issue?
for (var i:int = 0; foo.numElements; i++){
var bar:Object = foo.getElementAt(i);
if (bar.thing == current.thing){
..
}
..
Well, if you have a good way of uniquely identifying the tabs (are these browser tabs? URL would work), putting them in a dictionary would give you a faster way of seeing whether a tab already exists.Kinda general question:
I have a list of elements (tabs). When I open a new tab, I want to check if that tab already exists. If it exists, I change the view to that tab. If it doesn't, I open up the new tab.
What is the best way to do this? Currently I am basically iterating through the elements with
Code:for (var i:int = 0; foo.numElements; i++){ var bar:Object = foo.getElementAt(i); if (bar.thing == current.thing){ .. } ..
Is there a better way?
Well, if you have a good way of uniquely identifying the tabs (are these browser tabs? URL would work), putting them in a dictionary would give you a faster way of seeing whether a tab already exists.
voide removeHead( Node **head){
Node *temp = (*head)->next;
delete *head;
*head = temp;
bool remove(element *elem){
...
...
...
head = elem->next;
delete elem;
In the first case, I don't think the double pointer is needed at all. You would use a double pointer if you wanted to set the original pointer passed in to NULL, but your code sample does not do that.Can someone with a good handle on C pointers explain this to me?
I have a bunch of examples of a linked list implementation. I don't understand why you have to pass a pointer to the pointer into the functions, like this one:
Code:voide removeHead( Node **head){ Node *temp = (*head)->next; delete *head; *head = temp;
but the beginning of another function goes like this
Code:bool remove(element *elem){ ... ... ... head = elem->next; delete elem;
Why can we directly reference head in one but not the other? Also, when is -> notation and when is dot notation used?
Thanks!
In the first case, I don't think the double pointer is needed at all. You would use a double pointer if you wanted to set the original pointer passed in to NULL, but your code sample does not do that.
-> notation is used to access struct or object members on a pointer to an object, like "pObject->_someVariable".
. notation is used to access struct or object members on a object, like "myObject._someVariable".
<!DOCTYPE html>
<head>
<title>Login Page</title>
</head>
<body bgcolor="#FFE4C4">
<h1>Login</h1>
<form action=”login.php” method=”POST”>
Username<input type=”text” name=”user”>
Password<input type=”password” name=”pass”>
<input type=”submit” value=”Login”>
</form>
</body>
You're not using standard " characters.
You're not using standard " characters.
Can someone with a good handle on C pointers explain this to me?
I have a bunch of examples of a linked list implementation. I don't understand why you have to pass a pointer to the pointer into the functions, like this one:
Why can we directly reference head in one but not the other? Also, when is -> notation and when is dot notation used?
Thanks!
In the first case, I don't think the double pointer is needed at all. You would use a double pointer if you wanted to set the original pointer passed in to NULL, but your code sample does not do that.
-> notation is used to access struct or object members on a pointer to an object, like "pObject->_someVariable".
. notation is used to access struct or object members on a object, like "myObject._someVariable".
At the opposite end of the spectrum, I actually wish more people understood the really core low-level kind of coding. Not big, complex stuff like the lockless name lookup, but simply good use of pointers-to-pointers etc. For example, I've seen too many people who delete a singly-linked list entry by keeping track of the "prev" entry, and then to delete the entry, doing something like
if (prev)
prev->next = entry->next;
else
list_head = entry->next;
and whenever I see code like that, I just go "This person doesn't understand pointers". And it's sadly quite common.
People who understand pointers just use a "pointer to the entry pointer", and initialize that with the address of the list_head. And then as they traverse the list, they can remove the entry without using any conditionals, by just doing a "*pp = entry->next".
So there's lots of pride in doing the small details right. It may not be big and important code, but I do like seeing code where people really thought about the details, and clearly also were thinking about the compiler being able to generate efficient code (rather than hoping that the compiler is so smart that it can make efficient code *despite* the state of the original source code).
Well each pointer is just a number, say a 32-bit value for example (may depend on the operating system and machine). Take some contrived examples:Ah thanks. The code examples are straight from a programming interview book. It says that updating *head will do nothing if we pass *head into the function because c passes by value or something.
void bad_set_to_null( Robot* pSomeRobot )
{
pSomeRobot = NULL;
}
void good_set_to_null( Robot** pSomeRobot )
{
if(NULL != pSomeRobot)
{
*pSomeRobot = NULL;
}
}
Robot aRobotObject;
Robot* pRobotPointer = &aRobotObject;
bad_set_to_null(pRobotPointer);
// pRobotPointer is not NULL at this point.
good_set_to_null(&pRobotPointer);
// pRobotPointer is now NULL.
#include <cstdio>
#include <cstring>
class United { // be careful some functions have multiple statements on 1 line!
char x[6];
long y;
public:
United( ) { y=6; strcpy(x, "H sox"); }
United(const char a[ ], long b) {
y=b; strcpy(x, a); printf("United... %ld\n", y);
}
~United( ) { printf("%s", x); }
int change(United *); // be careful, this function accepts an object by address!
void change( ) { x[y] -= 2; y--; }
int operator!( ) { y--; return y; }
};
int United::change(United *p) {
if(p->y) {
p->x[p->y] += 1;
}
if(p->y < 5 && p->y >= 0 && p->y != 1) {
x[p->y] -= 3;
}
printf("%s ... %s\n", p->x, x);
int temp = p->y;
p->y--;
return 1 + temp;
}
int main( ) {
United many(" tmtl", 4);
United one("tkdwu", 4);
United dollar;
printf("You call this an election?\n");
while(!dollar) { // be careful here, the ! is an operator overload
if(dollar.change(&many) != 0) {
one.change( );
}
}
return 0;
}
United::change(United *p)
<string name="blah">\
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a nisi risus, in
imperdiet lacus. Quisque diam sapien, tristique eget
elementum at, laoreet in sem.</string>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a nisi risus, in
imperdiet lacus. Quisque diam sapien, tristique eget
elementum at, laoreet in sem.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a nisi risus, in
imperdiet lacus. Quisque diam sapien, tristique eget
elementum at, laoreet in sem.
I'd argue that what Linus says isn't quite fair -- beyond knowing how to use pointers, there's another insight that makes it work.
Very well put.I'd argue that what Linus says isn't quite fair -- beyond knowing how to use pointers, there's another insight that makes it work.
That insight is that assigning the ** pointer to the next element is iteration, and assigning the * pointer to the next element is deletion. Or put another way, deleting linked list elements is just an iteration gone intentionally awry.
For C: Does anyone know if you can return a double value from a int type function? Either this is possible or there is a typo in the assignment because I just can't figure this out.
For C: Does anyone know if you can return a double value from a int type function? Either this is possible or there is a typo in the assignment because I just can't figure this out.
int f() {
double *d = malloc(sizeof(double));
*d = [value here];
return (int)d;
}
Post the assignment?
Maybe you can return the adress of a double value?
I've noticed that, too. I'm not sure why it adds a space/tab at the start when you use a new line (\n).I'm having a problem with the text formatting in Android SDK.
Source code
Expected outputCode:<string name="blah">\ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a nisi risus, in imperdiet lacus. Quisque diam sapien, tristique eget elementum at, laoreet in sem.</string>
Actual outputCode:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a nisi risus, in imperdiet lacus. Quisque diam sapien, tristique eget elementum at, laoreet in sem.
The backslash is suppose to remove an extra blank on the first line, "Lorem ipsum dolor sit am...". It doesn't do that.Code:Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a nisi risus, in imperdiet lacus. Quisque diam sapien, tristique eget elementum at, laoreet in sem.
Well each pointer is just a number, say a 32-bit value for example (may depend on the operating system and machine). Take some contrived examples:
Code:void bad_set_to_null( Robot* pSomeRobot ) { pSomeRobot = NULL; } void good_set_to_null( Robot** pSomeRobot ) { if(NULL != pSomeRobot) { *pSomeRobot = NULL; } }
Now take some code to actually call those functions:
Code:Robot aRobotObject; Robot* pRobotPointer = &aRobotObject; bad_set_to_null(pRobotPointer); // pRobotPointer is not NULL at this point. good_set_to_null(&pRobotPointer); // pRobotPointer is now NULL.
The call to bad_set_to_null passes the VALUE of pRobotPointer, which is just a number, a memory address. The bad_set_to_null function gets a local parameter called pSomeRobot, which contains the VALUE...the memory address. It then sets the value of pSomeRobot to NULL, but since it is a local variable, it has nothing to do with pRobotPointer back in the main code.
In comparison, the call to good_set_to_null passes another VALUE to a function...but this value is the memory address of pRobotPointer. The & operator means "address of".
So, the good_set_to_null function receives a VALUE that tells where the original pRobotPointer pointer is stored, which means that good_set_to_null can use * to access what is at that memory address: pRobotPointer. This all means that good_set_to_null can directedly modify the value of the original pRobotPointer and set it to NULL.
Hopefully some of that made sense and wasn't just an excessively complicated explanation.
*edit* I really dislike the style, readability, and comprehensibility of that two-star programming article, though.![]()
ok fellow programming GAF, what's a good desktop keyboard for programming?
I have a Sony Vaio laptop and the keyboard is just amazing to type on but my desktop keyboard just sucks in comparison and therefore I rarely want to code on it because it isn't as smooth as on my laptop.
I'm tempted to buy one that looks like my laptop keyboard but I don't know if the keystrokes are as smooth.
ok fellow programming GAF, what's a good desktop keyboard for programming?
I have a Sony Vaio laptop and the keyboard is just amazing to type on but my desktop keyboard just sucks in comparison and therefore I rarely want to code on it because it isn't as smooth as on my laptop.
But I want a desktop keyboard that replicates the smoothness of my laptop keyboard.
This is the keyboard in question:
![]()
And this is the current desktop keyboard which sucks:
![]()
I'm tempted to buy one that looks like my laptop keyboard but I don't know if the keystrokes are as smooth.
Hello,
can anyone recommend me some sites to start learning java? I found something but it's based on videos and covers only basics so if you know some other sites i would appreciate it. And yeah i prefer text instead of only videos because it's faster and easier to follow. Thanks in advance