Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 C++ to C (conversion help)
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
WordLife619




PostPosted: 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.


code:
#include <iostream.h>
#include <stdlib.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;
}



NOTE: If anyone has the solution, please PM me ASAP, if you can.

Thank you, it is much appreciated.
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: Mon Apr 04, 2005 7:33 pm   Post subject: (No 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.
WordLife619




PostPosted: Mon Apr 04, 2005 7:35 pm   Post subject: (No subject)

Thank you very much, however, I didn't find it on the internet. Wink
WordLife619




PostPosted: Mon Apr 04, 2005 7:40 pm   Post subject: (No 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.
Mazer




PostPosted: Mon Apr 04, 2005 7:57 pm   Post subject: (No subject)

Yep, I see only 3 lines that have to be changed. Look up output in C and output in C++.
bugzpodder




PostPosted: Mon Apr 04, 2005 10:54 pm   Post subject: (No subject)

use printf instead of cout
Mazer




PostPosted: Tue Apr 05, 2005 9:05 am   Post subject: (No 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.
WordLife619




PostPosted: Tue Apr 05, 2005 1:39 pm   Post subject: (No 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. Very Happy
Sponsor
Sponsor
Sponsor
sponsor
Mazer




PostPosted: Tue Apr 05, 2005 1:58 pm   Post subject: (No 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?
WordLife619




PostPosted: Tue Apr 05, 2005 2:04 pm   Post subject: (No 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.
Tony




PostPosted: Tue Apr 05, 2005 2:09 pm   Post subject: (No 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
WordLife619




PostPosted: Tue Apr 05, 2005 2:21 pm   Post subject: (No 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)
Martin




PostPosted: Tue Apr 05, 2005 2:37 pm   Post subject: (No subject)

Why are you using math.h?
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: