Page 1 of 1

### need help

Posted: Sun Sep 04, 2005 9:23 pm
I'm a newbie in c++. Can you guy help me with this source code?

----------------------------------------------------------------------------------
#include <iostream>

using namespace std;

const long TData = 101;
long sum(long j,long k,long num[TData]) {
long i,res;
res=0;
for (i=j;i<k+1;i++) {
res+=num;
}

return res;
};

void main() {
short prime[TData];
long i,j,k,tc,tmp,n;
long num[TData];
long min,keep,x,y;

//sieve of aristhotenes;
for (i=0;i<TData;i++) {
prime=1;
}
prime[0]=0;
prime[1]=0;

i=2;
while (i<TData) {
if (prime=1) {
tmp=i;
j=i+i;
while (j<TData) {
prime[j]=0;
j+=tmp;
}
}
i++;
}

//input
cin >> tc;
for (i=0;i<tc;i++) {
cin >> n;
for(j=0;j<n;j++) {
cin >> num[j];
}

min=10001;
for(j=0;j<n;j++) {
k=j+1;
keep=sum(j,k,num);
while ((prime[keep]==0)&&(k<n)) {
k++;
keep=sum(j,k,num);
}
if (k<n) {
x=j;
y=k;
min=y-x+1;
}
}

//output
if (min=10001) {
cout << "This sequence is anti-primed.";
}
else {
cout << "Shortest primed subsequence is length " << min << ':';
for (j=x;j<=y;j++)
cout << ' ' << num[j];
}
cout << endl;
}
}
----------------------------------------------------------------------------------

Posted: Sun Sep 04, 2005 9:56 pm
main must return int.

Posted: Mon Sep 05, 2005 12:57 am
that doesn't help. the problem with my source code is runtime error.

Posted: Mon Sep 05, 2005 1:17 am
Well, with void main it doesn't even compile.

Are you sure you're not accessing not allocated memory? Is TData big enough? Does the crash happen locally or remotely on OJ? Answers to those questions should help you to find the reason for the crash.