| | |
error code
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 22
Reputation:
Solved Threads: 0
how to make a program which the out put is:
enter n=5
this is the code i use but an error occur
another i really need to fin the proper code for factorial using successive addition
this is the code i use
please someone help me...thanks
enter n=5
C++ Syntax (Toggle Plain Text)
1 2 3 4 5 4 3 2 1 2 3 4 5 4 3 2 3 4 5 4 3 4 5 4 5
c Syntax (Toggle Plain Text)
#include <iostream.h> void main() { int a=10; int n,i,j=0,k; cout<<"Enter the value for n: "; cin>>n; k=n; for (i=0 ;i < 10; i++) a[I]=i + 1; [/I] [I]while(k>0){ [/I] [I]for(i=(0+j);i<n;i++) [/I] [I]cout<<a[I]<<" "; [/I] [I]for(i=n-2;i>=(0+j);i--) [/I] [I]cout<<a[I]<<" "; [/I] [I]cout<<endl; [/I] [I]j++; [/I] [I]for(i=0;i<j;i++) [/I] [I]cout<<" "; [/I] [I]k--; [/I] [I]} [/I] [/I][/I]
this is the code i use
c Syntax (Toggle Plain Text)
int main() { int num, fact = 6,temp =1,i ,j; cout<<"Enter a factorial "; cin>>num; for(i=2;i<=num;i++) fact=0; {for(j=1;j<=1;j++)-------said code dosnt have effect fact=fact+temp; temp=fact; } }
Last edited by WaltP; May 17th, 2007 at 2:10 pm. Reason: AD:add code tags to retain spacing -- But not around code...??
•
•
Join Date: Apr 2007
Posts: 22
Reputation:
Solved Threads: 0
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
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.
•
•
Join Date: Jul 2005
Posts: 1,699
Reputation:
Solved Threads: 273
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).
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)
#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--; }
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jul 2005
Posts: 1,699
Reputation:
Solved Threads: 273
>> 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.
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)
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 } }
Last edited by Lerner; May 17th, 2007 at 2:31 pm.
//This is my code to solve your question.Want a go?
cpp Syntax (Toggle Plain Text)
#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; } }
Last edited by WolfPack; May 19th, 2007 at 7:45 am. Reason: Added [CODE][/CODE] tags
//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;
}
![]() |
Similar Threads
- Error Code 0x80020043 (Apple Hardware)
- Computer keeps crashing- error code help plz (Windows NT / 2000 / XP)
- Error 'not all code paths return a value' (C#)
- Error Code (Windows NT / 2000 / XP)
- Error code COOD11CD (Windows NT / 2000 / XP)
- find out the error in my code (Java)
- deleting registry key for error code 19 (Windows NT / 2000 / XP)
Other Threads in the C++ Forum
- Previous Thread: C++ maps
- Next Thread: Help with vector.. simple question
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline graph homeworkhelp homeworkhelper iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings template text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






