<cracking knuckles>
Ok, let's dig into this.

[QUOTE=prvnkmr449;1328943]As I understand your problem is to minimize this highly nested loop which print the value of 12 variable.
[/quote]
You should note that the program calls two functions [icode]a()[/icode] and [icode]b()[/icode]. So, it is doing more than just printing out the values of variables. At any rate, yes, this seems to be the main push of the program.

[QUOTE=prvnkmr449;1328943]
Here is my very short program for that but very deep logic is behind that please try to understand the logic behind this first take look too code
[/quote]

  1. Program isn't that short
  2. Logic isn't that deep
  3. Maybe you should present your algorithm in clearer terms instead of slapping the reader with convoluted pure C code and expecting them to follow

[CODE]
long double n_digit= 5;//0,1,2
long double n_var =4;//x,y,z
long double dif_com =pow( n_digit,n_var);
[/code]
When I got here, I knew we were in for a doozy. I notice that you are calculating the number all possible combination of n-digit numbers in a b-based numbering system. First of all, this is not necessary. Secondly, it is wrong. Let's investigate why

First look at a section of the OP's code:
[code]
for(int i=5;i>0;i--)
{
for(int j=5;j>0;j--)
{
if(j<=i){
[/code]

We can see clearly that the nested logic within the for loop iterating j is only executed [I]when j is less than or equal to i[/I]. This logic is repeated for each loop. Obviously, then, we aren't interested in [B]every possible[/B] ...

Akill10 commented: well said! +1

I forgot to mention that you use the above code for your application by subsituting an array (or vector, etc) of ints for the loop control variables. So:

[icode]

i j k ... t
| | | |
v v v v
[ 4 4 4 ... 2 ]
[/icode]

@firstPerson
That is a great idea for engineering students. I don't know if I'm convinced that it's appropriate for CS students ( because I friggen' hate matlab ).

I really think a dynamic programming language should be taught in CS I. This way, the basics of programming can be clearly taught without all the clutter of low-level maintenance to sour students to the art. Being a fan, I think Python would be a great language for anyone to cut their programming teeth on.

What it appears to me is that you have a specialized base-5 numbering system. It is specialized in that it has rules that dictate how you count down in this system. You could make this system very extensible and have it work with an arbitrary number of digits and arbitrary numerical base. The best way to do this is to develop some rules:

  • A number in this system may have an arbitrary numerical base b
  • A number in this system may have an arbitrary number of digits n
  • No digit at index i in the number may be larger than the digit at index i-1

I have implemented a system like this before. I had to create my own specialized numbering system for this problem: [url]http://www.daniweb.com/forums/thread280248.html[/url]
Your challenge is similar.

Try implementing something like this:

[icode]
b = 5 # 5 is arbitrarily chosen. this could be any number
n = 24 # again, 24 is arbitrary
num = array[n]
for i from 0 to n:
num[i] = b - 1 # Set all numerals to b-1

while num[0] > 0: # Terminate when the leftmost digit is 0.
[COLOR="Green"]do something with num[/COLOR] # All the rest of the code is counting down
j = n - 1
num[j] -= 1 # Decrement the right-most digit
while num[j] < 0: # If the digit goes below 0, you have to decrement the digit to the left
j -= 1
num[j] -= 1
while j < n - ...

@OP

I see no reason for the Die object to store the result of its roll. There is nothing wrong with this idea. However, it seems that you will always first roll the die and then fetch its value. This could be accomplished in one step by having the roll() function return the result of the roll. You could still store the result, if you really wanted to, and fetch it again later with the getFace() function:

[code]
int Die::Roll()
{
face = rand() % 6 + 1;
return face;
}

int Die::getFace()
{
return face;
}
[/code]

if you don't need to store the result (i.e. you will use it right away):
[code]
int Die::Roll()
{
return rand() % 6 + 1;
}
[/code]

The [icode]DiceRoll::rollDice()[/icode] and [icode]DiceRoll::getRollFaces()[/icode] could be consolidated in the same way.

Fbody commented: Both good points, but please see response. +2

[QUOTE=LordNemrod;1326996]Die::Die() //Initializes Face data member
{
Face = 1,2,3,4,5,6;
}

I am not sure what you wanted to achieve by this, but this assigns 6 to Face. The operator comma works like this - it evaluates all of its arguments from left to right then returns the rightmost argument, in this case 6. So, this is one thing.[/QUOTE]

This is not quite correct. The comma operator ( [url]http://msdn.microsoft.com/en-us/library/zs06xbxh%28VS.80%29.aspx[/url] -> please forgive the microsoft link. It seemed to be the most informative that I could find quickly ) is indeed evaluated from left to right. It also returns the right-most argument. [I]However[/I], you need to remember to check operator precedence. The comma operator allows two statements to be processed in one line of code. It gurantees that the left statement is completed from before the right, and that all the side-effects of the left statement will influence the right statement.

In this case, the assignment operator has a higher precedence than the comma operator, so it is evaluated first. So, Face is set to 1; The following statements are constant integer expressions that have no effect. The line does indeed return 6, but nothing is done with that value, so it is ignored.

Now, if the line read:
[code]
Face = ( Face = 1,2,3,4,5,6 );
[/code]
Face would first be assigned to 1, then all of the constant expressions would be evaluated, then 6 would be returned from the last comma operator and Face would be set to this value. ...

@firstPerson & Fbody

I couldn't agree more. However, I have known many professors ( teachers and colleagues ) that recommend against using the STL even outside/after the classroom. I think I has a lot to do with the stubbornness of C programmers to accept a new paradigm.

Usually, Computer Science programs progress something like:
[icode]
Computer Science I - Introduction to Computer Science (required of engineers also)
Computer Science II - Language specific tools and basic algorithms
Data Structures - Study of data composition, iterative processing, and intermediate algorithms
[/icode]

I think it is quite unfortunate that in CS I professors put introductory students through the pain of managing arrays. I work with engineers and scientists that have their entire programming knowledge based upon one college class. Every time I see them declare a static array of structs 1000 items deep, I rue the day their professor told them to use this in CS I:

[code]
myStruct myArray[1000];
int myArrayCounter = 0;
[/code]

[QUOTE=nizbit;1325886]My school for a lot of reasons frowns on the STL. In fact they barely teach it. I understand some of their concerns, but at the same time the STL doesn't have cooties :D.[/QUOTE]

This is, unfortunately, quite common. I really don't understand this mentality. The STL is a standard library, so it ships with all c++ compilers. It is a stable library, and, if you use it as it was intended (i.e. don't use it with threads), it is a very useful and powerful tool.

Sadly, a lot of professors of Computer Science still cling desperately to a lot of conventions [I]they[/I] studied as students of the C programming language. In those days, you spent a lot of time and effort managing memory and array sizes.

There are many times when the STL is not appropriate for the task. However, for most general purpose container needs, the STL is great. I'm a feisty person, so I would question my professor as to why, specifically, he doesn't teach the STL.

Here are a few neat points about STL containers:

  1. STL vectors have the same time complexity as arrays for random access.
  2. STL lists have the same time complexity for insertion/deletion as a self-implemented linked list.
  3. STL is a standard with historic stability.
  4. STL containers use dynamic memory allocation and de-allocation internally.
  5. STL containers are easy to use and richly featured.
  6. STL containers use a common interface, so most containers may be traversed using common ...

[QUOTE=arkoenig;1322869]I still disagree.

If you make the equality operator a member, it is somewhere between difficult and impossible in general to avoid situations in which a==b compiles but b==a doesn't.[/QUOTE]

Making it a non-member destroys inheritance. If you want to virtualize equality operators, they need to be members. In regards to the == symmetry. I generally don't support writing equality functions that take differently typed arguments for the left and right side. Semantically, an object of type A should never be equivalent to an object of type B. If the equality of underlying data needs to be tested, or if you need to test [I]functional[/I] equivalence, you should use explicit conversion/extraction functions in your equality logical expression. Alternatively, you could provide implicit conversion by specialized constructors.

[icode]
IntegerObject A( 3 );
RealObject B( 3.0 );

A == B;    // Should only be valid if IntegerObject has
           // a constructor from a RealObject.  otherwise

A == B.toInt();  // I prefer this approach.  Explicitness is nice, and
                 // there is symmetry in the equivalence arguments

[/icode]

My main point here is that the underlying semantics of an equivalence test expect that the [I]type[/I] of left and right side arguments must be the same. I.E. An apple cannot equal an orange, even if they are the same size.

Hmmm.... I see the problem. How asinine to make the bitset size controlled by a template parameter.

I think you could go down two roads.

First, you could store your bits in the string exclusively and return a specific bitset when asked:

[code]
template <int bsize>
bitset<bsize> getBits()
{
return bitset<bsize> bits( s );
}
[/code]

I would personally favor using a [B]vector<bool>[/B]. Of course, you'll have to implement your own logic. This would provide all the extensibility you would need.

I think the best way to achieve this is to wrap the functionality of a bitset with custom functions.

So, to extend your bitset:
[code]
class TryBits
{
// whatever other functionality
public:
extend( const string& ns )
{
// insert a check for ns to be a valid bit string.
// i.e. only 1s and 0s
s += ns;
b = bitset< s.size() >( s );
}
append( bool nbit )
{
bitset< b.count() + 1 > nb;
for( int i=0; i<b.count(); i++ )
nb[i] = b[i];
nb[ b.count() ] = nbit;
b = nb;
}
void remove( int index )
{
// check boundary conditions
bitset< b.count() - 1 > nb;
for( int i=0, j=0; i<b.count(); i++, j++ )
{
if( i == index )
i++;
nb[j] = b[i];
}
b = nb;
}
};
[/code]

I haven't tested any of this, but I think you could proceed with this kind of an idea. You would probably want to add in a [iCODE]setSize( int n )[/iCODE] function as well.

[QUOTE=arkoenig;1322554]I hadn't noticed it before, but in general, defining symmetrical operators such as == as members is the road to madness. If you want them to have virtual properties, you should do something like this:

[code]
bool operator==(const Foo& a, const Foo& b)
{
return a.equal(b);
}
[/code]
and now you can define [icode]equal[/icode] to do whatever typechecking you need on the type of [icode]b[/icode] (noting that the type of [icode]a[/icode] already determines which version of [icode]equal[/icode] will be called).[/QUOTE]

I partly agree.

I think the equality operator should be a [I]member[/I] function of the class it will be operating on. Otherwise, it will have to be a friend function, which I avoid unless necessary. Furthermore, by making it a member function, you may also make it virtual, which I recommend.

Now, let's talk about the part I agree with! I also support making explicit, non operator member functions in the class that are used by the operators. I generally think of operator overloading as a convenience for any segment of code that externally manipulates an object. Within the class, however, I prefer using explicit functions.

Here's an example of how I typically implement this sort of functionality:

[code]
class Base
{
private:
int b;
public:
Base() : b(0){}
Base( int b ) : b(b){}
virtual ~Base(){}

virtual bool equal( const Base& other ) const
{
    return b == other.b;
}
virtual bool operator==( const Base& other ) const
{
    return equal( other );
}

};

class Derived
{
private: ...

[QUOTE=frogboy77;1321061]seems to give correct answer but just looks wrong to me[/QUOTE]

It is wrong. GCD algorithm is looking for [I]multiplicative[/I] factors. So, subtraction is not the right method here. If you are getting correct results, I think it is coincidental. Read my first response. You need to use modulus.

You are returning the result of the function call. This is basically [I]forwarding[/I] the result of the function at a lower level to the call at a higher level.

Think about this simple recursive function:

[icode]
function sum_dum( number ):
if number == 0: # This is the base case
return 0
else:
return 1 + sum_dum( number -1 ) # This is the recursive call
[/icode]

Now, let's see how it works:

[icode]

main program scope:

number = 4
print sum_dum( number ) -> passes off to number to first recursion level

first recursion level

number == 4
number != 0
return 1 + result of second recursion level on number -1

second recursion level

number == 3
number != 0
return 1 + result of third recursion level on number -1

third recursion level

number == 2
number != 0
return 1 + result of fourth recursion level on number -1

fourth recursion level

number == 1
number != 0
return 1 + result of fifth recursion level on number -1

fifth recursion level

number == 0 <----- Aha! The base case
return 0

fourth recursion level

return 1 + 0 <- result of fifth level

third recursion level

return 1 + 1 <- result of fourth level

second recursion level

return 1 + 2 <- result of third level

first recursion level

return 1 + 3 <- result ...

You have a few problems:

  1. This is a recusion problem, but you are not recursing correctly:
    [code]
    int g_c_d(int a,int b)
    {
    int c;
    c=abs(b-a);
    cout<<a<<" "<<b<<" "<<c<<endl;/put this in to check it was going ok/
    if(c==0){return a;}
    if(b>a){b=a;}
    a=c;
    g_c_d(a,b); [B][COLOR="Red"]// This value should be returned[/COLOR][/B]
    }[/code]

  2. You aren't implementing the algorithm correctly. This algorithm is looking for multiplicative factors, so you will need to do some division (by modulus):

This is the recursive Euclidean Algorithm for GCD in pseudocode:
[icode]
function gcd( a, b ):
if b = 0
return a
else
return gcd( b, a mod b )
[/icode]

Try to implement this, and let us know how it goes.

[QUOTE=tennis;1320858]say iter1 and iter2 are both iterators

[CODE]iter1++=iter2++;[/CODE]

what does this line means? Can anybody help explain? ++ and * which is done first?
thanks[/QUOTE]

[URL="

Yes, this code is broken. First, receive is not declared in the main function. You must first declare an object of type [B]bank[/B]. Secondly, you won't be able to use strcpy to write data directly into the c-string bank::receive unless you make a dangerous assumption. Most compilers place the private members of a class at the very beginning of the objects allocated memory. It is often possible to access this [I]privare[/I] memory by reading or writing data directly from the memory address of the object. I don't think this sort of allocation is in the c++ spec, however, so I think this sort of hack can result in undefined behavior.

I'd like to point out that you can make your class much more user friendly by adding some functionality. First, you could make your bank constructor take a static character array as an argument to instantiate its receive c-string:

[code]
class bank
{
protected:
char receive[20];
public:
bank( const char* recStr ); // Implement this to copy recStr into receive
};
[/code]

Secondly, you could write a function in bank called [B]write[/B] that knows how to write its data to a binary ostream:
[code]
class bank
{
protected:
char receive[20];
public:
bank( const char* recStr ); // Implement this to copy recStr into receive
write( ostream& out ); // Implement this to write receive to the ostream
};
[/code]

I also must make one strong suggestion. Since you are using the c++ [B]Standard Template Library[/B] already by utilizing [B]fstream[/B], you ...

This is unnecessary code:
[CODE]
bool Candidate::operator ==(const Candidate& right) const
{
//return (firstName == right.firstName && lastName == right.lastName);
// Rather than the above, I used this...
return (static_cast<Person>(*this) == static_cast<Person>(right));
}
[/CODE]

If you do not define this operator in your derived Candidate class, the operator function from your base Person class will be used. I think you need to work on the concepts of inheritance a little bit.

Consider this:

[code]

include <iostream>

using namespace std;

class Base
{
int value;
public:
Base( int value ) : value(value){}
bool operator==( const Base& other )
{
return value == other.value;
}
};

class Derived : public Base
{
public:
Derived( int value ) : Base(value){}
};

int main()
{
Base b0( 4 );
Derived d0( 2 * 2 );
Derived d1( 2 + 2 );
cout << ( b0 == d0 ) << endl;
cout << ( b0 == d1 ) << endl;
cout << ( d0 == d1 ) << endl;
return 0;
}
[/code]

If you can understand why this works, you are on your way.

[QUOTE=firstPerson;1320247]Perhaps, one of the faster way to do this is like so :
[code]
template<typename ForwardIterator>
void makeUnique(ForwardIterator begin, ForwardIterator end){
std::sort(begin,end);
std::unique(begin,end);
}
[/code][/QUOTE]

Of course that would be simpler, but this smacks of a homework assignment. If that is the case, the important part is to learn the underlying logic of the algorithm. Thinking along those lines led me to discuss the explicit method.

I am a complete adherent of the STL, however, and, dear OP, if you haven't learned about the c++ standard template library, you should investigate it soon.

oh and i am using dev-c++ so i cant use strings

You should really consider using something besides dev-c++. Code::Blocks is a common suggestion. You should also know that dev-c++ isn't a language or a standard, so, aside from convenience for the classroom, it has no crucial place in the world of c++ programming.

@Narue std::pair is smart about initializing its members
Hmm...true, but I think you should say std::map. This is a bit confusing for the OP.

Also, I would like to emphasize that it is important to declare amount as an int or other numeric type. If you declare it a string because you split a getline() instead of using the istream >> operator, saying report[account] += amount will give you concatenations of the inputs.

[QUOTE=Fbody;1319894]On this site, a "space" or 2 is sufficient indent. A [I]TAB[/I] is too big.[/QUOTE]

Actually, you shouldn't have actual tabs in your code. That is my opinion, and it is right. Code editors use monospace fonts ( at least they should, for the love of Turing ). Tabs are used by word processors to gurantee alignment of [I]non[/I]-monospace fonts. As far as I know, all editors provide an option to replace tabs with some fixed number of spaces. You should use this option. My personal preference is 4 spaces for each indentation level.

Hrmmmmmmm...

  1. Why do you need a [I]screenshot[/I]?????
    I really have to wonder why you would need a screenshot of a running application. I bet it has something to do with my second question:

  2. Is this a homework assignment?
    This is the time of year when computer science classes begin. Perhaps your instructor wants to make sure his students can get their system ready to go on assignments... If this is the case, shame on you sir. Find out what libraries you need and install them.

  3. Show some effort, for the love of Turing.

That is all.

[QUOTE=nizbit;1320079]I'm trying to find duplicates in an array and removing them by shifting the array index using the same array. I can't figure out where in my code the problem is but I'm still getting a duplicate. Could someone please help pinpoint the problem? I've tried debugging it but I'm not seeing it.
[CODE]

include <iostream>

using namespace std;

int removeDuplicates(int A[], int n){
int last = n-1;
int found = 0;
//bool flag = false;

for(int i=0; i < last+1; i++)
{
    for(int j=i+1; j < last+1; j++)
    {
        if(A[i] == A[j])
        {
            found++;
            for(int k = j;k < last+1;k++)
            {
                A[k]=A[k+1];
                //if(A[k-1] == A[i])
                //  flag = true;
            }
            last--;
            j=j+1;
        }

    }

}
//found= last-found;
return last;
}

int main() {

// Test data
int n = 24;
int A[] = {1,1,2,4, 1,1,1,2, 3,5,8,0, 2,-2,-4,1, 9,10,-4,11, 11,8,5,-4};

// Call
int last = removeDuplicates(A,n);

// Print Results
for(int i = 0; i <= last; i++)
    cout << A[i] << " ";
cout << endl;

return 0;

}
[/CODE][/QUOTE]

I see one fundamental problem here. You are shifting your j index [I]even when a duplicate is found[/I]. When you find a duplicate and shift all items, you need to leave the j index in place:

[icode]

  1. Found duplicate

    i j
    | |
    v v
    ABCDEFGHIJEEKLMNO

  2. Shift

    i j v v
    ABCDEFGHIJEEKLMNO
    vvvvvv
       //////
      |||||| 
      vvvvvv 

    ABCDEFGHIJEKLMNO

  3. Incorrect increment of j

    i j
    | |
    v v
    ABCDEFGHIJEKLMNO
    ^
    |_____There is still a duplicate, ...

mrnutty commented: Nice Ascii Art and good explanation. +5

Yes, that is, in fact, what I was trying to say. And, I definitely agree with your second paragraph.

[QUOTE=Narue;1317817]
[B]>If the function may not be implemented in the derived class,

it really shouldn't be defined as a pure virtual function.[/B]
A pure virtual function [i]must[/i] be implemented in the derived class, this is absolute. With destructors the implicit implementation suffices, so while you don't see it in the source, it's still there. I don't know if that alters your opinion of broken semantics or not, but there you go.[/QUOTE]

Hmmm....the generality of the word 'may' may have caused some confusion here. Let me re-word.

If the function [B]might[/B] not be [I]fully[/I] implemented in the derived class, it really shouldn't be defined as a pure virtual function.

@Narue
Because of perverse curiosity, I must know. When and why have you used placement new before? Perhaps a hardware interface?

[QUOTE=Narue;1317680][B]>So in my view, I'm still right[/B]
...[/QUOTE]

Righteous indignation is as heavy a cross to bear as defending an invalid thesis.

@Mike
She got you. You should at least admit it.

@Narue
You may be right, but I don't completely agree. One of the benefits of using pure virtual functions is forcing derived classes to implement them. Though it may be legal, in my mind it breaks the semantics of abstract inheritance. If the function [I]may not be[/I] implemented in the derived class, it really shouldn't be defined as a pure virtual function. I would favor letting the derived class implicitly call the base class non-pure virtual function when the function isn't defined in the derived class. I tend to think that if you are having trouble thinking of a pure virtual function to put in your abstract base class, then perhaps the base class shouldn't actually be abstract.

<brace for the wrath>

[QUOTE=Stony;267875]All I was asking for is a little advice in how to setup my algorithm, I can do the code. I am a straight A student (3.871), I made an A in this class in the summer, just with a lot easier teacher. I have never had to learn about prime numbers. I am more interested in learning about functions, parameters, arrays, structs, classes, etc. I could care less about prime numbers, and how to find all even numbers in existence and have two prime numbers that equal them. Is prime number excellence a part of C++. [/QUOTE]

I understand that you are quite daunted by this homework assignment. Please understand that your professor probably doesn't care whether you love prime numbers or not. Rather, this assignment is a good way for you to

  1. Think about how to use logic to solve a problem
  2. Implement that logic in a program

Learning about the language mechanics is important. However, understanding how to use logical tools to solve a problem is more important. Logic is universal. Language mechanics are rather limited in scope.

You have all the functions. Now, you just need to think about how to combine them to get a working solution. I assure you, this is a very good exercise for an entry level Computer Science student.

[QUOTE=tayyab569;1317718]#include <iostream.h>

include <conio.h>

void main(void)
{
clrscr();
int i,p;
for(i=1;i<=300;i=i+1)
{
for(p=2;p<i;p=p+1)
{
if(i%p==0)
break;
else
continue;
}
if(i==p)
cout<<" Prime:"<<p;
}
getch();
}[/QUOTE]

[COLOR="Red"]What the #&&$*% is this?[/COLOR]

[B]1. [iCODE]void main()[/iCODE] is a great way to start fail code[/B]
Please, for the love of any deity, use int main.

[B]2. The [icode]conio.h[/icode] is a huge, non-standard fail.[/B]
Don't use this. Just don't. It's not standard, it's not cross-platform, and it's
not even very powerful

[B]3. READ THE RULES AND USE CODE TAGS[/B]

[B]4. Plain text in a reply is helpful.[/B]
Perhaps you should describe what your code is for and why the OP should give a crap about it.

[B]5. Read the original post.[/B]
Your solution simply prints [B]all the primes less than 300[/B]. This is trivial, and the OP already knows how to determine if a number is prime. His assignment is not so difficult, but this code is anything but helpful to him.

[B]6. Use less pointless code[/B]
This is painful:
[code]
for(p=2;p<i;p=p+1) // p=p+1? really? really??????
{
if(i%p==0)
break;
else // There is no point at all to using this
continue; // When the if clause fails, of course the loop will continue
}
[/code]

perhaps:
[code]
for( p = 2; p < i && i % p != 0; p++ );
[/code]

[B]7. There are far better ways of doing this[/B]
Even for the brute force method can be improved by stopping your search short:

[code]
for( p = 2; p < i / ...

I'm a huge fan of Qt. I build a lot of Qt applications, and I'm really happy with this GUI. Trolltech, the makers of Qt, also provide a full-featured integrated IDE called [URL="