problem during runtime

9 views
Skip to first unread message

Deepak

unread,
Jun 3, 2011, 5:01:28 AM6/3/11
to C++ Programming
hi
there is a problem of finding the factorial of no upto 100 now we
don't have any variable to store that amount of data
so i'm considering an array of 200 elements inwhich each element
contains the digits of final ans
for example 720 will be stored as a[0]=0 a[1]=2 a[2]=7
given below is it's program but it is not giving the correct ans
after 10
help me if u can find any bug in this program


#include<iostream>
using namespace std;
int main()
{
cout<<"enter the no of test cases:"<<endl;
int t;
int num;
cin>>t;
int a[200]; //array to accomodate the digits of the
factorial
for(int i=0;i<t;i++) //this loop will run for all the
test cases
{
a[0]=1; //initialising the array with
a[0]=1 and rest all the elements 0
for(int i=1;i<=199;i++)
{
a[i]=0;
}
int temp=0;
int m=1; //carry the no. of elements in
the array occupied during factorial computation
cout<<"enter the no"<<endl;
cin>>num; // the no. of which we have to
find the factorial
for(int j=1;j<=num;j++)
{
for(int k=0;;k++)
{
int x=a[k]*j+temp;
a[k]=x%10;
temp=x/10;
if(temp==0 && a[k+1]==0) break;

if(a[k+1]==0)m++;
}
}
for(int i=m-1;i>=0;i--)
{
cout<<a[i];
}
cout<<endl;
}
system("pause");
}

Deepak

unread,
Jun 3, 2011, 5:02:16 AM6/3/11
to C++ Programming

upashu2

unread,
Jun 14, 2011, 1:52:58 AM6/14/11
to C++ Programming
Integer is overflowing. take double and modified ur code accordingly.

BlueRaja

unread,
Jun 14, 2011, 12:26:34 PM6/14/11
to cpp-pro...@googlegroups.com
If your requirements allow it, using gmp (http://gmplib.org/) would be the best solution.

Otherwise, I suggest you become very friendly with your debugger - seeing the values of each variable as each line of code is executed will tell you exactly where something isn't behaving as you expected it to.

The debugger in Visual Studio is built in; a tutorial can be found here (http://www.cprogramming.com/tutorial/debugging_concepts.html) - skim through parts 1 and 2, part 3 is the important part.
If you are using gcc, the debugger is called gdb, and is more difficult to use.  A tutorial can be found here (http://www.cs.cmu.edu/~gilpin/tutorial/).
Debugging in most other IDEs is similar to Visual Studio, though you may want to use google to find a tutorial for your specific IDE.

Good luck!
Reply all
Reply to author
Forward
0 new messages