help with memory

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
LP
New poster
Posts: 1
Joined: Mon Jul 09, 2007 7:02 pm

help with memory

Post by LP » Wed Jul 11, 2007 12:43 pm

Hi there. I got my program running well on Dev-C++

here is the code :

#include <iostream>
using namespace std;
int const maxN = 104, maxR = 2500;
typedef int arr[maxN/2][maxR];
int n;
int total;
arr d;
int a[101];
int solve()
{
int i,j,k, lim, best;
int t1,t2,t;
lim = n / 2;
for ( i = 1; i<=(lim+1); i ++ )
d[0] = 0;

/*for ( i = 0; i<=lim; i ++ )
for ( j = 0; j <=total/2; j ++ )
d[j] = false; */
d[0][0] = 1;
d[0][1] = 0;
for ( i = 1; i<=n; i ++ )
for ( j = lim ; j >=0; j -- )
{
t = d[j][0];
for ( k = 1; k<=t; k ++ )
{
d[j+1][0] ++ ;
d[j+1] [ d[j+1] [ 0] ] = d[j][k] + a;
}
}
best = 1;
for ( i = 2; i<=d[n/2][0]; i ++ )
if ( abs( total - 2 * d[n/2] ) < abs ( total - 2 * d[n/2][best] ) )
best = i;
t1 = d[n/2][best];
t2 = total - t1;
//cout << best << endl;
if ( t1 > t2 ) cout << t2 << " " << t1 << endl;
else cout << t1 << " " << t2 << endl;

}
int main()
{
int ntest,i;
cin >> ntest;
while ( ntest > 0 )
{
ntest--;
cin >> n;
total = 0;

for ( int i = 1; i<=n; i++ )
{
cin >> a;
total += a;
}

solve();
}

}



but then I got Runtime Error

Your program has died with signal 11 (SIGSEGV). Meaning:

Invalid memory reference

I don't know how I got it and please give me some help!!!

lnr
Experienced poster
Posts: 142
Joined: Sat Jun 30, 2007 2:52 pm
Location: Dhaka,Bangladesh

Re: help with memory

Post by lnr » Mon Oct 06, 2008 3:09 pm

To LP

Look at your solve() function again.
May be it is not returning anything.

Post Reply

Return to “C++”