944,117 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1356
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 21st, 2009
0

Help: Code Sum/Factorial

Expand Post »
Dear all,

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

The code is

C++ Syntax (Toggle Plain Text)
  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
Attached Images
File Type: bmp dfdfdf.bmp (279.1 KB, 17 views)
Last edited by new programer; Oct 21st, 2009 at 6:05 am.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
new programer is offline Offline
83 posts
since Oct 2009
Oct 21st, 2009
0
Re: Help: Code Sum/Factorial
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.
Reputation Points: 749
Solved Threads: 135
Practically a Master Poster
StuXYZ is offline Offline
660 posts
since Nov 2008
Oct 21st, 2009
0
Re: Help: Code Sum/Factorial
Click to Expand / Collapse  Quote originally posted by StuXYZ ...
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 ?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
new programer is offline Offline
83 posts
since Oct 2009
Oct 21st, 2009
0
Re: Help: Code Sum/Factorial
//Any help, remarks would be appreciated
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
new programer is offline Offline
83 posts
since Oct 2009
Oct 21st, 2009
0
Re: Help: Code Sum/Factorial
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 21st, 2009
1
Re: Help: Code Sum/Factorial
For me, in these situations, its always easier to set up a for loop.

For instance you could have...
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 11
Solved Threads: 8
Light Poster
Kontained is offline Offline
34 posts
since Sep 2009
Oct 21st, 2009
0
Re: Help: Code Sum/Factorial
C++ Syntax (Toggle Plain Text)
  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 (&&) ?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
new programer is offline Offline
83 posts
since Oct 2009
Oct 21st, 2009
0
Re: Help: Code Sum/Factorial
And how come part b is not working ??
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
new programer is offline Offline
83 posts
since Oct 2009
Oct 21st, 2009
0
Re: Help: Code Sum/Factorial
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.
Reputation Points: 11
Solved Threads: 8
Light Poster
Kontained is offline Offline
34 posts
since Sep 2009
Oct 21st, 2009
0
Re: Help: Code Sum/Factorial
I did correct it. That was for part a

and I modified part b .. as follows
C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
new programer is offline Offline
83 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Compiling linux based static library in Windows
Next Thread in C++ Forum Timeline: variable help first time using C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC