| | |
Please help me with C++!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2008
Posts: 6
Reputation:
Solved Threads: 0
Hey hi I have a problem with to C++ programs for my final project, the problem is that i tried to make the code but first the C++ program say general protection exception i read some of this but i still think that the structure of my program is not good so i will post to u the original problem and give me opinions please(I just know basic stuffs of C++):
Make a program to calculate maximum common divisor of two positive whole number using the Euclides algorithm.
The two whole positive numbers are identified with the letters M and N
Euclides Algorithm
1.-Divide N by M, R =Residue
2.- If R=0, M is the maximum common divisor and the program end.
3.-Assign to N the values of M and assign the the values of R to M and start again the step 1
code i use:
The other problem i don't understand the part of no using logarithms the original problem is:
2.- Make a program to calculate AB , B can be positive or negative whole number or zero. Can´t use logarithms.
Thank you in advance for u help and sorry if its difficult to understand my problems but translated from spanish and sorry if i made some mistakes.
Make a program to calculate maximum common divisor of two positive whole number using the Euclides algorithm.
The two whole positive numbers are identified with the letters M and N
Euclides Algorithm
1.-Divide N by M, R =Residue
2.- If R=0, M is the maximum common divisor and the program end.
3.-Assign to N the values of M and assign the the values of R to M and start again the step 1
code i use:
C++ Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<math.h> main(void) { int N, M, R, D, Y, F, Z, S; printf("\nPlease enter N\n"); scanf ("%s", N); printf("\nPLease enter M\n"); scanf ("%s", M); D=N/M; Y=M*D; R=N-Y; if(R=0)goto A; printf("%d\n",R); if(R>0)goto B; B: F=M/R; Z=M*F; S=M-Z; A: return(0); }
2.- Make a program to calculate AB , B can be positive or negative whole number or zero. Can´t use logarithms.
Thank you in advance for u help and sorry if its difficult to understand my problems but translated from spanish and sorry if i made some mistakes.
Last edited by Narue; Dec 4th, 2008 at 3:14 pm. Reason: added code tags/formatting
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
First general advice: use code tags (here is an explanation of how) when posting code.
Second general advice: a more descriptive thread title would result in great improvement of the forum readability - so why not something like "problem with gcd algorithm and logarithms" ? Think about it on the next thread you'll open please.
For calculating GCD(a, b) I'd suggest using a recursive function - it's more elegant and easier to write imho. Hint: use % operator. Try to give more meaningful names to your variable also.
What's AB? A * B? The distance between A and B? Please clarify this point.
Btw this is C++ forum...
Second general advice: a more descriptive thread title would result in great improvement of the forum readability - so why not something like "problem with gcd algorithm and logarithms" ? Think about it on the next thread you'll open please.
For calculating GCD(a, b) I'd suggest using a recursive function - it's more elegant and easier to write imho. Hint: use % operator. Try to give more meaningful names to your variable also.
What's AB? A * B? The distance between A and B? Please clarify this point.
Btw this is C++ forum...
Last edited by mrboolf; Dec 4th, 2008 at 2:47 pm.
•
•
Join Date: Dec 2008
Posts: 6
Reputation:
Solved Threads: 0
I made a few mistakes pasting the code the best for me is this
C++ Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<math.h> main(void) { int N, M, R, D, Y, F, Z, S; printf("\nPlease enter N\n"); scanf ("%d", &N); printf("\nPLease enter M\n"); scanf ("%d", &M); D=N/M; Y=M*D; R=N-Y; if(R=0)goto A; printf("%d\n",R); if(R>0)goto B; B: F=M/R; Z=M*F; S=M-Z; A: return(0); }
Last edited by Narue; Dec 4th, 2008 at 3:15 pm. Reason: added code tags
•
•
Join Date: Dec 2008
Posts: 6
Reputation:
Solved Threads: 0
Ok first sorry for everything and next time i will follow u advice, then in the rpoblem number 2 i really dont know what´s AB because the teacher give us this problems in paper and she said that we can´t ask that we need to think, so I´m asking the same question as u, i hope that ab is equal to a multiplication so is to easy and i don´t really see the use of algorithms.
About the first problem thanks for u answer and i will try to search of what u tell me
Thanks for all
About the first problem thanks for u answer and i will try to search of what u tell me
Thanks for all
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
Well if it's multiplication it's trivial... but I don't see any purpose in mentioning logarithms. Waiting for others to express their opinion on the matter.
Only thing I'd like to add is: avoid use of goto (useful link to understand why)
One (last?) thing:
Only thing I'd like to add is: avoid use of goto (useful link to understand why)
One (last?) thing:
R = 0 is an assignment, use R == 0 to check wether R is equal to 0 or not. Last edited by mrboolf; Dec 4th, 2008 at 3:04 pm.
c++ Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<math.h> int main(void) { int N, M, R, S; printf("\nPlease enter N\n"); scanf ("%d", &N); printf("\nPLease enter M\n"); scanf ("%d", &M); R=(N-(M*(N/M))); if(R > 0) { S=(M-(M*(M/R))); printf("S = %d\n",S); } printf("R = %d\n",R); int AB=(M*(M-(M*(M/R)))); printf("AB = %d\n",AB); system("pause"); return(0); }
Last edited by cikara21; Dec 4th, 2008 at 5:10 pm. Reason: Take [\QUOTE]...:-)
•
•
Join Date: Dec 2008
Posts: 6
Reputation:
Solved Threads: 0
Hey thanks for u help i finally make it work the problem is that i use dev C++ to compile and run but everything work fine if i put the option run until line but when i open the executable file i enter the values and when i finished enter the second value the program just close i read that maybe is because dev doesn´t have the libraries or something like that can anyone help me
AB...???
c++ Syntax (Toggle Plain Text)
#include<stdio.h> #include<conio.h> #include<math.h> int main(void) { int N, M, R, S; printf("\nPlease enter N\n"); scanf ("%d", &N); fflush(stdin); printf("\nPLease enter M\n"); scanf ("%d", &M); R=(N-(M*(N/M))); if(R > 0) { S=(M-(M*(M/R))); printf("S = %d\n",S); } printf("R = %d\n",R); //this is AB.. int AB=(M*(M-(M*(M/R)))); printf("AB = %d\n",AB); system("pause"); return(0); }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Tree Traversal
- Next Thread: Logical Failure When printf is Removed?
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream input int integer java lazy lib linux loop looping loops map math matrix memory multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





