need help

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

Moderator: Board moderators

Post Reply
zero_cool
New poster
Posts: 27
Joined: Fri Sep 02, 2005 6:33 am

need help

Post by zero_cool » 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;
}
}
----------------------------------------------------------------------------------

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sun Sep 04, 2005 9:56 pm

main must return int.

zero_cool
New poster
Posts: 27
Joined: Fri Sep 02, 2005 6:33 am

Post by zero_cool » Mon Sep 05, 2005 12:57 am

that doesn't help. the problem with my source code is runtime error.

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » 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.

Post Reply

Return to “C++”