Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need help regarding a program
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
A.J




PostPosted: Fri Feb 22, 2008 1:19 pm   Post subject: Need help regarding a program

I am new to C++, so I needed help regarding why this program always crashes:
code:

#include <iostream>
#include <cmath>
using namespace std;
int sd(int x)
{
    int c=1;
    for (int k=1;k<=floor(sqrt(x));k++)
    {
        if (x % k==0)c++;
    }
    return c;
}
int main()
{
        clock_t starttime=clock();
    double count=0;
    int a[9999999];
    for (int i=1;i<=9999999;i++)
    {
        a[i]=sd(i);
    }
    for (int j=1;j<=9999998;j++)
    {
        if (a[j]==a[j+1])count++;
    }
        clock_t endtime=clock();
        double elapsedtime=(double)(endtime-starttime)/CLOCKS_PER_SEC;
        cout<<elapsedtime<<" second(s)."<<endl;
    cout<<count<<endl;
        return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
Saad




PostPosted: Fri Feb 22, 2008 1:54 pm   Post subject: RE:Need help regarding a program

Woah there,
code:
int a[9999999];
is a really huge array. The array a is a local variable to the main function and as a result, when memory needs to be allocated for a local variable it is taken off the stack. There isn't enough stack space to assign all that memory.

However if you made a global, the memory are assigned before the program starts and don't use the stack.
wtd




PostPosted: Fri Feb 22, 2008 2:03 pm   Post subject: RE:Need help regarding a program

Or just dynamically allocate it.
A.J




PostPosted: Fri Feb 22, 2008 2:21 pm   Post subject: Re: Need help regarding a program

and how do i do that?
Tony




PostPosted: Fri Feb 22, 2008 2:31 pm   Post subject: RE:Need help regarding a program

malloc
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Saad




PostPosted: Fri Feb 22, 2008 2:37 pm   Post subject: Re: RE:Need help regarding a program

Tony @ Fri Feb 22, 2008 2:31 pm wrote:
malloc


Err, for C++ you should use new and delete
wtd




PostPosted: Fri Feb 22, 2008 2:48 pm   Post subject: RE:Need help regarding a program

Additional note, use a constant for 9999999, instead of magic numbers.
OneOffDriveByPoster




PostPosted: Fri Feb 22, 2008 4:34 pm   Post subject: Re: Need help regarding a program

A.J @ Fri Feb 22, 2008 1:19 pm wrote:
code:
    int a[9999999];
    for (int i=1;i<=9999999;i++)
    {
        a[i]=sd(i);
    }
Array indices start at 0. You are indexing out-of-bounds. Also, please #include <ctime>.
Sponsor
Sponsor
Sponsor
sponsor
A.J




PostPosted: Fri Feb 22, 2008 9:13 pm   Post subject: Re: Need help regarding a program

thx for your help guys!!!
sooooo..................... can you help me with this program?
code:

#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;

unsigned long long factorial[100];
long choose(unsigned long long x,unsigned long long y,unsigned long long factorial[10])
{
    if (y=0 or x==y)
    {
        return 1;
    }
    else
    {
        int m=max(x,y);
        return factorial[x]/((factorial[y])*(factorial[x-y]));
    }
}
int main()
{
    int count=0;
    factorial[1]=1;
    for (unsigned long long i=2;i<=100;i++)
    {
        factorial[i]=factorial[i-1]*i;
    }
    for (unsigned long long j=1;j<=100;j++)
    {
        for (unsigned long long k=1;k<=j/2;k++)
        {
            if (choose(j,k,factorial)>1000000)count++;
        }
    }
    cout<<count;
        return 0;
}
OneOffDriveByPoster




PostPosted: Fri Feb 22, 2008 9:23 pm   Post subject: Re: Need help regarding a program

A.J @ Fri Feb 22, 2008 9:13 pm wrote:
code:
...
unsigned long long factorial[100];
...
    for (unsigned long long i=2;i<=100;i++)
    {
        factorial[i]=factorial[i-1]*i;
    }
...
Same mistakes still. Some comments or description of what you are doing may help (considering the fact that the people who are trying to help you may be almost asleep). What problem are you having? Crash?--if so, fix your array indices.
A.J




PostPosted: Sat Feb 23, 2008 12:51 am   Post subject: Re: Need help regarding a program

well, this was #53 on project euler.
it doesn't matter, I finished it in turing (using pascal's triangle)
thanks for helping
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: