error code

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 22
Reputation: jenymaru08 is an unknown quantity at this point 
Solved Threads: 0
jenymaru08 jenymaru08 is offline Offline
Newbie Poster

error code

 
0
  #1
May 17th, 2007
how to make a program which the out put is:
enter n=5
  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...??
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: error code

 
0
  #2
May 17th, 2007
>>this is the code i use but an error occur
what is (are) the error(s) ?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 22
Reputation: jenymaru08 is an unknown quantity at this point 
Solved Threads: 0
jenymaru08 jenymaru08 is offline Offline
Newbie Poster

Re: error code

 
0
  #3
May 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: error code

 
0
  #4
May 17th, 2007
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).

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1461
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: error code

 
0
  #5
May 17th, 2007
>>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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 69
Reputation: laconstantine is an unknown quantity at this point 
Solved Threads: 1
laconstantine laconstantine is offline Offline
Junior Poster in Training

Re: error code

 
0
  #6
May 17th, 2007
Dont use void at the main part.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: error code

 
0
  #7
May 17th, 2007
Originally Posted by laconstantine View Post
Dont use void at the main part.
Even I had trouble trying to figure out what this meant

What he means is this
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,673
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: error code

 
0
  #8
May 17th, 2007
>> 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.
  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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 32
Reputation: meiyantao is an unknown quantity at this point 
Solved Threads: 0
meiyantao's Avatar
meiyantao meiyantao is offline Offline
Light Poster

Re: error code

 
0
  #9
May 19th, 2007
//This is my code to solve your question.Want a go?
  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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 32
Reputation: meiyantao is an unknown quantity at this point 
Solved Threads: 0
meiyantao's Avatar
meiyantao meiyantao is offline Offline
Light Poster

Re: error code

 
0
  #10
May 21st, 2007
//I am sorry for that I hanvn't notice the char ' ' in your program. Now I rewrite my code in the following:

#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;
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC