Okay sorry but I'm not quite sure what you're doing here. I can go into the debugger(which I've never used) by typing gdb into the terminal and then from there what exactly do I do? I tried file ./a.out but it just said no debugging symbols found.BriareosGAF said:Your implementation is undefined based on the internal precision representation of the machine. If you run in a debugger you can inspect the program like so:
(gdb) p bWidth
$20 = 1.01
(gdb) p bWidth*i
$21 = 9.0899999999999999
(gdb) p bWidth*(i+1)
$22 = 10.1
On my machine I get 0 entries in bin number 9, as you're using < and not <=, or more correctly perhaps, an epsilon test.
[michael@namaste tmp] g++ -g -Wall foo.cppExuro said:Okay sorry but I'm not quite sure what you're doing here. I can go into the debugger(which I've never used) by typing gdb into the terminal and then from there what exactly do I do? I tried file ./a.out but it just said no debugging symbols found.
Its a member function named "number". Everything to the left of "number" is the return type, so in this case "int" will be returned from the function. Everything in the "()" are the parameters to the function, in this case there are none. The "const" to the right of the "()" indicates that the function does not change any of the class's data when you call the function, usually this indicates the function is an accessor to data.jokkir said:When a class contains this "int number() const" what does it do? I mean, what values are passed to it and the difference between "const int number()"?
Thanks, I kinda suck at this right now >__<
jokkir said:When a class contains this "int number() const" what does it do? I mean, what values are passed to it and the difference between "const int number()"?
Thanks, I kinda suck at this right now >__<
int primeCalc (int input, int &remainder)
{
int count;
count = input;
if (count > 2 && count <= input)
{
remainder = input%count;
count--;
}
cout << remainder <<endl;
return remainder;
}
Notrollious said:I can't figure out why this little guy either outputs 0 or some random negative string of numbers.
Code:int primeCalc (int input, int &remainder) { int count; count = input; if (count > 2 && count <= input) { remainder = input%count; count--; } cout << remainder <<endl; return remainder; }
Gotta take an input of a number, and determine whether or not it's prime. I for the life of me can't find my mistake.
None of this is correct.wolfmat said:&remainder is the literal address of the variable remainder in memory (something like 0x4F931120). To get the value of remainder, or to change it, you'd have to dereference &remainder (meaning, use the address to access the value). Which is kind of crazy here! You might therefore want to change the signature.
Get rid of "int &remainder" in the signature.
In the first two lines of the function:
Declare remainder as int.
Give it a value of 0.
int count;
count = input;
if (count > 2 && count <= input)
Whoops, you're right, I mangled C stuff together with by-reference somehow. Not sure what I was thinking there.Lathentar said:None of this is correct.
#include <iostream>
#include <cmath>
using namespace std;
//Function to get the integer from the user.
void getInput (int& input);
//Function that calculates the remainder from the given input.
//Returns: the remainder
int primeCalc (int& input, int& remainder);
//Function to display whether it is a prime number or not.
void displayPrime (int remainder);
int main()
{
int input = 0, remainder = 0;
//Function calls.
getInput (input);
primeCalc(input, remainder);
displayPrime(remainder);
return 0;
}
void getInput(int& input)
{
cout << "Please enter an integer greater than 1: ";
cin >> input;
//Preventing the user from entering anything less than or equal to 1.
if (input <= 1)
{
cout <<"You have entered an invalid integer, program terminating." <<endl;
exit (1);
}
}
int primeCalc (int& input, int& remainder)
{
int count;
count = 2;
do
{
remainder = input%count;
count++;
}
while ((count > 2 && count < input) && remainder != 0);
return remainder;
}
void displayPrime (int remainder)
{
if (remainder == 0)
cout << "The integer is a composite number." <<endl;
else
cout << "The integer is a prime number. " <<endl;
}
Notrollious said:I have everything set up but I keep getting 0 for the remainder, no matter what. I think it's something wrong with how I'm setting up my equation.
int count;
count = input;
....
remainder = input%count;
Margalis said:Edit: Now looking at your solution. Comments:
You only need to loop up to 1/2 the number, no integer is divisible by a number that's more than it's half.
Returning the remainder is a little weird. In plain english what is it that this function does? The best way to program (IMO) is to write functions that are very clear in what they do and piece them together. When people run into problems it's almost always because they have functions that are not quite clear in what they do and then they piece them together and the picture becomes even murkier.I totally understand what you're saying. It was one of my left overs from a test I had, when I was trying to determine what was wrong. I think I should just do return 0; there, right?
You really should not be passing by reference, especially not in a simple function that has no obvious need to change the things being passed in.
You are passing both params by reference even though you don't actually change one and you return the other. There isn't even a performance benefit here as the integer value and the reference are going to be the same size. (And if you wanted a performance gain you should use a const ref)
Well, the issue I ran into was the fact that no matter what, the function kept telling me that every number was composite (r=0). referencing the remainder helped solve my problem.
//Function that calculates the remainder from the given input.
//Returns: the remainder
What does this mean? There is no such thing as a remainder for a number, remainder is an operation that requires two inputs. It's not clear at all what the "remainder" here actually is, and it only seems to matter if it is zero or not.
Me being sloppy. >_>
I changed it to read "Returns: true if remainder >=1, false if remainder = 0", something along those lines, where it's clear what the function is doing.
std::vector<Polygon*> polygons;
std::vector<Triangle> triangles;
triangles.push_back(Triangle(p0, p1, p2));
polygons.push_back(new Triangle(p0, p1, p2));
Triangle::Triangle(const Point& p0, const Point& p1, const Point& p2) {
m_points = new Point[3];
m_points[0] = p0;
m_points[1] = p1;
m_points[2] = p2;
}
barnone said:the constructor for my Triangle class looks like this:
Code:Triangle::Triangle(const Point& p0, const Point& p1, const Point& p2) { m_points = new Point[3]; m_points[0] = p0; m_points[1] = p1; m_points[2] = p2; }
Point m_points[3];
// not
Point *m_points;
maharg said:Just don't make m_points a pointer.
Code:Point m_points[3]; // not Point *m_points;
You're probably leaking like a sieve doing that. And it's 'shallow' because it's copying the pointer, not the points object itself.
With GenreComboBox1
If .SelectedIndex <> -1 Then
.Items.RemoveAt(.SelectedIndex)
DialogResponseResult = MessageBox.Show("Remove the selected genre?", "Remove genre", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
Else
MessageBox.Show("Select the genre to be removed", _
"No selection made", MessageBoxButtons.OK, _
MessageBoxIcon.Exclamation)
End If
End With
Acullis said:I've only taken one or two programming classes, I understand the basics, but can someone explain something for me?
My friend and I are working on a program to play card games. We're currently figuring out how we can define a deck and shuffle it. Currently our plan is to represent a deck with a 2d array that contains order information and a cardid.
string deck[][];
a visual example of what we're trying to accomplish would be something like:
order | cardid
[0] 4 | 358
[1] 1 | 38
[2] 5 | 14
[3] 3 | 70
[4] 2 | 54
We want the order to be separate from the index, is that necessary? It seems like it would be easier to manage the cards order' this way.
Also, when defining a function to have a 2d array passed to it, why is it necessary that the second dimension have its size defined?
For example
void someFunction(int args[][]);
results in an error, whereas
void someFunction(int args[][5]);
works perfectly.
Well this was my original thought process:cpp_is_king said:Seems like the opposite to me. Order should be the index if you want it to be simple to manage the order.
Acullis said:Well this was my original thought process:
If we can change the order but leave the index alone that'll save us a lot of hassle dealing with messy arrays. Our function that will eventually list the deck will just sort based on the "order column" - but that ends up being almost as messy as reordering the cards based on index every time we shuffle.
I don't know which way ends up being cleaner :/
for (int i=0; i <= 51; ++i)
{
//pick a random index in the array after this index, up to and including index 51
int other = generateRandomNumber(i+1, 51);
swap(deck[i], deck[other]);
}
cpp_is_king said:I guess I don't see the problem.
1 2
3 4
5 6
7 8
9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
barnone said:Can anyone explain to me the meaning of the argument for this Qt QObject constructor?
QObject ( QObject * parent = 0 )
Also, why must a class derived from QObject contain a QObject* pointer argument in its own constructor? EDIT: Does it have to store this pointer as a private data member, and if so what is this pointer commonly used for?
cpp_is_king said:I've never used Qt before, but I can probably answer it anyway. Windowing systems typically represent things in a hierarchical structure. For example, when you reply to a message on this forum, the white box where you type your message is most likely a "child" of the larger grey box that also contains buttons and stuff you can use to control formatting. That grey box, in turn, is likely a child of the larger rendering area that your browser draws web pages in, which is in turn a child of the entire browser application.
Of course, windows might contain other children besides just other windows. there might be internal objects that are not visible to the user but are basically implementation details, but by being "associated" with a certain object or window, they are essentially children.
Often, these objects need to talk to their "parents" to let them know about things that are going on, or to access certain properties or information controlled by their parents.
The parent parameter to the constructor simply creates the hierarchy. You are essentially saying that the object you're creating is a child of whatever you pass into the constructor. If you pass NULL, you are saying it is a root-level object.
That's about the best answer I can give without a more specific scenario in which I could go further and explain why you would actually want a parent/child relationship
barnone said:Does storing the pointer to the parent class (as a private data member) just keep the structure of the hierarchy apparent to the user? I am just starting using Qt, and I am curious as to what operations people actually perform on this data member.
Class InsuranceCompany
{
InsuranceCompany()
}
Class InsurancePolicy
{
Private InsuranceCompany c;
InsurancePolicy(InsuranceCompany c)
{
this.c = c;
}
}
barnone said:Thanks! That makes a lot of sense.
Does storing the pointer to the parent class (as a private data member) just keep the structure of the hierarchy apparent to the user? I am just starting using Qt, and I am curious as to what operations people actually perform on this data member.
In Qt, the window would connect a slot that changes its colour to the button's pressed (or whatever) signal. I'd imagine there are situations where it would be useful to have a pointer to the object's parent, though.cpp_is_king said:That's just one use. But suppose you had a button that was part of another window, and the button was labeled "set window color to red". when you click the button, the parent has to change its background to red. This is another situation where you need to be able to get the parent of an object.
Wichu said:In Qt, the window would connect a slot that changes its colour to the button's pressed (or whatever) signal. I'd imagine there are situations where it would be useful to have a pointer to the object's parent, though.
The main purpose of the parent on a QObject is memory management: when a parent is deleted, it destroys all its children. There's also some stuff you can do by traversing the children of a QObject. You do not have to provide a parent if you don't want to. QObjects work fine without them.barnone said:Can anyone explain to me the meaning of the argument for this Qt QObject constructor?
QObject ( QObject * parent = 0 )
Also, why must a class derived from QObject contain a QObject* pointer argument in its own constructor? EDIT: Does it have to store this pointer as a private data member, and if so what is this pointer commonly used for?