943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 558
  • C++ RSS
Dec 4th, 2008
0

Please help me with C++!

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. main(void)
  5. {
  6. int N, M, R, D, Y, F, Z, S;
  7. printf("\nPlease enter N\n");
  8. scanf ("%s", N);
  9. printf("\nPLease enter M\n");
  10. scanf ("%s", M);
  11. D=N/M;
  12. Y=M*D;
  13. R=N-Y;
  14. if(R=0)goto A;
  15. printf("%d\n",R);
  16. if(R>0)goto B;
  17. B:
  18. F=M/R;
  19. Z=M*F;
  20. S=M-Z;
  21. A:
  22. return(0);
  23. }
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.
Last edited by Narue; Dec 4th, 2008 at 3:14 pm. Reason: added code tags/formatting
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Martin88 is offline Offline
6 posts
since Dec 2008
Dec 4th, 2008
0

Re: Please help me with C++!

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...
Last edited by mrboolf; Dec 4th, 2008 at 2:47 pm.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 4th, 2008
0

Re: Please help me with C++!

I made a few mistakes pasting the code the best for me is this
C++ Syntax (Toggle Plain Text)
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. main(void)
  5. {
  6. int N, M, R, D, Y, F, Z, S;
  7. printf("\nPlease enter N\n");
  8. scanf ("%d", &N);
  9. printf("\nPLease enter M\n");
  10. scanf ("%d", &M);
  11. D=N/M;
  12. Y=M*D;
  13. R=N-Y;
  14. if(R=0)goto A;
  15. printf("%d\n",R);
  16. if(R>0)goto B;
  17. B:
  18. F=M/R;
  19. Z=M*F;
  20. S=M-Z;
  21. A:
  22. return(0);
  23. }
Last edited by Narue; Dec 4th, 2008 at 3:15 pm. Reason: added code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Martin88 is offline Offline
6 posts
since Dec 2008
Dec 4th, 2008
0

Re: Please help me with C++!

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Martin88 is offline Offline
6 posts
since Dec 2008
Dec 4th, 2008
0

Re: Please help me with C++!

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: 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.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008
Dec 4th, 2008
0

Re: Please help me with C++!

c++ Syntax (Toggle Plain Text)
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. int main(void)
  5. {
  6. int N, M, R, S;
  7. printf("\nPlease enter N\n");
  8. scanf ("%d", &N);
  9. printf("\nPLease enter M\n");
  10. scanf ("%d", &M);
  11. R=(N-(M*(N/M)));
  12. if(R > 0)
  13. {
  14. S=(M-(M*(M/R)));
  15. printf("S = %d\n",S);
  16. }
  17. printf("R = %d\n",R);
  18. int AB=(M*(M-(M*(M/R))));
  19. printf("AB = %d\n",AB);
  20. system("pause");
  21. return(0);
  22. }
Last edited by cikara21; Dec 4th, 2008 at 5:10 pm. Reason: Take [\QUOTE]...:-)
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 4th, 2008
0

Re: Please help me with C++!

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Martin88 is offline Offline
6 posts
since Dec 2008
Dec 4th, 2008
0

Re: Please help me with C++!

Thanks for all i alredy fix my problem and really thanks for the help and i hope that i can help others in the future
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Martin88 is offline Offline
6 posts
since Dec 2008
Dec 4th, 2008
0

Re: Please help me with C++!

AB...???
c++ Syntax (Toggle Plain Text)
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. int main(void)
  5. {
  6. int N, M, R, S;
  7. printf("\nPlease enter N\n");
  8. scanf ("%d", &N);
  9. fflush(stdin);
  10. printf("\nPLease enter M\n");
  11. scanf ("%d", &M);
  12. R=(N-(M*(N/M)));
  13. if(R > 0)
  14. {
  15. S=(M-(M*(M/R)));
  16. printf("S = %d\n",S);
  17. }
  18. printf("R = %d\n",R);
  19.  
  20. //this is AB..
  21. int AB=(M*(M-(M*(M/R))));
  22. printf("AB = %d\n",AB);
  23. system("pause");
  24. return(0);
  25. }
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 4th, 2008
0

Re: Please help me with C++!

cikara21 really thanks for all u help u have been really cool and yes i alredy have AB and really again thanks for u help and support and never doubt to ask me anything maybe not of C++ because im not good enough and thanks again
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Martin88 is offline Offline
6 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Generic Data Types Help!
Next Thread in C++ Forum Timeline: Logical Failure When printf is Removed?





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


Follow us on Twitter


© 2011 DaniWeb® LLC