Hey men
When i wanna run the code below,i get the famouse don't send message and i use code::blocks 8.02

#include <conio.h>
#include <iostream>
using namespace std;
int main(){
    int start,end;
    double a[start-end][2],p[start-end];
    cout<<"Enter a number to begin with : ";
    cin>>start;
    cout<<"Enter a number to finish with : ";
    cin>>end;
    if(start==end || start>end){
        cout<<"The beginning number must be smaller than the ending one.";}
    else{
        char c='y';
        int sum1=0,sum2=0,ai=0,pi=0;
        do{
            for(register int i=start;i<=end;++i){
                for(register int j=1;j<i;++j) if(i%j==0) sum1+=j;
                for(register int j=1;j<sum1;++j) if(sum1%j==0) sum2+=j;
                if(sum2==i){
                    if(sum1==i){
                        p[pi]=i;
                        ++pi;}
                    else{
                        if(sum1<i){
                            a[ai][0]=sum1;
                            a[ai][1]=i;
                            ++ai;}
                        else if(sum1>i){
                            a[ai][0]=i;
                            a[ai][1]=sum1;
                            ++ai;}}}}
            a[ai][0]=0;
            p[pi]=0;
            cout<<"Amicable pair(s) between "<<start<<" and "<<end<<"\n";
            for(register int i=0;a[i][0]!=0;++i) cout<<"("<<a[i][0]<<","<<a[i][1]<<")";
            cout<<"Perfect number(s) between "<<start<<" and "<<end<<"\n";
            for(register int i=0;p[i]!=0;++i) cout<<p[i]<<",";
            cout<<"\n";
            cout<<"Do you want to continue(y/n) : ";
            cin>>c;}
        while(c=='y');
        return 0;}}
double a[start-end][2],p[start-end];
    cout<<"Enter a number to begin with : ";
    cin>>start;
    cout<<"Enter a number to finish with : ";
    cin>>end;

You are using 'start' and 'end' without being initialized. Give start, and end a value before using them.

if(start==end || start>end)

I think this would work the same. -> if(start >= end )

You should stop using the register keyword all over the place as well.

Modern optimising compilers are far better at figuring out best use of registers, and will just ignore it. So save yourself some typing.

I did as you recommended but now when I run it,after entering the values in the console window,I get the message:

Process returned -1073741571 (0xC00000FD) execution time : 4.046 s

Well, I did not compile your code for the first time, but I did now. So I have discovered another problem, you are trying to allocate an array by saying this: start-end. So what if I gave 1 to start with, and 100 to end with. The result will be start-end => 1-100 => -99. Negative size for your array. - Not good.
Another thing, when I tried to compile it in MS Visualbasic, it gave me errors, since [start-end] is not a constant value. Then I compiled it under Linux, with MonoDevelop, and it was OK, but I am quite sure allocating your array with "new" would be much safer. Are you using Linux or Windows? If you have Windows, you should consider using MS Visualbasic instead of Code::Blocks. It is better in pointing out errors, especially for beginners. Like me. :) Now, I am going to play with your code a bit, and try to fix it.

I changed the array declaration and now the value is not negative but the answer is the same as my last post.
And are you sure you are using Visual Basic?
And thanks

Sorry, my bad. Visual Studio 2008.

And are you sure you are using Visual Basic?
And thanks

Ofcourse not, this is C++ code :-O

Sloppy, you need a better coding style. Look at:

for(int j) if() code
for(int j) if() code

that looked at first like you had a for loop with a for loop with the same variable j being redeclared.

You're going to run into errors rapidly with a style like that.

#include <conio.h>
#include <iostream>
using namespace std;
int main(){
    char c='y';
    int sum1=0,sum2=0,ai=0,pi=0;
    do{
        int start,end;
        cout<<"Enter a number to begin with : ";
        cin>>start;
        cout<<"Enter a number to finish with : ";
        cin>>end;
        if(start>=end){
            cout<<"The beginning number must be smaller than the ending one.";}
        else{
            double a[end-start][2],p[start-end];
            for(int i=start;i<=end;++i){
                for(int j=1;j<i;++j) if(i%j==0) sum1+=j;
                for(int j=1;j<sum1;++j) if(sum1%j==0) sum2+=j;
                if(sum2==i){
                    if(sum1==i){
                        p[pi]=i;
                        ++pi;}
                    else{
                        if(sum1<i){
                            a[ai][0]=sum1;
                            a[ai][1]=i;
                            ++ai;}
                        else if(sum1>i){
                            a[ai][0]=i;
                            a[ai][1]=sum1;
                            ++ai;}}}}
            a[ai][0]=0;
            p[pi]=0;
            cout<<"Amicable pair(s) between "<<start<<" and "<<end<<"\n";
            for(int i=0;a[i][0]!=0;++i) cout<<"("<<a[i][0]<<","<<a[i][1]<<")";
            cout<<"Perfect number(s) between "<<start<<" and "<<end<<"\n";
            for(int i=0;p[i]!=0;++i) cout<<p[i]<<",";
            cout<<"\n";
            cout<<"Do you want to continue(y/n) : ";
            cin>>c;}}
    while(c=='y');
    return 0;}

I tried to fix your code, I managed to compile it without errors, but then the end result was messy. So in the end I got tired of bug hunting and decided to rewrite the whole thing. This is not a very efficient way to find amicable pairs. You should generate them in the desired interval, and then compare them to the given numbers. Anyway it looks like this one works. I hope you can use some of its parts. Good luck, with your code.

P.S.: If you find any bugs, errors please let me know. Thanks.

#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

struct amicable
{
	unsigned int itself, pair;
	bool scope;
};

int main()
{
	unsigned int start, end, tmp_pair = 0;
	amicable tmp = { 0, 0, false };
	
	vector<amicable>numbers;
	
	cout << "Give me the beginning and the ending value of the interval: ";
	cin >> start >> end;
	
	if( start >= end )
	{
		cout << "Invalid interval!" << endl;
		cin.get();
		exit(1);
	}
	
	for( unsigned int i = start; i <= end; i++ )
	{
		tmp.itself = i;
		numbers.push_back( tmp );
	}
	
	for( vector<amicable>::iterator it = numbers.begin(); it < numbers.end(); it++ )
	{	
		for( int i = it->itself-1; i > 0; i-- )
		{
			if( it->itself % i == 0 )
			{
				tmp_pair += i;
			}
		}
		
		it->pair = tmp_pair;
		if( (it->pair <= end) && (it->pair >= start) && (it->pair > 0 ) ) it->scope = true;
		
		tmp_pair = 0;
	}
	
	for( vector<amicable>::iterator it2 = numbers.begin(); it2 < numbers.end(); it2++ )
	{
		if( it2->scope == true && it2->itself != it2->pair )
		{			
			for( vector<amicable>::iterator sub = numbers.begin(); sub < numbers.end(); sub++ )
			{
				if( (it2->pair == sub->itself) && (sub->pair == it2->itself) ) cout << " Heureka I've found one!  " << it2->itself << " - " << sub->itself << endl;
			}
		}
	}

	cout << "Finished...";
	cin.ignore( 1, '\n' );
	cin.get();
	numbers.clear();
	return 0;
}
commented: It's a model of clarity the OP would do well to follow +27

Change the last for loop to this one. This would make it a little bit faster.

for( vector<amicable>::iterator it2 = numbers.begin(); it2 < numbers.end(); it2++ )
	{
		if( it2->scope == true && it2->itself != it2->pair )
		{			
			for( vector<amicable>::iterator sub = numbers.begin(); sub < numbers.end(); sub++ )
			{
				if( it2->pair == sub->itself )
				{
					if(sub->pair == it2->itself) cout << " Heureka I've found one!  " << it2->itself << " - " << sub->itself << endl;
					break;
				}
			}
		}
	}
commented: Well written code, I could follow! +4

jesus its really hard to understand your algorithm.
for example what is this scope thing.
and a problem.maybe its not important but i think you love fixing it.
your code finds every pair four times,two in inverse order from two other and 'cause i didn't understand your algorithm completely,i don't know where is the problem.and could you explain it a little and change it a little to be able to find perfect numbers,too.
thanks alot

AAA.and one more thing.my code says that the sum of 6's divisors except itself is 12 but it is proved that it is 6.(you know 1+2+3=6).
its not only this.you know 6 is a perfect number.when i give 1 and 10 to my program to find amicable pairs and perfect numbers there,it returns nothing and says that the sum of 6's divisors is 12 and 7's is 13.and the sum differs whit the change of interval but when i give it 6 and 7 it returns 6 and 7 as perfect numbers and tells that the sum of 7's divisors is 7 that is wrong.I think i had a messy mistake somewhere but i have no idea where.please tell me if you find it.
Do you know what is it's problem?

jesus its really hard to understand your algorithm.
for example what is this scope thing.

...Right... That scope thing is to determinate if the pair of a number is possibly in the interval. For example lets take the interval of 220-240. The amicable pair of 220 is 284, but because of the shortness of the range, 284 is unreachable. Therefore it is not in the "scope". Scope is set to "false" by default.

your code finds every pair four times,two in inverse order from two other and 'cause i didn't understand your algorithm completely,i don't know where is the problem.and could you explain it a little and change it a little to be able to find perfect numbers,too. thanks alot

No, it does not. It just finds them two times, because one element is the other element's pair and visa versa. It does not register the shown pairs, but I have fixed that... Yes, I will comment my code out for you. And, NO I am not going to modify it to find perfect numbers.

AAA.and one more thing.my code says that the sum of 6's divisors except itself is 12 but it is proved that it is 6.(you know 1+2+3=6).
its not only this.you know 6 is a perfect number...

Well, the first time you did not stated what your program does. I had to assume you wanted to find amicable pairs. Amicable pairs and perfect numbers are not the same. The reason why 6 is not being displayed is because its pair is the number itself. And that is not a valid amicable pair, although it is a perfect number. Remove the line beneath, from the scope line "thing", and 6 will be displayed.

(it->itself != it->pair)

.... think i had a messy mistake somewhere but i have no idea where.please tell me if you find it. Do you know what is it's problem?

No idea, but If I were you I would rewrite the whole thing. By the way you still did not fixed the negative array size issue. Watch p[start-end] closely.

The modified code:

#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

struct amicable //structure
{
	unsigned int itself, pair; // to store the number, and its pair
	bool scope, checked; // "scope" already explained
};                                            // "checked" to remember, previously displayed elements

int main()
{
	unsigned int start, end, tmp_pair = 0; // tmp_pair is a temporary variable
	amicable tmp = { 0, 0, false, false }; //number and pair set 0, scope,checked to false
	
	vector<amicable>numbers; // vector to store numbers
	
	cout << "Amicable pair finder..." << endl << "Give me the beginning and the ending value of the interval!" << endl << "#: ";
	cin >> start >> end;  // reading in 
	
	if( start >= end ) //start must be smaller than end
	{
		cout << "Invalid interval!" << endl;
                cin.ignore(1,'\n');
		cin.get();
		exit(1);
	}
	
	for( unsigned int i = start; i <= end; i++ ) // occupy vector
	{
		tmp.itself = i;
		numbers.push_back( tmp );
	}
	
	for( vector<amicable>::iterator it = numbers.begin(); it < numbers.end(); it++ )
	{	
		for( int i = (it->itself / 2) + 1; i > 0; i-- ) // creating the sum of the divisors
		{
			if( it->itself % i == 0 )
			{
				tmp_pair += i;
			}
		}
		
		it->pair = tmp_pair; // sets pair
		if( (it->pair <= end) && (it->pair >= start) && (it->pair > 0 ) && (it->itself != it->pair) ) it->scope = true;
		// check if the pair is possibly in the range
		tmp_pair = 0; // reset tmp_pair to 0 for the next element
	}
	// outer for loop works similar as bubble sorting, as it walks through the elements
	for( vector<amicable>::iterator it2 = numbers.begin(); it2 < numbers.end(); it2++ )
	{      // if in the scope, and hasn't been checked start looping
		if( it2->scope == true && it2->checked == false )
		{	// inner loop for comparing the rest of the elements to "it2" outer loop		
			for( vector<amicable>::iterator sub = numbers.begin(); sub < numbers.end(); sub++ )
			{      // check if it2's pair matches to "sub"
				if( it2->pair == sub->itself )
				{       // first timer or not
					if( sub->checked == false )
					{       // compare sub's pair to it2
						if(sub->pair == it2->itself)
						{
							cout << " Heureka I've found a pair!  " << it2->itself << " - " << sub->itself << endl;
							it2->checked = true; //set both of them to checked
							sub->checked = true;
						}
						break; // break the loop
					}
				}
			}
		}
	}

	cout << "Finished...";
	cin.ignore( 1, '\n' ); //ignore new line
	cin.get(); // works as "pause"
	numbers.clear(); // free memory from vector
	return 0;
}

Tiny code, to generate a few perfect numbers..

#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>

using namespace std;

int main()
{
	const unsigned int tmb[] = { 2, 3, 5, 7, 13, 17, 19, 31, 0 }; //Mersenne primes
	
	const unsigned int *ptr = tmb; // ptr pointer points to tmb
	unsigned long long int tmp = 0;
	
	cout << "Perfect numbers... " << endl;
	
	while( *ptr != 0 )
	{       // if you want to compile this with MS VisualStudio, then some type casting will be needed here, because of pow.
      		tmp = pow( 2, *ptr-1 ) * ( pow( 2, *ptr ) - 1 ); //calculating perfect number
		ptr++; //moving pointer to the next element
		
		cout << " " << tmp << endl;
	}
			
	cout << "Finished...";
	cin.ignore( 1, '\n' );
	cin.get();
	return 0;
}

You can find some help in here to generate perfect numbers. Or you can write a simple program with multiple loops to find perfect numbers, but that would be really really slow.

Well, the first time you did not stated what your program does. I had to assume you wanted to find amicable pairs. Amicable pairs and perfect numbers are not the same. The reason why 6 is not being displayed is because its pair is the number itself. And that is not a valid amicable pair, although it is a perfect number. Remove the line beneath, from the scope line "thing", and 6 will be displayed.
Help with Code Tags C++ Syntax (Toggle Plain Text)
(it->itself != it->pair)(it->itself != it->pair)

In fact i meant my own code not yours.and if you read and understand mine completely you will see that it is supposed to find both perfect numbers and amicable pairs in the interval.in fact i was sure about this algorithm 'cause i tried it with Python and It worked perfect.and my code's problem causes the program to not to be able to find amicable pairs too.
Could you look at my code once more and tell me about my for loops that find the sum of divisors.there must be a problem that i couldn't find.maybe you will.
and about that array.I changed the size to a number.
thanks

And
Amicable pairs and perfect numbers are really close.you know them so there is no need for an explanation.So you can find them with one program.notice this part of my code:

if(sum2==i){
                    if(sum1==i){
                        p[pi]=i;
                        ++pi;}
                    else{
                        if(sum1<i){
                            a[ai][0]=sum1;
                            a[ai][1]=i;
                            ++ai;}
                        else if(sum1>i){
                            a[ai][0]=i;
                            a[ai][1]=sum1;
                            ++ai;}}}}

this part stores amicable pairs in a and perfect numbers in p.

Hey bevox.With respect to you,I've changed your code a little and now it can find perfect numbers,too.

#include <iostream>
#include <vector>
#include <cstdlib>

using namespace std;

struct amicable //structure
{
	unsigned int itself, pair; // to store the number, and its pair
	bool scope, checked; // "scope" already explained
};                                            // "checked" to remember, previously displayed elements

int main()
{
	unsigned int start, end, tmp_pair = 0; // tmp_pair is a temporary variable
	amicable tmp = { 0, 0, false, false }; //number and pair set 0, scope,checked to false

	vector<amicable>numbers; // vector to store numbers

	cout << "Amicable pair finder..." << endl << "Give me the beginning and the ending value of the interval!" << endl << "#: ";
	cin >> start >> end;  // reading in

	if( start >= end ) //start must be smaller than end
	{
		cout << "Invalid interval!" << endl;
                cin.ignore(1,'\n');
		cin.get();
		exit(1);
	}

	for( unsigned int i = start; i <= end; i++ ) // occupy vector
	{
		tmp.itself = i;
		numbers.push_back( tmp );
	}

	for( vector<amicable>::iterator it = numbers.begin(); it < numbers.end(); it++ )
	{
		for( int i = (it->itself / 2) + 1; i > 0; i-- ) // creating the sum of the divisors
		{
			if( it->itself % i == 0 )
			{
				tmp_pair += i;
			}
		}

		it->pair = tmp_pair; // sets pair
		if( (it->pair <= end) && (it->pair >= start) && (it->pair > 0 ) ) it->scope = true;
		// check if the pair is possibly in the range
		tmp_pair = 0; // reset tmp_pair to 0 for the next element
	}
	// outer for loop works similar as bubble sorting, as it walks through the elements
	for( vector<amicable>::iterator it2 = numbers.begin(); it2 < numbers.end(); it2++ )
	{      // if in the scope, and hasn't been checked start looping
		if( it2->scope == true && it2->checked == false && it2->itself>1 )
		{	// inner loop for comparing the rest of the elements to "it2" outer loop
			for( vector<amicable>::iterator sub = numbers.begin(); sub < numbers.end(); sub++ )
			{      // check if it2's pair matches to "sub"
				if( it2->pair == sub->itself )
				{       // first timer or not
					if( sub->checked == false )
					{       // compare sub's pair to it2
						if(sub->pair == it2->itself)
						{
                                if(sub->itself != it2-> itself)
                                {
                                cout << " Heureka I've found an amicable pair!  " << it2->itself << " - " << sub->itself << endl;
                                it2->checked = true; //set both of them to checked
                                sub->checked = true;
                                }
                                else{
                                    cout<<" Heureka I've found a perfect number!  "<<it2->itself<<endl;
                                }
						}
						break; // break the loop
					}
				}
			}
		}
	}

	cout << "Finished...";
	cin.ignore( 1, '\n' ); //ignore new line
	cin.get(); // works as "pause"
	numbers.clear(); // free memory from vector
	return 0;
}

...I've changed your code a little and now it can find perfect numbers,too.

...The reason why 6 is not being displayed is because its pair is the number itself. And that is not a valid amicable pair, although it is a perfect number. Remove the line beneath, from the scope line "thing", and 6 will be displayed.

(it->itself != it->pair)

:) Hope, this thread is now solved. Good luck, with rest of your programs.

I know you are tired of writing in this thread but I wanna tell you that finally i got my code working.
I'm not humble enough to prevent myself to tell that mine worked faster than yours.
here's the corrected code:

#include <conio.h>
#include <iostream>
using namespace std;
int main(){
    char c='y';
    do{
        int start,end,ai=0,pi=0;
        bool checked=true;
        cout<<"Enter a number to begin with : ";
        cin>>start;
        cout<<"Enter a number to finish with : ";
        cin>>end;
        if(start>=end){
            cout<<"The beginning number must be smaller than the ending one.\n";}
        else{
            double a[500][2],p[500];
            a[0][0]=0;
            p[0]=0;
            for(int i=start;i<=end;++i){
                int sum1=0,sum2=0;
                for(int j=i/2;j>0;--j) if(i%j==0) sum1+=j;
                for(int j=sum1/2;j>0;--j) if(sum1%j==0) sum2+=j;
                if(sum2==i){
                    if(sum1==i){
                        for(int j=0;p[j]!=0;++j){
                            if(p[j]==i){
                                checked=false;
                                break;}}
                        if(checked){
                            p[pi]=i;
                            ++pi;
                            p[pi]=0;}
                             checked=true;}
                    else{
                        if(sum1<i){
                            for(int j=0;a[j][0]!=0;++j){
                                if(a[j][0]==sum1){
                                    checked=false;
                                    break;}}
                            if(checked){
                                a[ai][0]=sum1;
                                a[ai][1]=i;
                                ++ai;
                                a[ai][0]=0;}
                                 checked=true;}
                        else if(sum1>i ){
                            for(int j=0;a[j][0]!=0;++j){
                                if(a[j][0]==i){
                                    checked=false;
                                    break;}}
                            if(checked){
                                a[ai][0]=i;
                                a[ai][1]=sum1;
                                ++ai;
                                a[ai][0]=0;}
                                 checked=true;}}}}
            cout<<"----------------------------------------\n";
            cout<<"Amicable pair(s) between "<<start<<" and "<<end<<"\n";
            for(int i=0;a[i][0]!=0;++i) cout<<"*("<<a[i][0]<<","<<a[i][1]<<")* ";
            cout<<"\n----------------------------------------\n";
            cout<<"Perfect number(s) between "<<start<<" and "<<end<<"\n";
            for(int i=0;p[i]!=0;++i) cout<<"*"<<p[i]<<"* ";
            cout<<"\n----------------------------------------\n";
            cout<<"Do you want to continue(y/n) : ";
            cin>>c;}}
    while(c=='y');
    return 0;}

hi,I know that your code has been solved!But I didn't see the solution!But I have fixed by initializing start and end giving them size 0!It runs now!is it correct or not?

#include <conio.h>
#include <iostream>
using namespace std;
int main(){
    int start = 0,end = 0;
    double a[start-end][2],p[start-end];
    cout<<"Enter a number to begin with : ";
    cin>>start;
    cout<<"Enter a number to finish with : ";
    cin>>end;
    if(start==end || start>end){
        cout<<"The beginning number must be smaller than the ending one.";}
    else{
        char c='y';
       int sum1=0,sum2=0,ai=0,pi=0;
        do{
            for(register int i=start;i<=end;++i){
                for(register int j=1;j<i;++j) 
                if(i%j==0) 
                sum1+=j;
                for(register int j=1;j<sum1;++j) 
                if(sum1%j==0)
                 sum2+=j;
                if(sum2==i){
                    if(sum1==i){
                        p[pi]=i;
                        ++pi;}
                    else if(sum1<i){
                            a[ai][0]=sum1;
                            a[ai][1]=i;
                            ++ai;}
                        else if(sum1>i){
                            a[ai][0]=i;
                            a[ai][1]=sum1;
                            ++ai;}}}
            a[ai][0]=0;
            p[pi]=0;
            cout<<"Amicable pair(s) between "<<start<<" and "<<end<<"\n";
            for(register int i=0;a[i][0]!=0;++i) cout<<"("<<a[i][0]<<","<<a[i][1]<<")";
            cout<<"Perfect number(s) between "<<start<<" and "<<end<<"\n";
            for(register int i=0;p[i]!=0;++i) cout<<p[i]<<",";
            cout<<"\n";
            cout<<"Do you want to continue(y/n) : ";
            cin>>c;}
        while(c=='y');
        system("pause");
        return 0;}}

seems you just read the first page.

anyway.yes its correct but as i said i got it working
but thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.