| | |
Help: Code Sum/Factorial
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 39
Reputation:
Solved Threads: 0
Dear all,
There is some errors in the code that I can’t seem to spot.
The code is
and the question is attached
There is some errors in the code that I can’t seem to spot.
The code is
C++ Syntax (Toggle Plain Text)
# include <iostream> using namespace std; int main () { int N; // Declaring and initializing variables int n=0; int i=0; int fact=1; cout << "Please Enter an Integer" << endl; // The prompt cin >> N; // To read from the keyboard // Part a if (N==1 || N==0) { cout << "The Factorial of " << N << " = 1" << endl; } // Because it's known that the fact. of 0/1 = 1 while (N>1 && i>=0) { fact = fact * (N-i); i = i+1; } cout << "The factorial of " << N << " = " << fact << endl; // Part b for ( int sigma=0; n=0 || n<=N ; n++) { sigma = sigma + (n*n); } return 0; }
and the question is attached
Last edited by new programer; Oct 21st, 2009 at 6:05 am.
•
•
Join Date: Nov 2008
Posts: 394
Reputation:
Solved Threads: 72
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.
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
•
•
Join Date: Oct 2009
Posts: 39
Reputation:
Solved Threads: 0
0
#3 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.
(b) I am using the baisc concept of it "Mathematically ... should it be altered somehow ?
•
•
Join Date: Jan 2008
Posts: 3,822
Reputation:
Solved Threads: 501
0
#5 Oct 21st, 2009
C++ Syntax (Toggle Plain Text)
while (N>1 && i>=0) { fact = fact * (N-i); i = i+1; }
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.
•
•
Join Date: Sep 2009
Posts: 19
Reputation:
Solved Threads: 5
1
#6 Oct 21st, 2009
For me, in these situations, its always easier to set up a for loop.
For instance you could have...
This way the code in the for loop keeps repeating until i is greater than n, which solves your problem of an infinite loop.
For instance you could have...
C++ Syntax (Toggle Plain Text)
for(int i = 1; i <= n; i++) { // Factorial code goes here }
This way the code in the for loop keeps repeating until i is greater than n, which solves your problem of an infinite loop.
•
•
Join Date: Oct 2009
Posts: 39
Reputation:
Solved Threads: 0
0
#7 Oct 21st, 2009
•
•
•
•
C++ Syntax (Toggle Plain Text)
while (N>1 && i>=0) { fact = fact * (N-i); i = i+1; }
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.
•
•
Join Date: Oct 2009
Posts: 39
Reputation:
Solved Threads: 0
0
#10 Oct 21st, 2009
I did correct it. That was for part a
and I modified part b .. as follows
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?
and I modified part b .. as follows
C++ Syntax (Toggle Plain Text)
// Part b cout << "Please Enter an Integer" << endl; // The prompt cin >> N; // To read from the keyboard sum=0; for ( n=0 ; n<=N ; n++) { sum = sum + (n*n); } return 0; }
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?
![]() |
Similar Threads
- Time complexity of algorithm (Computer Science)
- Code Snippet: finding factorial of a number. (C)
- Factorial of any length (C++)
- having problem while creating factorial (C#)
- what is C++ builder code for sum (C++)
- SUm up combos or choose r out of n (C)
Other Threads in the C++ Forum
- Previous Thread: Compiling linux based static library in Windows
- Next Thread: variable help first time using C++
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






