BevoX 39 Junior Poster in Training

Your array is too small, you only allocated space for only one int element.

int* x = new int;
correct way: int* x = new int [ the amount you need ];
while ( b != 0 )
{
          x[counter_x] = b;
          cout << x[counter_x] << " ";
          counter_x++;
          b--;
}

In your loop you index out of your array. The program is trying to access areas of the memory, which don't belong to it, and thats why you got that memory access violation error.

BevoX 39 Junior Poster in Training

sorry - double post

BevoX 39 Junior Poster in Training

Thank you Vmanes! I am still confused with passing pointers. And now looking at my code, I am wondering how on earth it compiled. :D I will look into this matter more closely, and thanks again.

BevoX 39 Junior Poster in Training
#include <iostream>

using namespace std;

void b( char** p )
{
	*p = "some text";
}

int main()
{
	char *p = new char [20];
	b( &p );
	
	cout << p << endl ;

	cin.get();
	return 0;
}

But, isn't it wiser to pass the pointer by reference? - Generally speaking of course. Because if you pass the pointer by parameter, you just make a copy of the original pointer. And by manipulating its copy, you would leave the original pointer in the main() function intact, and it would still have its old position. And it might cause you some headaches.

BevoX 39 Junior Poster in Training

I personally think it's much more simple to use an array of structs, and dynamically reallocate it ...
You don't need an array of pointers in that way...

I agree, but he is interested in 2D arrays, so I tried to give him an example to show him, how it works. I would create a structure as well, and then go for a vector. :)

BevoX 39 Junior Poster in Training
#include <iostream>

using namespace std;

int main()
{
	char ** my_arrays;

	my_arrays = new char * [10]; // after this you will have 10 "arrays" - pointers
	my_arrays[3] = new char [10]; // allocate memory for the 4th array - 10 characters for my_array[3]; - 9 can be used last is '\n'

	cout << "Say sg: " << flush;
	
	cin.getline( my_arrays[3], 10 );
	//get 10-1 characters into the 4th "array" of my_arrays
	
	cout << my_arrays[3] << endl; //print out;

	delete[] my_arrays[3]; // free the 4th array
	delete[] my_arrays; // free my_arrays

	cin.get();
	return 0;
}

Hope this helps.

BevoX 39 Junior Poster in Training

You have to delete all the branches of the tree you've allocated. You use delete to get rid of a single block of data. And delete[] - to get rid of an array of data. And if you just delete[] the root, the other branches would remain there.

Maybe you could try to use one of the STL containers. I've read there is a garbage collection library for C/C++ you should check that out. But I've never used it, so I have no experience with that kind of stuff.

http://developers.sun.com/solaris/articles/libgc.html
http://www.codeproject.com/KB/cpp/agm_libgc.aspx
The second link states, there are some drawbacks of using libgc...

BevoX 39 Junior Poster in Training
BevoX 39 Junior Poster in Training

And what about white spaces and new lines? You are stuffing all the characters, one by one in a temporary character, but by doing that you are losing all the new lines and white spaces. Therefore you will get an unreadable stream of characters. It would be better to read the file line by line, and then tokenize it. Also with this line:

text[i] = '\0';

you are overwriting the last character in the array, because text is still pointing at the last read character.

BevoX 39 Junior Poster in Training

I don't get it. What's the point of duplicating the source array in a reverse order? You can check the beginning and the ending of the array at the same time. And you can still use the array's indexing method with strings to compare each character to another.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string word;
	bool chk = true;

	cout << "Give me a word: ";
	getline( cin, word );

	int n = word.length();

	for( int i = 0, j = n - 1; i < n; i++,j-- )
	{
		if( word[i] != word[j] )
		{
		    chk = false;break;
		}
	}

	if( chk ) cout << word << " is a palindrome!";
	else cout << word << " is not a palindrome!";

	cin.get();
	return 0;
}

This is the same thing, but without copying the source. Only difference this one uses string class, so you won't be limited by the size of your arrays, and also you won't have to bother with memory allocations. :)( if the word is longer than 19 characters )

BevoX 39 Junior Poster in Training

You start dividing every number by 1! Every integer can be divided by 1 without remainder. Therefore you will always get "false" from your is_prime(int) function.

Secondly, you are going to overflow your int value. There are thousands of prime numbers in between 1 and 2000000. And their sum is way bigger than what int can handle. Use 64bit integer. (long long)

verruckt24 commented: good point on the int capacity +2
BevoX 39 Junior Poster in Training

I recognize this code, this is from 3DBuzz's tutorial. :)
You did not declare the default constructor in your class. In other words you do not have declaration for the constructor with no arguments. Watch the 'public section' closely.

BevoX 39 Junior Poster in Training
BevoX 39 Junior Poster in Training

mPayment = (P*i)/q((1-pow((1 + (i/q)),-t)));

Didn't you forget to put a * after q?
By the way it gives back 323,406.

BevoX 39 Junior Poster in Training
#include <iostream>

using namespace std;

int a = 3;
int b = 4;
int c = 5;
int * pA = &a;
int * pB = &b;
int * pC = &c;

int *abc[] =  { pA, pB, pC };

int main()
{
     cout <<  *abc[0] << endl;
     return 0;
}

Give the array the index number of the element, and then dereference it.

BevoX 39 Junior Poster in Training

...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.

BevoX 39 Junior Poster in Training

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 …

BevoX 39 Junior Poster in Training

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;
				}
			}
		}
	}
StuXYZ commented: Well written code, I could follow! +4
BevoX 39 Junior Poster in Training

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( …
Salem commented: It's a model of clarity the OP would do well to follow +27
BevoX 39 Junior Poster in Training

Sorry, my bad. Visual Studio 2008.

BevoX 39 Junior Poster in Training

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.

BevoX 39 Junior Poster in Training
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 )