Please help me with C++!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2008
Posts: 6
Reputation: Martin88 is an unknown quantity at this point 
Solved Threads: 0
Martin88 Martin88 is offline Offline
Newbie Poster

Please help me with C++!

 
0
  #1
Dec 4th, 2008
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Please help me with C++!

 
0
  #2
Dec 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 6
Reputation: Martin88 is an unknown quantity at this point 
Solved Threads: 0
Martin88 Martin88 is offline Offline
Newbie Poster

Re: Please help me with C++!

 
0
  #3
Dec 4th, 2008
I made a few mistakes pasting the code the best for me is this
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 6
Reputation: Martin88 is an unknown quantity at this point 
Solved Threads: 0
Martin88 Martin88 is offline Offline
Newbie Poster

Re: Please help me with C++!

 
0
  #4
Dec 4th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Please help me with C++!

 
0
  #5
Dec 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Please help me with C++!

 
0
  #6
Dec 4th, 2008
  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]...:-)
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 6
Reputation: Martin88 is an unknown quantity at this point 
Solved Threads: 0
Martin88 Martin88 is offline Offline
Newbie Poster

Re: Please help me with C++!

 
0
  #7
Dec 4th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 6
Reputation: Martin88 is an unknown quantity at this point 
Solved Threads: 0
Martin88 Martin88 is offline Offline
Newbie Poster

Re: Please help me with C++!

 
0
  #8
Dec 4th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Please help me with C++!

 
0
  #9
Dec 4th, 2008
AB...???
  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. }
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 6
Reputation: Martin88 is an unknown quantity at this point 
Solved Threads: 0
Martin88 Martin88 is offline Offline
Newbie Poster

Re: Please help me with C++!

 
0
  #10
Dec 4th, 2008
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC