>>this is the code i use but an error occur
what is (are) the error(s) ?
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I suspect your code compiles but provides unexpeceted output. I think it helps if you write out what you want to do first, then try to write code. I've attached a number of comments to your code indicating what I think your code is doing. If you write out what needs to get done to display the pattern you want I think you will find several logic errors.
Also when posting code to this board please use code tags to maintain indentation and spacing, which I hope you use in your code, as it makes it much more readable and much easier to pick up a number of types of errors (none of which I can find in this code).
#include <iostream.h>
//use int main(), not void main()
void main()
{
int a = 10;
int n, i, j=0, k;
cout << "Enter the value for n: ";
cin >> n;
k=n;
//overwrite the value of a 10 times
for (i = 0; i < 10; i++)
a = i + 1;
/*a now equals 10. Since a never changes for the rest of the program it will be 10 for the rest of the program.*/
while(k > 0) //control number of lines
{
//General flow:
//each line will have three sections
//initially is a series of spaces
//in the middle is a series of numbers
//int the end is a series of numbers
//zero plus any number is always the number so drop the zeros
//on the middle part of the line display a and a space n - j times
for(i = (0 + j); i < n; i++)
cout << a << " ";
//at the end of the line display a plus a space n - 2 - j times
for(i = n - 2; i >= (0 + j); i--)
cout << a << " ";
//start a new line
cout<<endl;
j++;
//display j spaces initially on the new line
for(i = 0; i < j; i++)
cout << " ";
k--;
}
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
>>for(j=1;j<=1;j++)
It will never loop more than 1 time therefore you might as well delete that line. There is no point in writing a loop that only loops one time.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Dont use void at the main part.
laconstantine
Junior Poster in Training
70 posts since May 2007
Reputation Points: 10
Solved Threads: 1
Dont use void at the main part.
Even I had trouble trying to figure out what this meant :icon_wink:
What he means is this
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
>> really need to fin the proper code for factorial using successive addition
I think the factorial code has a combination of logic and syntax errors. Here's my logic:
factorial is successive multiplication:
3! = 3 * 2 * 1
mutiplication can be viewed as serial addition
3 * 2 = 3 + 3
Note that that means 3! is same as 3 * 2! and 4! is
4 * 3 * 2 = 4 * (3 + 3) = 4 * 6 = 4 + 4 + 4 + 4 + 4 + 4
or 4! = 4 * 3!;
So by extension, n! = n * (n - 1)!
This means you should be able to start with the lower factorials and work your way up to the desired factorial. Which is what I think your code is trying to do, but explaining your logic with comments is very important. And here's the piece of the logic that I think you have wrong. Each new factorial on the way up is the current number added to itself by the prior factorial number of times.
And here's my comments on your code using my logic on your code and commenting on the errors I see.
int main()
{
int num, fact = 6, temp =1, i , j; //initialize fact to 1.
cout<<"Enter a factorial ";
cin>>num;
for(i = 2; i <= num; i++)
fact=0; /*don't reset fact to zero every time. fact is ever increasing each time through the inner loop. It should be the same as the current number, not zero each time the inner loop starts. You can figure out what variable represents the current number*/
//here's one of those errors from not indenting properly.
{
for(j = 1; j <= 1; j++)-------said code doesnt have effect---because the { should be right after the first for loop, not where it is.
/*this loop controls how many times the current number is added to itself to get the current factorial. the terminating condition is wrong as written, you need the prior factorial repeats. I'll let you figure out which variable in your code represents the prior factorial.*/
fact=fact+temp; //you're adding the wrong value each time through the loop.
temp=fact; //this should be outside the inner loop
}
}
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396