:sad: My teacher is killing me, he just graduated from Yale, and he hasn't been teaching long that is very apparent. I don't know, but I think this has something to do with the complexity of his assignments for this, the first class you take for Computer Science, C++.

In our first assignment on functions, he gives us this. He wants us to find any two prime numbers that add up to an even number. He wants us to print out every even number from 4 to 1000, and the two prime numbers which add up to it. for example: 4=2+2 , 10=3+7 , 12=5+7 36=13+23

We have to implement these 4 functions:

int checkPrimer(int n)
Checks if n is a prime number. Return 1 if yes, return 0 if not.
{ for(int m = 2; m*m <= n; m++)
if(n % m == 0)
return 0;
return 1;
}

int findNextPrimer(int n)
Finds next prime number > than n.
findNextPrimer(20)=23
This function must call checkPrimer.
input n
m = n + 1;
Call checkPrimer(m)
if it is prime return m.
if it is not prime m = m +1,
then call checkPrimer(m) again

int onePlusOne(int e)
Given even number e, find where e=p1+p2.
It returns p1(we can compute p2, because p2=e - p1)
This function must call checkPrimer & findNextPrimer.
input e which is > 4
assign 3 to p1
p2 = e - p1
Call checkPrimer(p2)
if yes return p1
if no then p1 = findNextPrimer(p1)
is p1 <= e, if yes go back up to the
p2 = e - p1 step

int printVerification(int e1, int e2, int width)
Prints out all e=p1+p2, for all even numbers e
between e1 & e2. Width is used to control amount
of expressions on each row. This function must call onePlusOne.

I must use printVerification to output (4, 1000, 5).
Each expression takes 30 spaces, each row has 5 expressions.
Print all twin primer pairs between 4 & 1000. (11, 13) is one pair, they equal 24 and equal number.

Any pointers would be welcome, thankyou in advance. :confused:

Just look at one function at a time. Write, compile, and debug your code until it works. Then go on to the next function, etc. Don't look at the whole thing at once and panic.

I broke it down into the 4 functions I need, just don't know how to go about it.
That took some time in and of itself.
Heres what I got so far, and it doesn't work correctly.
Without any functions at all, just the main.
I don't need any code so to speak, I need a start on how to design an algorithm on a topic(primenumbers) that is new to me. My classmates, most of them are way lost, cause most of them don't even understand the C++ part of it, much less the prime number part.
Thanks for your motivational help though, I appreciate it.

``````[COLOR=#0000ff]#include[/COLOR][COLOR=#800000]<iostream>[/COLOR]
[COLOR=#0000ff]#include[/COLOR][COLOR=#800000]<iomanip>[/COLOR]
[COLOR=#0000ff]#include[/COLOR][COLOR=#800000]<cmath>[/COLOR]
[COLOR=#0000ff]#include[/COLOR][COLOR=#800000]<fstream>[/COLOR]
[COLOR=#0000ff]using[/COLOR][COLOR=#0000ff]namespace[/COLOR][COLOR=#000000] std;[/COLOR]
[COLOR=#0000ff]int[/COLOR][COLOR=#000000] main()[/COLOR]
{
[COLOR=#0000ff]double[/COLOR] i;
[COLOR=#0000ff]int[/COLOR] j,aa,k;
[COLOR=#0000ff]int[/COLOR] prime;
ofstream outfile;
outfile.open([COLOR=#800000]"output.txt"[/COLOR]);

k=0;
[COLOR=#0000ff]for[/COLOR](i = 2; i<= 1000; i++)
{
aa=i;
[COLOR=#0000ff]     for[/COLOR](j = 2; j<= sqrt(i); j++)
{
[COLOR=#0000ff]          int[/COLOR](i)%j?0 :  (aa=0);
}
[COLOR=#0000ff]     if[/COLOR] (aa!=0)
{
k++;
prime[k]=i;
}
}

[COLOR=#0000ff]int[/COLOR][COLOR=#000000] kk=0;[/COLOR]
[COLOR=#0000ff]for[/COLOR] ([COLOR=#0000ff]int[/COLOR] ii=1;ii<=k;ii++)
{
[COLOR=#0000ff]     for[/COLOR] ([COLOR=#0000ff]int[/COLOR] jj=1;jj<=k;jj++ )
{
[COLOR=#0000ff]           if[/COLOR] ((prime[ii]+prime[jj])%2==0)
{
kk++;
[COLOR=#0000ff]                 if[/COLOR](kk%5==0)
{
outfile <<[COLOR=#800000]"("[/COLOR]<<prime[ii]<<[COLOR=#800000]","[/COLOR]<<prime[jj]<<[COLOR=#800000]")"[/COLOR]<<endl;
}
[COLOR=#0000ff]                 else[/COLOR]
{
outfile <<[COLOR=#800000]"("[/COLOR]<<prime[ii]<<[COLOR=#800000]","[/COLOR]<<prime[jj]<<[COLOR=#800000]")"[/COLOR];
}
}
}
}
[COLOR=#0000ff]    return[/COLOR] 0;
}
``````

Just look at one function at a time. Write, compile, and debug your code until it works. Then go on to the next function, etc. Don't look at the whole thing at once and panic.

I would personally take this advice one step further and break functions down into little segments of code to figure out. I am not gonna do your work for you but I will give you a little tip for the prime function to make the code a little more efficient. Only odd numbers can be prime. 2 is the only even prime number. If an even number other than 2 is inputted, then don't call the function to see if its prime. This avoids needless calls and needless computation.

look guys I don't need somebody to do my work for me, heres the program I already turned in listing all the prime numbers from 2 - 997.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double prime;
int count;
int remainder;
int LHu;
LHu = 0;
for(prime = 2; prime <= 1000; prime++)
{
remainder = prime;
for(count = 2; count <= sqrt(prime); count++)
{
int (prime) % count ?0: (remainder = 0);
}
if (remainder != 0)
{
//if......else to get 5 numbers on a line
LHu++;
if (LHu % 5 == 0)
{
cout << setw(6) << prime << " " << endl;
}
else
{
cout << setw(6) << prime << " ";
}
}
}
return 0;
}

All I was asking for is a little advice in how to setup my algorithm, I can do the code. I am a straight A student (3.871), I made an A in this class in the summer, just with a lot easier teacher. I have never had to learn about prime numbers. I am more interested in learning about functions, parameters, arrays, structs, classes, etc. I could care less about prime numbers, and how to find all even numbers in existence and have two prime numbers that equal them. Is prime number excellence a part of C++.
Anyways thanks for your help with my algorithm.
Can somebody just tell me the steps necessary.
Like, for example:
1. get the prime numbers
2. get even numbers
3. find two prime numbers that equal each and every even number known to man
4. print the pairs out (11,13) (3,23) etc.
5. clap your hands cause you did a good job
6. etc.

I don't think I could do this with a paper and pencil, oh well, its only worth like 4% of our grade, and with the curves he gives, don't give me any hints, its ok. After all, I've only put in 5 or 6 hours on trying to find a good algorithm. But that does sound like good advice, break down the problem into little pieces, compile often, etc. Unfortunately, my teacher already told us that on the first day of class. :)

It seems to me that your program is defined above -- each function and it's algorithm -- in your first post. You can almost convert those steps directly into code.

The only thing that's missing is your main() which processes your initial loop from 4 to 1000.

Although I'd change one aspect of your first function listed:

``````{
for ( int m = 2; m*m <= n; m++)
{
if(n % m == 0)
{
return 0;  // this is your TRUE condition
}
}
return 1;  // this is your FALSE condition
}``````

In C/C++, 0 is generally regarded as FALSE, 1 is TRUE.

Thank you sir. I just need my main, thats right. And I need to know the steps to take in the main, its finally coming around. :cool:

yea, thats what I needed all along, the main and the plan to implement all four functions plus main. Thats why in my first post, I put down the 4 functions I needed, and the details of my assignment. All I've got from this forum, is keep your chin up, work on it in small chunks, you already have the algorithm, etc.
I look at some other threads in this same forum area, and guys have asked all kinds of simple questions, like how to count from 1 to 50, add all those #'s together etc.
So for the easy questions, forum members will help you out, but ask you guys something a little tough, and its well I don't want to do all your work for you, try a little harder, etc.
Thats real fair.
Perhaps next time I need some help, I will try to find an easy question for you guys.
Thats the end of the story.
Good day. :cheesy:

#include <iostream.h>
#include <conio.h>
void main(void)
{
clrscr();
int i,p;
for(i=1;i<=300;i=i+1)
{
for(p=2;p<i;p=p+1)
{
if(i%p==0)
break;
else
continue;
}
if(i==p)
cout<<" Prime:"<<p;
}
getch();
}

commented: terrible. just terrible +0

#include <iostream.h>
#include <conio.h>
void main(void)
{
clrscr();
int i,p;
for(i=1;i<=300;i=i+1)
{
for(p=2;p<i;p=p+1)
{
if(i%p==0)
break;
else
continue;
}
if(i==p)
cout<<" Prime:"<<p;
}
getch();
}

What the #&&\$*% is this?

1. `void main()` is a great way to start fail code
Please, for the love of any deity, use int main.

2. The `conio.h` is a huge, non-standard fail.
Don't use this. Just don't. It's not standard, it's not cross-platform, and it's
not even very powerful

3. READ THE RULES AND USE CODE TAGS

Perhaps you should describe what your code is for and why the OP should give a crap about it.

Your solution simply prints all the primes less than 300. This is trivial, and the OP already knows how to determine if a number is prime. His assignment is not so difficult, but this code is anything but helpful to him.

6. Use less pointless code
This is painful:

``````for(p=2;p<i;p=p+1)  // p=p+1?  really?  really??????
{
if(i%p==0)
break;
else            // There is no point at all to using this
continue;   // When the if clause fails, of course the loop will continue
}``````

perhaps:

``for( p = 2; p < i &&  i % p != 0; p++ );``

7. There are far better ways of doing this
Even for the brute force method can be improved by stopping your search short:

``for( p = 2; p < i / 2 &&  i % p != 0; p++ );``

Of course, the brute force method is a poor way of doing this. A simple google search would give you the Sieve of Eratosthenes, which is a decent method.

I'm sorry to be harsh, but, for some reason, your post really infuriated me. Please, please think before you post. Or, maybe don't post advice (if that's what this post is supposed to be) until you are actually able to provide good advice.

All I was asking for is a little advice in how to setup my algorithm, I can do the code. I am a straight A student (3.871), I made an A in this class in the summer, just with a lot easier teacher. I have never had to learn about prime numbers. I am more interested in learning about functions, parameters, arrays, structs, classes, etc. I could care less about prime numbers, and how to find all even numbers in existence and have two prime numbers that equal them. Is prime number excellence a part of C++.

I understand that you are quite daunted by this homework assignment. Please understand that your professor probably doesn't care whether you love prime numbers or not. Rather, this assignment is a good way for you to

1. Think about how to use logic to solve a problem
2. Implement that logic in a program

Learning about the language mechanics is important. However, understanding how to use logical tools to solve a problem is more important. Logic is universal. Language mechanics are rather limited in scope.

You have all the functions. Now, you just need to think about how to combine them to get a working solution. I assure you, this is a very good exercise for an entry level Computer Science student.