NP-complete 42 Junior Poster

as far as your code is concerned, no difference at all. Every class implements those two anyway. It depends on what you want to do with Comparable. If you want to make a list of all houses and "sort" them based on the compareTo method then you can perhaps use something like Collections.sort if you explicitly implement Comparable. But in most cases (read simple cases) you don't have to be that explicit.

P.S: Before learning JAVA or any other programming language learn how to use CODE TAGS !!
I couldn't give you a proper answer because i skimmed through your post due to lack of readability.

NP-complete 42 Junior Poster

And for that overdraft method not working....
Its the same problem as is with your constructor. You are NOT passing the correct number of arguments to it.(Madawar actually tried to tell you this only)

Hope it helps.

NP-complete 42 Junior Poster

oh...true, true...that was a bad mistake (or, not a good optimisation)...:)

NP-complete 42 Junior Poster

@nbaztec and others

I think the main loop that checks for primes can be iterated from:

for (int i = 2; i <= n/2; i++) //where n is the number to be checked for

I think it'll be a bit faster.
And also, using break may also reduce unnecessary iterations.

NP-complete 42 Junior Poster

if you don't know already, daniweb only helps people who show at least some efforts. Just saying "help me please" wont help (and that too for homeworks!!)
First try yourself. Post what you are thinking. If you have managed to write any code so far then post that too.
This will be enough to start with...
Good luck...

NP-complete 42 Junior Poster

great....in that case, just mark this thread "solved".

happy coding

NP-complete 42 Junior Poster

>Now i'm too busy...

relax and take some time out to figure out the problem in your code pertaining to "code style".

Read good books and also read about programming style. (just google it up)

And always remember:
"Programming is more about the art of programming"


I may not be the most experienced coder out here, but Ancient Dragon surely is. So if he has labeled your code as "messy" you better take it seriously, unless off course you plan not to code for the industry.

NP-complete 42 Junior Poster

@tendavola:

You must also take a deeper look into what amrith92 posted. his code is generally the way you should do these things. And if you follow, his loops iterates from 0 to
"less than" 4. That is, it effectively goes 0,1,2 and 3.

NP-complete 42 Junior Poster

@amrith
Yep he should include all the three.

His main problem i guess was the out of bounds thing.

The above explanation was for tendavola.

NP-complete 42 Junior Poster

@tendavola
glad that you figured it out.

Explanation:
An array ALWAYS starts from 0. So if you have something like int A[4] the first element will be at A[0]. Then A[1],A[2] and A[3]. Note we have already reached 4 elements as we wanted (0-3) hence A[4] is meaningless and when you try to assign something to A[4] we say that you are going out of bounds.

Hope that helps.

NP-complete 42 Junior Poster

may i know which compiler you are using?
You surely have array out of bounds in line 24.

Also may i know the headers you are including??

NP-complete 42 Junior Poster

Welcome to the 21st century guys...turbo c++ in that sense, is something from the last century which didn't bother to make it to this century (read, didn't bother to update itself). But C++, in all glory has been standardized, i guess, in the last century itself. But Mr. Turbo doesn't even know that!!!

Your code now is indented, but that's not the only "art" in programming. Your code has no single comment.

Here's an open challenge:
can anyone explain the code?? I bet even you (who claims to have written it once upon a time) can't explain.

NP-complete 42 Junior Poster

my pleasure...

NP-complete 42 Junior Poster

okay... i got the point but surely this line:

Now, for any given number, the maximum value of the factor it can have is always less than or equal to its square-root

is wrong.

You have implemented your logic in a different way, and that's why all the confusion.

In my opinion maryam ahmmed's code is slightly better in terms of presentation except that:
1) Its not properly indented :D
2) He uses void main() :@
3) The range of the loop should have been:

for(int x=1; x<=[B]a/2[/B]; x++)

cheers...

NP-complete 42 Junior Poster

How many times do you want DANIWEB to remind you of CODE TAGS?? They are so sick and tired that they have also watermarked "please wrap code in code tags..." in every "post reply" message box. Still you guys can't see. Its a shame.

I also found that you have used comments in a foreign language!!!

Now, how about me giving you some help in Bengali, my mother tongue?

NP-complete 42 Junior Poster

@amrith92:

Now, for any given number, the maximum value of the factor it can have is always less than or equal to its square-root

That's wrong. For e.g. sqrt(16) = 4 but factors of 16 are 2,4,8 (and off course 1).

What you said is true for prime factors (that too, except prime factors of primes) and not for general factors.

NP-complete 42 Junior Poster

well i copied your code and tried to compile it. Looks like, other than line 22 of your code the rest will compile fine if you take jonsca's advice and make the call to pow as

pow(-1.0,i+k)

. It just gives you a warning regarding possible loss of precision.
The second set of errors are definitely not due to a wrong call to pow. Or so I guess.

BTW, even I compiled it using Visual Studio 9.0 (i.e. VS 2008).

NP-complete 42 Junior Poster

well actually <string> includes all the functions and other instances and also overload operators(to enhance functionality) of the string "class". So if you wanna use any of those(which in all probability you will) you HAVE to include <string>.
In the above code as well, you HAVE to #include <string>. Why ?? Because I have used the << and >> operators for string objects and they will not work unless you #include <string>.
So its always a better idea to include <string> as Fbody has already mentioned.

Hope it clarifies
Cheers.

NP-complete 42 Junior Poster

Sorry for the late reply.

Sure...i included string for more flexibility. If you don't want to use that then simply use char instead of string. Just replace all the instances of the word "string" with "char" and remove that #include <string> thingy.
That's it.
Hope it helps.

NP-complete 42 Junior Poster

great. The code though a bit bad in english, is not bad except for that iostream.h thing. for using characters (or strings if you want to have more than 1 character) just replace the ints to strings at appropriate places.

// may 10 2010


#include <iostream>
#include <string>

using namespace std;

const int MAXSTACK=5;
const int TRUE=1;
const int FALSE=0;
// Class Declaration
class Stack
{
private:
	int top;
	string num[MAXSTACK];  //change1
public:
	Stack(); // constructor
	void push(string);
	string pop();        //change2
	int isempty();
	int isfull();
};
// implementation section
Stack::Stack()
{
	top=-1; // initialize the top-of-stack position current position
}
void Stack::push(string value)   //change3
{
	top++; // increment the index in top
	num[top]=value; // store the value
}

string Stack::pop()
{
	string topval;      //change4
	topval=num[top]; // retrieve the top element
	top--; // decrement the index stored in top
	return (topval);
}
int Stack::isempty()
{
	if(top==-1)
	return TRUE;
	else
	return FALSE;
}

int Stack::isfull()
{
	if(top==MAXSTACK-1)
	return TRUE;
	else
	return FALSE;
}
//
int main()
{
	Stack digits; // define a Stack named digits
	string newnum;    //change6, i should have changed the name as well. but     felt too lazy!!
	cout<<"Enter as many letters wish.. \n";
	while(1)
{
	cout<<"Enter a char: ";
	cin>>newnum;
	if(digits.isfull()) //check overflow
{
	cout<<"\nNo more storage allocation space."
	    <<"\nThe last digit has not been entered on the stack.";
	break;
}
	else
	digits.push(newnum); // push value onto the stack
}
// pop and display digits from the stack.
	cout<<"\nThe values popped from the stack are: \n";
	while(!digits.isempty()) // …
NP-complete 42 Junior Poster

What have you done so far?? If you are fine with nos...chars are not that different. Post the code that you have written so far.

NP-complete 42 Junior Poster

Thanks Dave...even i got confused for a moment.

NP-complete 42 Junior Poster

Great. glad that you liked it.

About closing threads, i don't know...i think its upto the admins and mods...lets see if others can clarify.

NP-complete 42 Junior Poster

Hi Sparkle Jam, no offense but i would just like to give you some advice...firstly start writing STANDARD code. In all probability you are using an antique compiler like turbo c++. Dump that right NOW.
Secondly NEVER USE GOTO. Repeat that a 100 times. It makes code-maintenance so very difficult.
Also, I found you were tying to return from your functions by a call to main(). You should generally never call main() as it's the property of the OS and you shouldn't be interfering.
Hope it helps.
Cheers...