/* This program returns a run time error as follows: "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." PLEASE HELP.*/

#include<conio.h>
#include<stdio.h>
struct amicable
{
int **amicablePairs;
int size;
};
struct amicable* getAmicablePairs(int startnum,int endnum);
int main()
{
struct amicable *ami;
int i,startnum,endnum;
clrscr();

ami=getAmicablePairs(100,2000);
printf("{");
for(i=0;i<ami->size;i++)
{
printf("{%d %d}",ami->amicablePairs[i][0],ami->amicablePairs[i][1]);
}
printf("}");
getch();
return 0;
}


struct amicable * getAmicablePairs(int startnum,int endnum)
{
struct amicable *amic;
int i,j,divisor=0,divisor1=0,k=0,l,**p;
p=malloc(15*sizeof(int*));
if(p==NULL)
printf("failed for p");
if(startnum>0&&endnum>0&&startnum<endnum&&endnum<=15000)
{
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(l=1;l<=divisor/2;l++)
{
if(divisor%l==0)
divisor1+=l;
}
if(divisor1==i)
{
p[k]=malloc(2*sizeof(int));
if(p[k]==NULL)
printf("failed for loop");
p[k][0]=i;p[k][1]=divisor;
//amic->amicablePairs[k][0]=i;
//amic->amicablePairs[k][1]=divisor;
k++;
}
}
divisor=0;divisor1=0;
}
if(k==1)
goto here;
amic->size=k;
amic->amicablePairs=p;
return amic;
}
else
here:return NULL;
}

Recommended Answers

All 3 Replies

line 67: amic is an ininitialized pointer.

how to initialise that pointer????

call malloc() to allocate memory before line 67.

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.