Please help with the following code,,,
It is compiling though but
During run its stating that
"Your code has stopped its execution with a non-zero (failure) exit value.This is generally due to run time Exceptions like Memory Access Violation and Floating Point Exception. Please check your code for run time Exceptions and try again. "[/B]

#include<stdlib.h>
#include<stdio.h>
struct amicable			
{
	int **amicablePair;
	int size;
};
struct amicable *getAmicablePairs(int startnum,int endnum);
int main()
{
struct amicable *ami;
int i,startnum=200,endnum=300;

if(startnum<0&&endnum<0&&startnum>endnum&&endnum>=15000)
{
ami->amicablePair=NULL;
ami->size=0;
}
else
{
  ami=getAmicablePairs(startnum,endnum);
printf("{");
for(i=0;i<ami->size;i++)
{
printf("{%d %d}",ami->amicablePair[i][0],ami->amicablePair[i][1]);
}
printf("}");
free(ami);
}
}

struct amicable *getAmicablePairs(int startnum,int endnum)
{
struct amicable *amic;
amic->size=0;
int i,j,divisor=0,divisor1=0,k=0,m;
amic->amicablePair=(int **)malloc(32*sizeof(int));

	for(i=startnum;i<=endnum;i++)
	{
	for(j=1;j<=i/2;j++)
	{
		if(i%j==0)
			divisor+=j;
	}
	if(divisor<=endnum&&divisor>i)
	{
		for(m=1;m<=divisor/2;m++)
		{
			if(divisor%m==0)
				divisor1=divisor1+m;
		}
		if(divisor1==i)
		{
			amic->amicablePair[amic->size][0]=i;
			amic->amicablePair[amic->size][1]=divisor;
			amic->size++;
		}
	}
	divisor=0;divisor1=0;
	}
return amic;
}
Comments
Perhaps if you had spent a few more minutes on code formatting instead of running around screaming "urgent", you might get somewhere.

Did you use a debugger?

Meanwhile, there are few problems here. First, the amicablePair array is allocated, but not initialized. Its elements, which are supposed to be of type int * are garbage pointers. As soon as you try to access them (at lines 55 and 56) you have your memory access violation.
Second, you allocate the array of ints, but pretend to allocate an array of int pointers. It is not exactly what bits you now, but it would bit you later.
To fix your code, change line 37 to

amic->amicablePair=malloc(32*sizeof(int *));

and add initialization:

for(i = 0; i < 32; i++)
    amic->amicablePair[i] = malloc(sizeof(int) * 2);

Don't forget to check for malloc failures, and cleanup properly when you done.

Hi,,,Thanx for ur for quick response..I made the necesssary corrections u suggested but I am still facing the problem..This time there is a compilation error..
Actually its working on my Turboc debugger....But when i am trying to execute it on my competiton online debugger its showing the following compilation errors..
amicablenumber/amicablenumber.c: In function ‘amicable* getAmicablePairs(int, int)’:
amicablenumber/amicablenumber.c:33: error: invalid conversion from ‘void*’ to ‘int**’
amicablenumber/amicablenumber.c:51: error: invalid conversion from ‘void*’ to ‘int*’

Please suggest me what to do this for this problem...I got stuck up for nearly 5 days inspite of completing the code....

#include"amicablenumber.h"
#include<stdlib.h>
#include<stdio.h>
struct amicable *getAmicablePairs(int startnum,int endnum);
int main()
{
struct amicable *ami;
int i,startnum=200,endnum=300;

if(startnum<0&&endnum<0&&startnum>endnum&&endnum>=15000)
{
ami->amicablePair=NULL;
ami->size=0;
}
else
{
  ami=getAmicablePairs(startnum,endnum);
printf("{");
for(i=0;i<ami->size;i++)
{
printf("{%d %d}",ami->amicablePair[i][0],ami->amicablePair[i][1]);
}
printf("}");
free(ami);
}
}

struct amicable *getAmicablePairs(int startnum,int endnum)
{
struct amicable *amic;
amic->size=0;
int i,j,sum=0,sum1=0,k=0,m;
amic->amicablePair=malloc(32*sizeof(int *));

	for(i=startnum;i<=endnum;i++)
	{
	for(j=1;j<=i/2;j++)
	{
		if(i%j==0)
			sum+=j;
	}
	if(sum<=endnum&&sum>i)
	{
		for(m=1;m<=sum/2;m++)
		{
			if(sum%m==0)
				sum1=sum1+m;
		}
		if(sum1==i)
		{
			amic->amicablePair[amic->size]=malloc(2 * sizeof(int));
			amic->amicablePair[amic->size][0]=i;
			amic->amicablePair[amic->size][1]=sum;
			amic->size++;
		}
	}
	sum=0;sum1=0;
	}
return amic;
}

Edited 6 Years Ago by Nick Evan: Added code-tags

I don't know what to say. I copied and pasted your code. Added amicable.h with the your struct definition. Compiled it with gcc -Wall. Got one warning.

Dump your turbo c, what else can I tell. It's a crap anyway.

PS: ran it, and get an obvious segfault at amic->size = 0 . Please acquire a habit of not using uninitialised variables.

Edited 6 Years Ago by nezachem: n/a

This article has been dead for over six months. Start a new discussion instead.