954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help: Code Sum/Factorial

Dear all,

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

The code is

# 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

Attachments dfdfdf.bmp (279.12KB)
new programer
Junior Poster in Training
83 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

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.

StuXYZ
Practically a Master Poster
680 posts since Nov 2008
Reputation Points: 760
Solved Threads: 138
 

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 ?

new programer
Junior Poster in Training
83 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

//Any help, remarks would be appreciated

new programer
Junior Poster in Training
83 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 
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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

For me, in these situations, its always easier to set up a for loop.

For instance you could have...

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.

Kontained
Light Poster
34 posts since Sep 2009
Reputation Points: 11
Solved Threads: 8
 
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.

aha ... so the problem is with the condition .. exactly in using (&&) ?

new programer
Junior Poster in Training
83 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

And how come part b is not working ??

new programer
Junior Poster in Training
83 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 
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.

Kontained
Light Poster
34 posts since Sep 2009
Reputation Points: 11
Solved Threads: 8
 

I did correct it. That was for part a

and I modified part b .. as follows

// 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?

new programer
Junior Poster in Training
83 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

It doesnt appear that you have sum declared anywhere, unless you included it in the part A of the code. Also, you are not telling the program to do anything after the loop takes place. Output the variable sum after the for loop to see if your program actually works. ;)

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 

Hi, thanks for replying

well actually I started doing it all again "FOCUSING" lol

it works just fine now


Thanks all

new programer
Junior Poster in Training
83 posts since Oct 2009
Reputation Points: 10
Solved Threads: 1
 

Yes sometimes a little focus can go a long way. Please mark the thread solved if you have no further questions. Thanks.

Grn Xtrm
Posting Pro in Training
495 posts since Nov 2008
Reputation Points: 100
Solved Threads: 48
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You