Computer Science Canada C++ to C (conversion help) |
Author: | WordLife619 [ Mon Apr 04, 2005 7:30 pm ] | ||
Post subject: | C++ to C (conversion help) | ||
Hello everyone. I have been recently doing a bonus program for discrete math class. The program had to generate prime numbers in a simple mathematical, expression, where 'n' was the number being subbed into the expression. However, the program must generate prime numbers for the numbers, 'n', 1 through 200. Recently, my friend helped me out, but he did it in C++. The thing is, he is taking a C++ class, and I'm not. I need help to convert the code below into a C construct, WITHOUT changing the algorithm of the program. This may probably seem lowkey for some of the experience programmers on here, but I have tried using my past knowledge of C, but to no avail. Please help. This is the code in C++, and I need it in C.
NOTE: If anyone has the solution, please PM me ASAP, if you can. Thank you, it is much appreciated. |
Author: | Martin [ Mon Apr 04, 2005 7:33 pm ] |
Post subject: | |
Don't use the .h's at the end of the header files. Also, you don't use stdlib anywhere in the program, so I'm going to assume you found it on the internet. |
Author: | WordLife619 [ Mon Apr 04, 2005 7:35 pm ] |
Post subject: | |
Thank you very much, however, I didn't find it on the internet. ![]() |
Author: | WordLife619 [ Mon Apr 04, 2005 7:40 pm ] |
Post subject: | |
Sorry, this is the original program in C++, which I need to convert to C: #include <iostream.h> int main() { int m; const int MAX=200; for (int n=1; n<=MAX; n++) { m =(n*n)-(79*n)+1601; for (int i=2; i<m; i++) { if (m%i==0) m = false; } m = true; if (m==false) { cout << "\n n = " << n << " m = " << m << " It's not prime!\n"; break; } else { cout << "\n n = " << n << " m = " << m << " It's prime!"; } } return 0; } I'm presuming there's probably a few minor modifications needed, since 90% of the time that's my case. |
Author: | Mazer [ Mon Apr 04, 2005 7:57 pm ] |
Post subject: | |
Yep, I see only 3 lines that have to be changed. Look up output in C and output in C++. |
Author: | bugzpodder [ Mon Apr 04, 2005 10:54 pm ] |
Post subject: | |
use printf instead of cout |
Author: | Mazer [ Tue Apr 05, 2005 9:05 am ] |
Post subject: | |
I was trying to let him do it himself. This is for bonus marks, and his friend already gave him most of a solution. |
Author: | WordLife619 [ Tue Apr 05, 2005 1:39 pm ] |
Post subject: | |
I didn't state that my friend gave me most of the solution, he helped me out, but by no means did he give me "most" or "all" of it. ![]() |
Author: | Mazer [ Tue Apr 05, 2005 1:58 pm ] |
Post subject: | |
I'll give you the benefit of the doubt but could you please clarify why, if your friend helped you write it, the program is written in a language you don't know? |
Author: | WordLife619 [ Tue Apr 05, 2005 2:04 pm ] |
Post subject: | |
I assumed he was going to help me in C, but since he is taking a programming class right now in C++, he probably saved himself the trouble of going back to C concepts and just used C++, since it's what he's learning right now. Besides, he's also in my discrete math class, so he did the program as well. I thought I'd be able to apply my past knowledge of C, but it seems I was a bit forgetful this time. The following is what I have now after some changes: #include <math.h> #include <stdio.h> int main() { int m, n, i; int MAX=200; for (n=1; n<=MAX; n++) { m =(n*n)-(79*n)+1601; for (i=2; i<m; i++) if (m%i==0) printf("\n n = %d, m = %d, 'It's not prime!', n, m"); break; if (m%i==1) printf(" n = %d, m = %d, 'It's prime!', n, m"); } return 0; } I ran the program in CodeWarrior IDE, but the Console App Window opens up quickly and closes in a blink of an eye. Any suggestions? I think I'm close to finishing it. |
Author: | Tony [ Tue Apr 05, 2005 2:09 pm ] |
Post subject: | |
I'll take a guess that your program has finished the execution and has closed. Try writing output to a file or place some "wait" block to let the user confirm the output |
Author: | WordLife619 [ Tue Apr 05, 2005 2:21 pm ] |
Post subject: | |
Thank you. Coincidentally, my friend had just suggested that to me about 20 minutes ago. Many thanks to everyone who helped me out. It was well appreciated. I've got the program working now. Wish me luck! 8) |
Author: | Martin [ Tue Apr 05, 2005 2:37 pm ] |
Post subject: | |
Why are you using math.h? |