Help: Code Sum/Factorial

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 39
Reputation: new programer is an unknown quantity at this point 
Solved Threads: 0
new programer new programer is offline Offline
Light Poster

Help: Code Sum/Factorial

 
0
  #1
Oct 21st, 2009
Dear all,

There is some errors in the code that I can’t seem to spot.

The code is

  1. # include <iostream>
  2. using namespace std;
  3. int main ()
  4. {
  5.  
  6. int N; // Declaring and initializing variables
  7. int n=0;
  8. int i=0;
  9. int fact=1;
  10.  
  11. cout << "Please Enter an Integer" << endl; // The prompt
  12. cin >> N; // To read from the keyboard
  13.  
  14. // Part a
  15.  
  16. if (N==1 || N==0)
  17. { cout << "The Factorial of " << N << " = 1" << endl; } // Because it's known that the fact. of 0/1 = 1
  18.  
  19.  
  20. while (N>1 && i>=0)
  21.  
  22. { fact = fact * (N-i);
  23. i = i+1;
  24. }
  25.  
  26.  
  27. cout << "The factorial of " << N << " = " << fact << endl;
  28.  
  29. // Part b
  30.  
  31. for ( int sigma=0; n=0 || n<=N ; n++)
  32. { sigma = sigma + (n*n);
  33. }
  34.  
  35.  
  36. return 0;
  37.  
  38.  
  39. }

and the question is attached
Last edited by new programer; Oct 21st, 2009 at 6:05 am.
Attached Images
File Type: bmp dfdfdf.bmp (279.1 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 394
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz
 
0
  #2
Oct 21st, 2009
If you are debugging code (and that is what you are doing here), you need to step through the code and check your assumptions. That can be done with a traditional debugger that lets you see each line as it is run, or by putting some output statements in the code.

Your errors include
(a) the loop for the factorial never is run. since neither N nor i are changed in the while loop. This should be fairly obvious since you never reached the "factoraila == " statement

(b) you are using a loop to calculate [tex]\sum n^2[\tex] ?? There are simple formula to get the result, no loop.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: new programer is an unknown quantity at this point 
Solved Threads: 0
new programer new programer is offline Offline
Light Poster
 
0
  #3
Oct 21st, 2009
Originally Posted by StuXYZ View Post
If you are debugging code (and that is what you are doing here), you need to step through the code and check your assumptions. That can be done with a traditional debugger that lets you see each line as it is run, or by putting some output statements in the code.

Your errors include
(a) the loop for the factorial never is run. since neither N nor i are changed in the while loop. This should be fairly obvious since you never reached the "factoraila == " statement

(b) you are using a loop to calculate [tex]\sum n^2[\tex] ?? There are simple formula to get the result, no loop.
How come it is not changing in part (a) I did increament it

(b) I am using the baisc concept of it "Mathematically ... should it be altered somehow ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: new programer is an unknown quantity at this point 
Solved Threads: 0
new programer new programer is offline Offline
Light Poster
 
0
  #4
Oct 21st, 2009
//Any help, remarks would be appreciated
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,822
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #5
Oct 21st, 2009
  1. while (N>1 && i>=0)
  2.  
  3. { fact = fact * (N-i);
  4. i = i+1;
  5. }

Yes, i changes, but not in a way that will get you out of this loop. N doesn't change, so this is an infinite loop. Once inside, you'll never get out (well, eventually, after i overflows, you might possibly get out, but that'll take a few billion iterations). Change the conditions on your while loop so that it is no longer an infinite loop.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 19
Reputation: Kontained is an unknown quantity at this point 
Solved Threads: 5
Kontained Kontained is offline Offline
Newbie Poster
 
1
  #6
Oct 21st, 2009
For me, in these situations, its always easier to set up a for loop.

For instance you could have...
  1. for(int i = 1; i <= n; i++)
  2. {
  3. // Factorial code goes here
  4. }

This way the code in the for loop keeps repeating until i is greater than n, which solves your problem of an infinite loop.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: new programer is an unknown quantity at this point 
Solved Threads: 0
new programer new programer is offline Offline
Light Poster
 
0
  #7
Oct 21st, 2009
Originally Posted by VernonDozier View Post
  1. while (N>1 && i>=0)
  2.  
  3. { fact = fact * (N-i);
  4. i = i+1;
  5. }

Yes, i changes, but not in a way that will get you out of this loop. N doesn't change, so this is an infinite loop. Once inside, you'll never get out (well, eventually, after i overflows, you might possibly get out, but that'll take a few billion iterations). Change the conditions on your while loop so that it is no longer an infinite loop.
aha ... so the problem is with the condition .. exactly in using (&&) ?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: new programer is an unknown quantity at this point 
Solved Threads: 0
new programer new programer is offline Offline
Light Poster
 
0
  #8
Oct 21st, 2009
And how come part b is not working ??
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 19
Reputation: Kontained is an unknown quantity at this point 
Solved Threads: 5
Kontained Kontained is offline Offline
Newbie Poster
 
0
  #9
Oct 21st, 2009
Originally Posted by new programer View Post
aha ... so the problem is with the condition .. exactly in using (&&) ?
No its not just the &&, its the whole statement. You need to rethink your condition. The one you have now is incorrect. Take a look at my earlier post for ideas.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 39
Reputation: new programer is an unknown quantity at this point 
Solved Threads: 0
new programer new programer is offline Offline
Light Poster
 
0
  #10
Oct 21st, 2009
I did correct it. That was for part a

and I modified part b .. as follows
  1. // Part b
  2. cout << "Please Enter an Integer" << endl; // The prompt
  3. cin >> N; // To read from the keyboard
  4.  
  5.  
  6. sum=0;
  7. for ( n=0 ; n<=N ; n++)
  8. {
  9. sum = sum + (n*n);
  10. }
  11.  
  12. return 0;
  13.  
  14.  
  15. }

It did not use to appear so I thought I would add the prompt again

the prompt appeared and I entered the number but then it terminates

I think I don't have to put the prompt again .. maybe there's something wrong with the code at part b .. any hints?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC