#include <stdio.h>
#include <stdlib.h>
struct amicable
{
int** amicablePair;
int size;
};
struct amicable *getAmicablePairs(int startnum, int endnum);
int sumFactors(int number);
int main(int argc, char** argv) {
int startnum = 220;
int endnum = 284;
struct amicable* ami;
int i;
ami = getAmicablePairs(startnum, endnum);
if(ami==NULL)
{
printf("Error: malloc could not allocate");
return (EXIT_SUCCESS);
}
printf("{");
for(i = 0; i<ami->size; i++)
{
printf("{%d, %d}",ami->amicablePair[i][0], ami->amicablePair[i][1]);
}
printf("}");
//getch();
//return 0;
return (EXIT_SUCCESS);
}
int sumFactors(int number){
int i;
int sum;
sum=1;
for(i=2;i<=number/2;i++){
if(number%i==0)
{
sum=sum+i;
}
}
return sum;
}
struct amicable *getAmicablePairs(int startnum, int endnum)
{
int nrows=(endnum-startnum);
int ncols=2;
int i,j,k=0;
struct amicable* record;
/* allocate array of pointers */
record->amicablePair=(int**)malloc(nrows*sizeof(int*));
if(record->amicablePair==NULL)
{
printf("Error: malloc could not allocate %d bytes for a\n", 2 * sizeof(int *));
return record;
}
/* allocate each row */
for(i=0;i<nrows;i++) {
record->amicablePair[i]=(int*)malloc(ncols*sizeof(int));
if(record->amicablePair[i]==NULL)
{
printf("Error: malloc could not allocate %d bytes for a\n", 3 * sizeof(int));
return record;
}
}
/* problem solution */
for(i = startnum; i <= endnum; i++)
{
for(j = endnum; j >= startnum; j--)
{
if ((sumFactors(i)==j) && (sumFactors(j)==i) && (i != j))
{
record->amicablePair[k][0]=i;
record->amicablePair[k][1]=j;
k++;
}
}
}
record->size=k;
return record;
}
I am getting this error while executing (in netbeans 6.7) the above code:
6 [main] asp11d 3400 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
908 [main] asp11d 3400 open_stackdumpfile: Dumping stack trace to asp11d.exe.stackdump
RUN FAILED (exit value 35,584, total time: 421ms)
..............please send me solutions as soon as possible.................
Thanks in advance