943,972 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4423
  • C++ RSS
May 17th, 2007
0

error code

Expand Post »
how to make a program which the out put is:
enter n=5
C++ Syntax (Toggle Plain Text)
  1. 1 2 3 4 5 4 3 2 1
  2. 2 3 4 5 4 3 2
  3. 3 4 5 4 3
  4. 4 5 4
  5. 5
this is the code i use but an error occur
  1. #include <iostream.h>
  2.  
  3. void main()
  4. {
  5.  
  6. int a=10;
  7. int n,i,j=0,k;
  8.  
  9. cout<<"Enter the value for n: ";
  10. cin>>n;
  11. k=n;
  12.  
  13. for (i=0 ;i < 10; i++)
  14. a[I]=i + 1; [/I]
  15.  
  16. [I]while(k>0){ [/I]
  17. [I]for(i=(0+j);i<n;i++) [/I]
  18. [I]cout<<a[I]<<" "; [/I]
  19. [I]for(i=n-2;i>=(0+j);i--) [/I]
  20. [I]cout<<a[I]<<" "; [/I]
  21. [I]cout<<endl; [/I]
  22. [I]j++; [/I]
  23. [I]for(i=0;i<j;i++) [/I]
  24. [I]cout<<" "; [/I]
  25. [I]k--; [/I]
  26. [I]} [/I]
  27. [/I][/I]
another i really need to fin the proper code for factorial using successive addition

this is the code i use
  1. int main()
  2. {
  3. int num, fact = 6,temp =1,i ,j;
  4. cout<<"Enter a factorial ";
  5. cin>>num;
  6.  
  7. for(i=2;i<=num;i++)
  8. fact=0;
  9. {for(j=1;j<=1;j++)-------said code dosnt have effect
  10. fact=fact+temp;
  11. temp=fact;
  12. }
  13. }
please someone help me...thanks




Last edited by WaltP; May 17th, 2007 at 2:10 pm. Reason: AD:add code tags to retain spacing -- But not around code...??
Similar Threads
Reputation Points: 21
Solved Threads: 0
Newbie Poster
jenymaru08 is offline Offline
22 posts
since Apr 2007
May 17th, 2007
0

Re: error code

>>this is the code i use but an error occur
what is (are) the error(s) ?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 17th, 2007
0

Re: error code

in the first code actually i cant say its an erro but error in the codebecause when i try to run it
the output is

1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1
1 1 1
1

why is the result display that one

second code

{for(j=1;j<=1;j++)-------said code doesnt have effect ?????


please help
Last edited by jenymaru08; May 17th, 2007 at 10:57 am.
Reputation Points: 21
Solved Threads: 0
Newbie Poster
jenymaru08 is offline Offline
22 posts
since Apr 2007
May 17th, 2007
0

Re: error code

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).

C++ Syntax (Toggle Plain Text)
  1. #include <iostream.h>
  2.  
  3. //use int main(), not void main()
  4. void main()
  5. {
  6. int a = 10;
  7. int n, i, j=0, k;
  8.  
  9. cout << "Enter the value for n: ";
  10. cin >> n;
  11. k=n;
  12.  
  13. //overwrite the value of a 10 times
  14. for (i = 0; i < 10; i++)
  15. a = i + 1;
  16.  
  17. /*a now equals 10. Since a never changes for the rest of the program it will be 10 for the rest of the program.*/
  18.  
  19. while(k > 0) //control number of lines
  20. {
  21. //General flow:
  22. //each line will have three sections
  23. //initially is a series of spaces
  24. //in the middle is a series of numbers
  25. //int the end is a series of numbers
  26.  
  27. //zero plus any number is always the number so drop the zeros
  28.  
  29. //on the middle part of the line display a and a space n - j times
  30. for(i = (0 + j); i < n; i++)
  31. cout << a << " ";
  32.  
  33. //at the end of the line display a plus a space n - 2 - j times
  34. for(i = n - 2; i >= (0 + j); i--)
  35. cout << a << " ";
  36.  
  37. //start a new line
  38. cout<<endl;
  39.  
  40. j++;
  41.  
  42. //display j spaces initially on the new line
  43. for(i = 0; i < j; i++)
  44. cout << " ";
  45.  
  46. k--;
  47. }
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
May 17th, 2007
0

Re: error code

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
May 17th, 2007
0

Re: error code

Dont use void at the main part.
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
laconstantine is offline Offline
70 posts
since May 2007
May 17th, 2007
0

Re: error code

Dont use void at the main part.
Even I had trouble trying to figure out what this meant

What he means is this
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,744 posts
since May 2006
May 17th, 2007
0

Re: error code

>> 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.
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. int num, fact = 6, temp =1, i , j; //initialize fact to 1.
  4. cout<<"Enter a factorial ";
  5. cin>>num;
  6.  
  7. for(i = 2; i <= num; i++)
  8. 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*/
  9.  
  10. //here's one of those errors from not indenting properly.
  11. {
  12. 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.
  13.  
  14. /*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.*/
  15.  
  16.  
  17.  
  18. fact=fact+temp; //you're adding the wrong value each time through the loop.
  19.  
  20. temp=fact; //this should be outside the inner loop
  21. }
  22. }
Last edited by Lerner; May 17th, 2007 at 2:31 pm.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
May 19th, 2007
0

Re: error code

//This is my code to solve your question.Want a go?
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int num;
  6. cout<<"Enter a factorial ";
  7. cin>>num;
  8. for(int line=1;line<=num;line++){
  9. int list=line;
  10. for(int temp1=1;temp1<list;temp1++){
  11. cout<<' ';
  12. }
  13. for(int temp2=list;temp2<num;temp2++)
  14. cout<<temp2;
  15. for(int temp3=num;temp3>=list;temp3--)
  16. cout<<temp3;
  17.  
  18. cout<<endl;
  19.  
  20. }
  21. }
Last edited by WolfPack; May 19th, 2007 at 7:45 am. Reason: Added [CODE][/CODE] tags
Reputation Points: 10
Solved Threads: 0
Light Poster
meiyantao is offline Offline
31 posts
since May 2007
May 21st, 2007
0

Re: error code

//I am sorry for that I hanvn't notice the char ' ' in your program. Now I rewrite my code in the following:

Quote ...
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter a factorial ";
cin>>num;

for(int line=1;line<=num;line++){
int list=line;
for(int temp1=1;temp1<list;temp1++)
cout<<' '<<' '<<' ';
for(int temp2=list;temp2<num;temp2++)
cout<<temp2<<' ';
for(int temp3=num;temp3>=list;temp3--)
cout<<temp3<<' ';

cout<<endl;
}
cin>>num;
}
Reputation Points: 10
Solved Threads: 0
Light Poster
meiyantao is offline Offline
31 posts
since May 2007

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: C++ maps
Next Thread in C++ Forum Timeline: Help with vector.. simple question





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


Follow us on Twitter


© 2011 DaniWeb® LLC